How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask TheDoctor Your Own Question

TheDoctor
TheDoctor, Professional w/Adv. Degree
Category: Homework
Satisfied Customers: 1590
Experience:  M.S. in Internet Information Systems
59032869
Type Your Homework Question Here...
TheDoctor is online now
A new question is answered every 9 seconds

Need help with completing my javascript/php

Resolved Question:

Need help with completing some aspects of my javascript/php website.

It's only this form that I need help with.

When a user types in their postcode, I want to validate whether that postcode is valid (whether it exists in the database). If it is not valid, I want a error message to appear like the rest of my error messages.

I want this validation to take place without having to click the 'Save' button.

Because I need this validation done in order to populate the 'Current Primary School' selection box with the schools in that postcode from the database.

http://wikisend.com/download/398708/project.php
http://wikisend.com/download/518018/assign.css
http://wikisend.com/download/109520/images.zip
Submitted: 1 year ago.
Category: Homework
Expert:  TheDoctor replied 1 year ago.
Hello again!

Should the postal code be validated as Australian postal code only?
Expert:  TheDoctor replied 1 year ago.
Hello!

In order to complete this, I would need the database and the dbconn.php file.

Please use either phpMyAdmin or MySQL Workbench to export the database. Then zip that file up along with the project files. You can just zip it all up as one zip file. Then upload it to http://ge.tt

Once uploaded, you will be given a link on the right. Click on the 'Copy Link' option and then paste it here.

Thank you
Expert:  TheDoctor replied 1 year ago.
Hello again!

Please download the files here:

http://wikisend.com/download/263948/postValidation.zip

Since I do not have access to your database or your connection parameters, you will have to make some minor changes.

Open up the checkZip.php file. You will see four definition lines at the top for the host, user, password, and database. You will want to change these to match your system settings. Currently, it is set up to use the username root, no password, and connect to a database called "zip". So make certain that you change these to your settings instead.

Once that is done, your postcode validation will work in real time.

In order to check a field against the database without submitting the field, you have to use AJAX. That is what this does. If you attempt to enter no postcode or a postcode that is too short or contains non-numeric data, you will immediately get the error message just like your other errors.

If you enter four numbers, an AJAX call will be made. An AJAX call queries the server for data. This checks the database to see if the postcode is in the valid_postcodes table. This can take a couple seconds. If it is not found in that table, the error message will display. If it is found, the focus will move to the next field.

Let me know if you have any questions. I'm here to help.

If you require assistance in the future, please feel free to request me directly by starting your questions with **For TheDoctor**.

Please remember to rate my answer. Thank you so much and have a wonderful day.
Expert:  TheDoctor replied 1 year ago.
Hello again!

Please let me know if you have any issues or require additional assistance. AJAX can be difficult to understand at first if you are not already familiar with it. Note that AJAX must be run from on a server due to same-domain constraints. This shouldn't be an issue since you have to run PHP on a server anyway.

Alternately, would could have dynamically built a JavaScript array with all of the zip codes from the database in it. This could be done when the page loads and then the validation could check the array rather than query the database with AJAX in real-time. That is certainly an option, but a significantly less advanced one. Let me know what you think and if you have any issues with the AJAX validation.

Please remember to rate my answer. Thank you again!
Customer: replied 1 year ago.
Hi thanks for the reply.

This option is better for me:

Could have dynamically built a JavaScript array with all of the zip codes from the database in it.

Customer: replied 1 year ago.
Or if we use the ajax way, can you please answer the questions in this file and execute it in the project.php ?

http://wikisend.com/download/510356/qn.txt

Seems like i can't copy and paste code in this 'reply to expert' - gives an error.
Expert:  TheDoctor replied 1 year ago.
No, you cannot have the code in the main file. If it were in the main file, then you would need to reload that file in order for this to work. That would completely defeat the purpose and it is not what you requested.

In order to obtain information from the database without reload the page you are currently on, we have to have another script to call. That is the purpose of checkZip.php

Additionally, an AJAX back-end file (what this is) should output ONLY the data relevant to your query. checkZip output either true or false, nothing else. You absolutely cannot put the AJAX code in your main file. It must be in its own, separate file. Your instructor would be very familiar with this. All AJAX ever written anywhere in the world is contained in its own file. That's how AJAX works.

The rest of your questions concern standard database functions in PHP.

The first is a prepared statement. The question mark is a place holder.

On the very next line, is the bind parameter. It tells PHP to put a string element (the "s") into the query at the placeholder (?). This is the preferred method of sending data into a query as it automatically sanitizes the data and prevents SQL Injection attacks.

Your third question is not applicable, as you absolutely cannot place the checkZip.php code inside of your project.php file. It must remain in its own file.

How would you like to proceed?

Expert:  TheDoctor replied 1 year ago.
Hello again!

In the interest of providing excellent service, here is the project completed using the other method (JavaScript Array without AJAX).

Attachment: 2013-05-23_041741_othermethod.zip



With this method, there is no need to have the checkZip file at all. Another advantage to this method is that there is no delay when entering the zip code. The validation is always instant.

In the project.php file, on lines 217 - 220 you will find the same DEFINE statements that I mentioned earlier. Make sure that you change these to use your database connection parameters instead of mine.

Let me know if you need anything else. As always, I'm here to help.

Please remember to rate my answer. Thank you again!
Customer: replied 1 year ago.
Hi

When i uploaded the project.php file and the jquery.js file to my server - it doesn't load the page contents at all - probably cause there's an error.

I noticed that if i remove the php code you've added for the postcode - the contents is displayed.

Can you please enter these details in the file.

This is just dummy values, but we were given just this from our instructor.

$dbConn = mysql_connect("localhost", "abc123", "abcdefghi");

"abc123" is the database
"abcdefghi" is the password


Thanks.
Customer: replied 1 year ago.
I really need help to complete this form

http://wikisend.com/download/164142/FormValidationForProject.zip

I have taken screenshots of the validation required as well as the tables in the database.

I cannot access the database via phpMyAdmin or MySQL Workbench. Our instructor has made that clear - probably to prevent us from exporting it. We're working on the school's server.

Can you please help finish the form validation for the other parts as well.

Even if you don't have access to the database - please implement the code anyway.

Thanks so much.
Expert:  TheDoctor replied 1 year ago.
Hello again,

I cannot work on all of the other validation parts. In this question, you asked for work on the postcode validation, not the validation of the entire form. Working on the validation of the entire form adds an additional 4+ hours onto this work. Additionally, you may not add entirely new requirements to a question well after the question has been asked. You must enter a new question if you require additional work.

As for the postcode and uploading to the server, it is likely that this server is using PHP4. If I put those values into mysql_connect without access to that server, it is just going to cause a fatal error on my system. The code I gave you is 100% functional. However, if the server is using PHP4 (which is about 6 or 7 years outdated now), then it will not work.

Can you please write a text file or document with information about the server and upload it to http://ge.tt and then provide me with the link.

Again, I can only help with what you asked in your original question (postcode validation). Adding on a massive amount of work beyond that is technically a violation of site policy. You must enter a new question for that as they are tasks that are not related to your original request.
Expert:  TheDoctor replied 1 year ago.
In addition to what I stated in the previous response, please download this file:

http://wikisend.com/download/557210/phpinfo.php

Upload that to your server and then navigate to it. Take a screen shot of the top of the page (the part that display the PHP Version) and send that to me.

Additionally, please zip up exactly what you uploaded to your server and send that to me.

And, if you can put information about working with this server to a file, please send that to me also.

Thank you
Expert:  TheDoctor replied 1 year ago.
Hello again,

Did you still want assistance with this? The code I gave you in both instances works perfectly. However, neither will work on PHP4. If your server is running PHP4, I can rewrite it for you to be in PHP4. That is why I asked you to upload that file and get a screenshot of it so that I can see the PHP version. PHP4 and PHP5 are very different.

Also, I wanted to see exactly what you uploaded to see if there are any errors in what you changed. Also, send me your dbconn.php file. In this code:

$dbConn = mysql_connect("localhost", "abc123", "abcdefghi");

"abs123" is not the database name. That is the user name.

Please provide the requested additional information so that I can continue assisting you.
Customer: replied 1 year ago.
Hi thanks for you help.

The version is PHP 5.3.21.
I've just fixed it and it works.

If you cannot help me with completing the validation of the whole form, can you at least help in extending the postcode validation as I mentioned in my initial question - that i need the postcode validation to occur in order to generate the list of schools available for that postcode in the select box below "Current Primary Schools".

How do I use the postcode that was entered to generate the list of schools in the select box?

If you can do/show me this, then I will be able to finish the form myself.

FYI, I am now using the project.php file with the postcode php query in it that you sent me.

Expert:  TheDoctor replied 1 year ago.
Sure, I will let you know when I have that part ready.

Thank you
Expert:  TheDoctor replied 1 year ago.
Hello again!

Please download the files here:

http://wikisend.com/download/520800/popfield.zip

With this change, when you enter postcode that is valid, the primary school drop down will populate with schools associated with that postcode.

You probably have to make changes to the project.php file. Again, the file I am sending has my connection parameters because putting different parameters on my system would prevent me from being able to test my own work. Simply update the values to match your system.

The file is basically the same as the last one I sent you. However, it now includes the code to populate that drop down. It is somewhat complex code and took a little while to write.

Let me know if you have any questions.

Please remember to rate my answer. Thank you again.
Customer: replied 1 year ago.

Hi


 


I've uploaded the files and the contents doesn't load - as before.


 


I've added my own connection parameters.


 


This is what I uploaded.


FYI, our username and database name is XXXXX XXXXX


 


I doubt the error is the connection parameters.


 


http://wikisend.com/download/595188/projectv2.php

Expert:  TheDoctor replied 1 year ago.
Hello again,

I can assure that the code I sent you works perfectly. I am testing the code functionality myself and it works. Would you be will to join me in a short screen sharing session so that I can see your screen?

Simply go to join.me

Once there, click on the big orange share button. It may ask you to install some stuff and/or allow some stuff. Make sure you install/allow everything.

Once it is up and running, you will have a little dashboard near the top of your screen. Within it, there is a little URL. Select it and copy that url. Then paste it here.

I will join you and you can show me so that I can see for myself.

Thank you
Customer: replied 1 year ago.

No I cannot.


 


Is there a simpler way of doing this?


 


A basic level but maybe more code?


 


If not, that's ok.

Expert:  TheDoctor replied 1 year ago.
Can you give me the URL to the actual page? There is likely an error being reported, but you cannot see it unless you view the page source. I could view the source and see what the error is, but I will need the URL so that I can view the page.

I wrote the new query based on the database specifications that you sent me. My guess is that there is an error on the query for whatever reason. I need to see what that is. Alternately, do this:

Go to the URL. Once there and the page has loaded without the content, right click on the page and select "view source". Copy every thing (CTRL-A, CTRL-C) and the paste it into a file. Then send me the file.

Either the file or the URL will work. The URL would be easier.
Expert:  TheDoctor replied 1 year ago.
As I mentioned in the previous message, it is likely that there is an error being displayed when you "view source". Please either provide me with the URL to the page or right click on the page, select "View Source" and copy all of the source.

Paste it into a file and then send it to me. I would prefer the URL.

Thank you
Customer: replied 1 year ago.

Here's the error:


 


var validPostCodes = ['2747','2750','2753','2760','2777'];Unknown column 'ls.postcode' in 'field list'


At the bottom of the file:


 


http://wikisend.com/download/943090/error.txt

Expert:  TheDoctor replied 1 year ago.
Alright,

That is saying that the local_schools table does not have a column called postcode. However, in the database definition files that you sent me, the local_schools table DOES have a column named postcode.

So, the code I sent you is accurate based on what you sent me. However, what you sent me is not what is actually in the database. This is stating that local_schools has no column called postcode.

To fix that error, I would need to see what the local_schools table actually looks like, because the data definition you sent me is wrong. The code I sent you is accurate and would work perfectly if the database actually conformed to the database definition you provided me with.

Can you get what the table actually looks like? If you look at what you sent me, it says that it has a field called "postcode". But the system is erroring out saying that there is no field called postcode.
Expert:  TheDoctor replied 1 year ago.
It looks like later on in your PHP file, you make reference to postcodeID, which is not what is in the data definition files that you sent me. I'll try using that instead and send you the revised version.
Customer: replied 1 year ago.

Thanks do that - our instructor has changed the requirements a few times including the column names in the database.


 


So i sent you the old one.


 


It is postcodeID .

Customer: replied 1 year ago.

 


 


Please note:


 


 


in the local_schools table


 


it is postcodeID


 


 


in the valid_postcodes table


 


it is postcode


 


 


Thanks!

Expert:  TheDoctor replied 1 year ago.
That doesn't make any sense.

The postcode table is supposed to have a primary key field called either ID or postcodeID.

The postcode filed contains the actual postcode. So is the primary key called postcodeID or ID?
Expert:  TheDoctor replied 1 year ago.
Are you capable of running SQL commands on the database via command line?
Customer: replied 1 year ago.

new


 


http://wikisend.com/download/251466/databasetables.png

Expert:  TheDoctor replied 1 year ago.
I cannot tell if the postcodeID holds an actual postcode or it is a foreign key to the primary key of the postcode table.

According tot he PNG you are sending, the primary key of the postcode table is just called id (which is absolutely horrible practice, by the way).

A foreign key always references the a key of the table it is referencing. So if that is the case, then postcodeID should link to id. Unless the valid_postcodes table actually has a field called postcodeID instead of ID. Writing code for a database without being able to see the REAL database schema (not a png) is like operating on a person in the dark.

So here is what I am doing:

http://wikisend.com/download/430838/again.zip

I have sent you the file with three different versions of the query. Two of them are commented out, from lines 256 - 274.

If the first does not work, commented it out and uncomment the second. Then do the same for the third. One of them will work.

As always, check the source and see the error. When you see things about bad columns, it means that the column doesn't exist.

If you have command-line access to the MySQL database, you can get the actual schema. Let me know how it goes.

Please remember to rate my answer. Thank you again.
Customer: replied 1 year ago.

Hi you didn't send the new file - cause I don't see any comments as per above.


 


But I did manage to change mine - this doesn't give any errors now - contents is displayed.


 


Note - in the png file - 'name' in school_info has also changed to 'Name'


 


http://wikisend.com/download/174578/myform.php


 


But the select box doesn't come up with anything - once a valid postcode is in.


 


So now it's a matter of getting the database tables to work together as per the new png file i sent to give me a list of schools.


 


 


 


 

Expert:  TheDoctor replied 1 year ago.
I did send you the changes. They were in project.php, not projectv2.php

I just downloaded the file from this thread, and my comments are there. And everything works perfectly. The problem is that if you are joining incorrectly, it will not necessary bring up an error, but it will not populate the drop downs.

Again, do you have command-line access to mysql?
Expert:  TheDoctor replied 1 year ago.
I just reviewed your code. It absolutely would not work. postcodeID is an ID for a postcode. It is not a postcode itself. You cannot do that. I'm altering your code.
Expert:  TheDoctor replied 1 year ago.

First, try this one: http://wikisend.com/download/271194/project.php

 

If that one doesn't work, try this one:

 

http://wikisend.com/download/317134/project.php

 

See, you have to join onto the valid_postcodes table. That is an absolute must.

 

This is actually a ridiculously simple and basic development task. What is making it so difficult is not seeing the actual table schemas. If I could see those, I would have had this code to you in 10 minutes with no issues.

 

Please remember to check/reload this form more often. I sent these two files over 30 minutes ago. Again, joining with valid_postcodes is absolutely necessary.

Customer: replied 1 year ago.

ok but in your query


 


$schoolQuery = $db->prepare("SELECT DISTINCT s.schoolID, s.Name, vp.postcode " .
"FROM school_info s " .
"JOIN local_schools ls " .
"ON s.schoolID = ls.schoolID " .
"JOIN valid_postcode vp " .
"ON ls.postcodeID = vp.postcodeID " .
"ORDER BY s.Name") or die ($db->error);


 


 


Didn't work because vp.postcodeID is meant to be vp.postcode


and valid_postcode is meant to be valid_postcodes


 


So i changed it to this:


 


$schoolQuery = $db->prepare("SELECT DISTINCT s.schoolID, s.Name, vp.postcode " .
"FROM school_info s " .
"JOIN local_schools ls " .
"ON s.schoolID = ls.schoolID " .
"JOIN valid_postcodes vp " .
"ON ls.postcodeID = vp.postcode " .
"ORDER BY s.Name") or die ($db->error);


 


 


And the page loads, but drop box doesn't work.


 


This is annoying. :)


 

Expert:  TheDoctor replied 1 year ago.
No, you are wrong.

It is not meant to be ls.postcodeID = vp.postcode

That is wrong. It is completely wrong.

There are two columns in valid_postcode

postcodeID DOES NOT EQUAL postcode from the other table. That is wrong.

postcodeID is the PRIMARY KEY of the postcode table.

The primary key of the postcode table is not postcode

IT IS ID (according to your document)

so it should be

ON ls.postcodeID = vp.id

postcodeID is NOT, NOT, NOT postcode. Is a a KEY that relates to a postcode. That's why it is called a relational database. Change it to :

ON ls.postcodeID = vp.id

Expert:  TheDoctor replied 1 year ago.
$schoolQuery = $db->prepare("SELECT DISTINCT s.schoolID, s.Name, vp.postcode " .
"FROM school_info s " .
"JOIN local_schools ls " .
"ON s.schoolID = ls.schoolID " .
"JOIN valid_postcodes vp " .
"ON ls.postcodeID = vp.id " .
"ORDER BY s.Name") or die ($db->error);
Customer: replied 1 year ago.

Great the selection box works!


 


Except - not sure if there should be that many schools listed for the one postcode.


 


Here are the database records for each table.


 


http://wikisend.com/download/163912/database_records.zip


 


Test it out!

Expert:  TheDoctor replied 1 year ago.
How many are you getting for a single zip and which zip are you entering?
Customer: replied 1 year ago.

I tried all the postcodes


 


 


2777 - i get 6 schools


 


 


and I just noticed 3 are primary schools and 3 are high schools.


 


So the school_type also needs to be taken into consideration.


 



All the ones marked 'P' in the table.


 


 


Because this needs to be all primary schools only.


 


 


 


 

Expert:  TheDoctor replied 1 year ago.
The number is XXXXX correct. There are six entries for that zip code based on the data you sent me. However, we do need to do only the primary schools, so change that query to:

$schoolQuery = $db->prepare("SELECT DISTINCT s.schoolID, s.Name, vp.postcode " .
"FROM school_info s " .
"JOIN local_schools ls " .
"ON s.schoolID = ls.schoolID " .
"JOIN valid_postcodes vp " .
"ON ls.postcodeID = vp.id " .
"WHERE s.school_type = 'P' " .
"ORDER BY s.Name") or die ($db->error);

Very simple.

See if that gets you down to three. Then please remember to rate my answer.

Thank you again
Expert:  TheDoctor replied 1 year ago.
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
If you've already paid for this answer, simply Login.
TheDoctor, Professional w/Adv. Degree
Category: Homework
Satisfied Customers: 1590
Experience: M.S. in Internet Information Systems
TheDoctor and other Homework Specialists are ready to help you
Customer: replied 1 year ago.

Hi yes.


 


It's all working now.


Thanks for your help!

Expert:  TheDoctor replied 1 year ago.
You are very welcome!

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
< Last | Next >
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
  • He answered my question promptly and gave me accurate, detailed information. If all of your experts are half as good, you have a great thing going here. Diane Dallas, TX
 
 
 

Meet The Experts:

 
 
 
  • Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4522
    More than 5000 online tutoring sessions.
< Last | Next >
  • http://ww2.justanswer.com/uploads/BU/BusinessTutor/2012-2-2_115741_Kouki2.64x64.jpg Manal Elkhoshkhany's Avatar

    Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4522
    More than 5000 online tutoring sessions.
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Engineer

    Satisfied Customers:

    3458
    Expert in Java C++ C C# VB Javascript Design SQL HTML
  • http://ww2.justanswer.com/uploads/LI/lindaus/2012-6-10_04811_IMG20120609164157.64x64.jpg Linda_us's Avatar

    Linda_us

    Finance, Accounts & Homework Tutor

    Satisfied Customers:

    3124
    Post Graduate Diploma in Management (MBA)
  • http://ww2.justanswer.com/uploads/chooser77/2009-08-18_162025_Chris.jpg Chris M.'s Avatar

    Chris M.

    M.S.W. Social Work

    Satisfied Customers:

    2385
    Master's Degree, strong math and writing skills, experience in one-on-one tutoring (college English)
  • http://ww2.justanswer.com/uploads/JawaadAhmed/2009-6-27_12137_SIs_SHadi.jpg F. Naz's Avatar

    F. Naz

    Chartered Accountant

    Satisfied Customers:

    1988
    Experience with chartered accountancy
  • http://ww2.justanswer.com/uploads/JK/jkcpa/2011-1-16_182614_jkcpa.64x64.jpg Bizhelp's Avatar

    Bizhelp

    CPA

    Satisfied Customers:

    1876
    Bachelors Degree and CPA with Accounting work experience
  • http://ww2.justanswer.com/uploads/avremote/photoa.jpg Seanna's Avatar

    Seanna

    Tutor

    Satisfied Customers:

    1781
    3,000+ satisfied customers, all topics, A+ work
 
 
 
Chat Now With A Tutor
TheDoctor
TheDoctor
Software Engineer
738 Satisfied Customers
M.S. in Internet Information Systems