Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  2. #2
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    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"?

  3. #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,065
    Thanks
    3
    Thanked 502 Times in 490 Posts
    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.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •