2 Drop down lists

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
rapid3642
Posts: 45
Joined: Thu Nov 26, 2009 9:38 pm

2 Drop down lists

Post by rapid3642 »

Hey everyone i'm really sorry i'm posting so many help topics but they all pertain to each other so this is why i haven't resolved the other ones yet. The thing is i have 2 drop down selections and i want the first one to retrieve information from my mysql table and then based on the first selection the second one will appear, right now it just shows all of the categorys and all of the subcategorys, i only want the subcategories to display according to the category selected.

Here is the code

Code: Select all

<?php
include 'Connect.php';


$result = mysql_query( "SELECT id,name FROM category" )
or die("SELECT Error: ".mysql_error());
$options=""; 
while ($row=mysql_fetch_array($result)) {

$firstvalue=$row["name"];
$id=$row["id"];

$options.="<OPTION VALUE=\"$firstvalue\">".$firstvalue.'</option>';

}
?>

<?php


$results = mysql_query( "SELECT id,name FROM subcategory" )
or die("SELECT Error: ".mysql_error());
$optionss="";
while ($rows=mysql_fetch_array($results)) {

$secondvalue=$rows["name"];
$options1.="<OPTION VALUE=\"$secondvalue\">".$secondvalue.'</option>';

}
?>




<SELECT NAME=p>
<OPTION VALUE=0>
<?php echo $options?>
</SELECT>
<SELECT NAME=q>
<OPTION VALUE=0>
<?php echo $options1?>
</SELECT>

what i want it to do is when a level has been selected it will grab the id on the table and then it will query the subcategory where pid=id but unfortunately i tried putting it to where it will say pid=$id but it doesn't work because when setting the id it is in the while statement then the value is lost, and also even if it did work you would have to refresh the page right? or is it possible to make it to where you dont have to refresh and keeping it all php?

Thank you in advance and i'm so close to having the other posts resolved!
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: 2 Drop down lists

Post by Chris »

Code: Select all

<?php
include 'Connect.php';
?>

<select name="p" id="p" onchange="location.href = <?php echo $_SERVER['PHP_SELF']; ?>?p=document.getElementById('p').value()">
    <option value="0"></option>
    <?php
    $result = mysql_query("SELECT `id`,`name` FROM `category`");
    while($row = mysql_fetch_array($result))
    {
        echo "<option value=\"{$row['id']}\">" . $row["name"] . "</option>\n";
    }
    ?>
</select>

<?php
if( !empty($_GET['p']) ) :
?>
    <select name="q">
        <?php
        $result = mysql_query("SELECT `id`,`name` FROM `subcategory` WHERE `id` = '". (int)$_GET['q'] ."'");
        while($row = mysql_fetch_array($result))
        {
            echo "<option>" . $row["name"] . '</option>';
            
        }
        ?>
    </select>
<?php
endif;
?>
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Advanced Help and Support”