How do I use the selected item in a drop down list to pupulate on items in php?

Go To StackoverFlow.com

0

I populate my drop down list from a table in my database. I am trying to grab to the selected item in the drop list that the user selects, so that I can then query the database to get more information related to that item.

<html>
<head></head>
<body>
<br>    
<table id ="topnav" width="1100" border="0" align="center">
    <tr>
    <td colspan="2" style="background-color:#FFA500;">
    <h1><table border="0">
    <tr>
    <th colspan="6"><img src ="../_images/Eu_flags.jpg" border="0" align="top" width="1100" height="180" />
    </th>
    </tr>

<table></h1>
    </td>
    </tr>

    <tr valign="top">
    <td style="background-color:#FFD700;width:150px;text-align:top;">


    <b>Menu</b><br />

    Select the state<br>
    See rank of Universities<br />
    Other information
    </td>
    <td style="background-color:#eeeeee;height:100px;width:940px;text-align:top;">
<?php
        $con = mysql_connect("localhost","root","pasword");
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db("test", $con);
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }


        $result = mysql_query("SELECT lnamestate FROM state" );

        while($row = mysql_fetch_array($result))
        {
        $lnamestate_id = $lnamestate['id'];
        $row_lnamestate=$row['lnamestate'];
        $row_block .= '<OPTION value="'.lnamestate_id.'">'.$row_lnamestate.'</OPTION>';
        }
        mysql_close($con);

?>  
        <label for="row">Select state</label>
        <select name="lnamestateID"><?php echo $row_block; ?></select>



    </td>
    </tr>

    <tr>
    <td colspan="2" style="background-color:#FFA500;text-align:center;">
    </td>
    </tr>
</table>



</table>

</body>

</html>
2012-04-04 03:34
by raheleh etminan
There are plenty of examples on how to chain a form so that one field determines the values in another. This one is for ajax, but the concepts still hold: http://stackoverflow.com/questions/6495657/display-data-from-two-mysql-tables-by-getting-values-from-drop-down-list-using- - Marc B 2012-04-04 03:41


1

First, you need to add an Event Handler to the Select Control so that when something is selected, you can catch this event, you can achieve through the following;

<select name="ab" onchange="if (this.selectedIndex) doSomething();">

Now, hold the index to know what has been selected, or if you want, through the index, get the selected item and put it in a global variable in javascript.

If you to load more information when an item has been selected use ajax with xmlHttpRequest. In many web application, when you use JQuery, it facilitates many task for you. Have a look at JQuery here, its simple to use and eases many things.

2012-04-04 03:47
by Noor


0

You're going to need AJAX:

$('select.whatever').change(function(){
   $.ajax({
      url: '/path/to/your/script.php',
      data: 'var='+$(this).val(),
      type: 'POST',
      success: function(data) {
         $('.insert_element').html(data);
      }
   });
});

This takes the value of the element being changed, sends it to the script (which can then query the DB), and returns the data in a particular element.

2012-04-04 15:36
by hohner
Ads