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, Professional w/Adv. Degree
Category: Homework
Satisfied Customers: 1757
Experience:  M.S. in Internet Information Systems
Type Your Homework Question Here...
TheDoctor is online now
A new question is answered every 9 seconds

Need help with completing my javascript/php

This answer was rated:

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.
Hello again!

Should the postal code be validated as Australian postal code only?

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

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

Thank you
Hello again!

Please download the files here:

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.
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 4 years 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 4 years ago.
Or if we use the ajax way, can you please answer the questions in this file and execute it in the project.php ?

Seems like i can't copy and paste code in this 'reply to expert' - gives an error.
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?

Hello again!

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


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 4 years ago.

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

Customer: replied 4 years ago.
I really need help to complete this form

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.
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 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.
In addition to what I stated in the previous response, please download this file:

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
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 4 years 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.

Sure, I will let you know when I have that part ready.

Thank you
Hello again!

Please download the files here:

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 4 years ago.



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.

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

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 4 years ago.

No I cannot.


Is there a simpler way of doing this?


A basic level but maybe more code?


If not, that's ok.

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.
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 4 years 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:


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.
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 4 years 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 4 years ago.



Please note:



in the local_schools table


it is postcodeID



in the valid_postcodes table


it is postcode




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?
Are you capable of running SQL commands on the database via command line?
Customer: replied 4 years 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:

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 4 years 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'


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.





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?
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.

First, try this one:


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


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 4 years 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. :)


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 =

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 =

$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 = " .
"ORDER BY s.Name") or die ($db->error);
Customer: replied 4 years 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.


Test it out!

How many are you getting for a single zip and which zip are you entering?
Customer: replied 4 years 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.





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 = " .
"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

You need to spend $3 to view this post. Add Funds to your account and buy credits.
TheDoctor and 3 other Homework Specialists are ready to help you
Customer: replied 4 years ago.

Hi yes.


It's all working now.

Thanks for your help!

You are very welcome!

Related Homework Questions