View Full Version : javascript-php???!!!

09-04-2008, 02:36 PM
hi! Can u please tell me whats wrong with my codes cause it doesn't work and I don't see where I went wrong.I have a page with two select fields where the second select field is populated with the values depending on the selection made in the first field.And this page refers to a php page which I refered it as a javascript page as shown below

<title>Chained Selects</title>
<script language="javascript" src="chainedselects.js"></script>
<script type="text/javascript" src="reference.php"></script>
<body onload="initListGroup('vehicles', document.forms[0].make, document.forms[0].type, document.forms[0].model, 'cs')">
<table align="center"><tr>
<td>Select a vehicle:&nbsp;</td>
<td><select name="make" style="width:160px;"></select></td>
<td><select name="type" style="width:160px;"></select></td>
<td><input type="button" value="Reset" onclick="resetListGroup('vehicles')">

The values to be filled in these select fields are obtained from reference.php which runs javascript codes as shown below

echo "
var disable_empty_list=true;<br>

addListGroup(\"vehicles\", \"car-makers\");<br>

addOption(\"car-makers\", \"Select vehicle\", \"\", \"\", 1); <br>
addList(\"car-makers\", \"Toyota\", \"Toyota\", \"Toyota\");<br>
addList(\"car-makers\", \"Nissan\", \"Nissan\", \"Nissan\");<br>

addOption(\"Toyota\", \"Select maker\", \"\", \"\", 1);<br>
addList(\"Toyota\", \"\");<br>
addList(\"Toyota\", \"Hilux\");<br>
addList(\"Toyota\", \"Land cruser\");<br>

addOption(\"Nissan\", \"select maker\", \"\", \"\", 1);<br>
addList(\"Nissan\", \Safari\");<br>
addList(\"Nissan\", \"Hard body\");<br>
addList(\"Nissan\", \"Terrano\");

Whats wrong with this reference.php page?

09-04-2008, 03:25 PM
The "<br>"s in your PHP don't have to be there.
And, correct me if I'm wrong, but shouldn't the header say, "text/javascript"?

09-04-2008, 03:25 PM
Your browser should have been indicating errors (line numbers/character positions) in the javascript.

To start with <br> is not valid javascript. Remove all of them in reference.php.

The following line is missing a double-quote -

addList(\"Nissan\", \Safari\");

The header() statement is not necessary.

And I recommend using the HEREDOC syntax so that you don't need to do things like escape all the double-quotes (which is probably how the double-quote got lost.) The following worked for me -

echo <<<EOT
var disable_empty_list=true;

addListGroup("vehicles", "car-makers");

addOption("car-makers", "Select vehicle", "", "", 1);
addList("car-makers", "Toyota", "Toyota", "Toyota");
addList("car-makers", "Nissan", "Nissan", "Nissan");

addOption("Toyota", "Select maker", "", "", 1);
addList("Toyota", "");
addList("Toyota", "Hilux");
addList("Toyota", "Land cruser");

addOption("Nissan", "select maker", "", "", 1);
addList("Nissan", "Safari");
addList("Nissan", "Hard body");
addList("Nissan", "Terrano");

If you copy/paste the above, make sure that the EOT; at the end has nothing else on the line with it. The forum software usually adds a space at the end of the line and this will prevent the code from working.