Jump to content

PHP Drop Down Menu


JackJack

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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.