Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-04-2008, 01:36 PM   PM User | #1
asherinho
New to the CF scene

 
Join Date: Jul 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
asherinho is an unknown quantity at this point
javascript-php???!!!

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

Code:
<html>
<head>
<title>Chained Selects</title>
<script language="javascript" src="chainedselects.js"></script>
<script type="text/javascript" src="reference.php"></script>
</head>
<body onload="initListGroup('vehicles', document.forms[0].make, document.forms[0].type, document.forms[0].model, 'cs')">
<form>
<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')">
</tr></table>
</form>
</body>
</html
The values to be filled in these select fields are obtained from reference.php which runs javascript codes as shown below
PHP Code:
<? 
header
("content-type:application/javascript");    
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?
asherinho is offline   Reply With Quote
Old 09-04-2008, 02:25 PM   PM User | #2
Samhain13
Regular Coder

 
Samhain13's Avatar
 
Join Date: Aug 2008
Location: Pilipinas
Posts: 165
Thanks: 4
Thanked 18 Times in 18 Posts
Samhain13 is on a distinguished road
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"?
Samhain13 is offline   Reply With Quote
Old 09-04-2008, 02:25 PM   PM User | #3
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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 -

Code:
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 -

PHP Code:
<?php
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"); 
EOT;
?>
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.
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:20 AM.


Advertisement
Log in to turn off these ads.