Jump to content

Archived

This topic is now archived and is closed to further replies.

JackJack

PHP Drop Down Menu

Recommended Posts

I wish to create something that would have the data from another page which was a drop down box/menu.

Example:
[code]
echo "Choose select a <b>motor</b> for<br> your robot:    ";

echo "<select name='part1'>";

echo "<option value='null'>Select a Motor</option>";

                    $romotor = mysql_query("select * from `parts` where `owner` = '$stat[id]' and `slot` = 'Motor'");
                    while ($partuno = mysql_fetch_array($romotor)) {


echo "

<option value='$partuno[id]'>$partuno[name] </option>";
}
echo "</select>";

echo "<br><br>Now add a <b>shell</b> to<br> your robot:    ";

echo "<select name='part2'>";

echo "<option value='null'>Select a Shell</option>";

                    $roshell = mysql_query("select * from `parts` where `owner` = '$stat[id]' and `slot` = 'Shell'");
                    while ($partdeux = mysql_fetch_array($roshell)) {


echo "

<option value='$partdeux[id]'>$partdeux[name]</option>";

}
echo "</select>";
[/code]

Next Page
[code]
        mysql_query("insert into bots (motor, shell) values('???','??????')")
[/code]


Thank You For Your Help

JJ

Share this post


Link to post
Share on other sites
You seemed to be repeating chunks of code so I put the option list build into a function. For clarity I also renamed the <SELECTS> as "motor" and "shell".

[code]
function getOptions($id, $type) {
         $str = "<OPTION value=''> - Select a $type -</OPTION>\n";
         $res = mysql_query("select id, name from `parts`
                where `owner` = '$id' and `slot` = '$type'");
         while (list($id, $name) = mtsql_fetch_row($res)) {
                $str .= "<OPTION value='$id'> $name</OPTION>\n";
         }
         return $str;
}

echo "<FORM action='nextpage.php' method='post'>\n";

echo "<p>Choose select a <b>motor</b> for<br> your robot:</p> ";
echo "<select name='motor'>";
echo getOptions($stat['id'], 'Motor');
echo  "</select>\n";

echo "<p>Now add a <b>shell</b> to<br> your robot:</p>";
echo "<select name='shell'>";
echo getOptions($stat['id'], 'Shell');
echo  "</select>\n";

echo "<p><INPUT TYPE='SUBMIT'  name='submit' value='Submit'></p>\n"
echo "</FORM>\n";[/code]


:: nextpage.php ::

[code]  $motor = $_POST['motor'];
  $shell = $_POST['shell'];

  mysql_query ("INSERT INTO bots (motor, shell) VALUES ('$motor', '$shell')");[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.