简体   繁体   中英

Auto populate two drop down menus

There are two Drop down menus, A and B.

The values in A are:- Car, Bike, Plane The values in B are (when car is selected):- ferari, mercedes When Bike is selected:- Ducati, harley when plane is selected:- MIG, Concorde

Also after the Value from both the Drop menus are selected, a text box with the price will be updated, and i need this to be a recurring process. How do i implement it? I am fairly new to PHP.

First I would like to thank jogesh_p for his help.

I have finally managed to auto populate both the drop down menus, and made the 2nd drop down menu dependent on the 1st drop down menu.

As jogesh mentioned his blog at Dynamic select box with php and jQuery

did help a lot.

However, I am going to post the code that i have used, as forums tend to occupy the higher results in Google and to make it a bit more easy finding for other new php programmers like me.

The coding was done in two pages:

  1. attempt.php (this was the main page)
  2. level.php (this page is called when the value of the first dropdown menu is changed)

jQuery and PHP have been used.

attempt.php :

<?php
//connect to the database
$con = mysql_connect("localhost","root","12345") or die("error ".mysql_error());

//connect to the trav table
mysql_select_db("trav",$con) or die("error ".mysql_error());
?>

<html>
<head>    
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
   <script>
   $(function()
   {

        $(window).load(function()
        {
            $('#building').change(function()
            {
                var parentValue = $(this).val();


                // Determine the selected Value
                if( parentValue.length != "" )
                {
                    $.ajax(
                    {
                        url: 'level.php',
                        data: {parent: parentValue},
                        success: function(data)
                        {
                            $('#level').html(data);
                        }
                    });
                }
                else
                {
                    $('#level').html('<option value="">Please Select</option>');
                }
            });
        });
    });
    </script>
</head>

<body>
<form method="post" action="<?php echo $PHP_SELF;?>">

    <table cellpadding="6" cellspacing="1">
        <tr>
            <td>
                <label>Select Building : </label>
            </td>
            <td>
                <select id="building">
                    <option value="">Please Select</option>
                    <?php
                        $query = "select * from build_list";
                        $result = mysql_query($query) or die('Error'.mysql_error());

                        while( $row = mysql_fetch_assoc($result) )
                        {
                            echo '<option value="'. $row['building'] .'">' . ucwords($row['building']) . '</option>';
                        }
                    ?>
                </select>
            </td>
            <td>
                <label>Select Level : </label>
            </td>
            <td>
                <select id="level">
                        <option value="">Please Select</option>
                </select>
            </td>
        </tr>   
    </table></center>
</form>
</body>
</html>

level.php :

<?php
//connect to the database
$con = mysql_connect("localhost","root","12345") or die("error ".mysql_error());

//connect to the trav table
mysql_select_db("trav",$con) or die("error ".mysql_error());

$building = mysql_real_escape_string($_GET['parent']);

$query = "select * from ";
$query = $query . $building;
$query = $query . ";";
$result = mysql_query($query) or die('Error in Child Table!');

echo '<option value="">Please Select</option>';
while( $row = mysql_fetch_assoc($result) )
{
    echo '<option value="'. $row['lvl'] .'">'.$row['lvl'].'</option>';
}
?>

Note the above codes have been used from the blog of jogesh. The link has been mentioned above.

Hope it helps other php programmers like me who are new to php but like to try our different things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM