How to parse a function's result to php select dropdown value property.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Easytime
    New Member
    • Nov 2012
    • 26

    How to parse a function's result to php select dropdown value property.

    Dear pros, I have 2 select option comboboxes on the form. One combo box reads data from mysql on pageLoad event.
    OnChange of the selected option, php should read another data from mysql into the second combo box.
    The second read-data should be in a function (not necessarily, just if there is a simpler way to do it). But please check the code below: Thank you!

    Code:
    <form name="form1" action="confirm_zone.php" method="post" style="margin-left:15px; margin-right:20px;">
    	    <p><b style="color:#990000;">Select / Choose your Region:</b>
    
    <?php
    //this code reads first data from mysql and populate 
    //combo box 1.
    //this is where the onChange event will come in.
    
    $load_region = mysql_query("SELECT region_number FROM region ORDER BY region_number");
    echo "<Select name='region' size='1' onChange='". read($data)  ."'> ";//read($data) is a presumed function
    
    while ($row = mysql_fetch_array($load_region)) {
      echo '<option value="'. $row['region_number'] .'">' . $row['region_number'].'</option>'; } 
    echo "</select> ";
    ?>
    
    &nbsp;&nbsp;&nbsp;&nbsp;
    <b style="color:#990000;">Select your Province:</b>
    
    <?php
    //this should be the function.
    function read($data) {
    
    $dropdown ="<Select name='province' size='1'> ";
    $load_province = mysql_query("SELECT province_number, province_location FROM province ORDER BY province_number");
    
    while ($row = mysql_fetch_array($load_province)) {
    $dropdown .= "\r\n<option value='{$row['province_location']}'" . "-{$row['province_number']}>" ."{$row['province_location']}" . " {$row['province_number']}".  "</option>"; }
    
    $dropdown .= "\r\n</select>";
    echo $dropdown."<br>";
    }
    ?> <!-- end of function -->
    Thank you!
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You'll need to use an AJAX request for that. Here is a link to an AJAX tutorial: http://www.w3schools.com/ajax/

    Comment

    • Easytime
      New Member
      • Nov 2012
      • 26

      #3
      how will I combine AJAX with php to read from mysql database?
      How is that possible?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        It shows you how in the tutorial.

        Comment

        Working...