0

How do I get the value in the combo box and store it in a variable? And use the variable in another PHP file? Here is my code.

Note: I am already able to get data from database and store it in the combo box.

$con = mysql_connect("localhost","root","aaaa");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("maptemp", $con);


$sql = "SELECT * FROM users";
$rs = mysql_query($sql) or die(mysql_error());
$selectbox='<select name=\'userst\'>';

while ($row = mysql_fetch_assoc($rs)) {
    $selectbox.='<option value=\"' . $row['username'] . '\">' . $row['username'].'</option>';
}

$selectbox.='</select>';
mysql_free_result($rs);
echo $selectbox;
user2151530
  • 9
  • 1
  • 1
  • 3
  • A combobox is a UI control that is a combination (hence the name) of a drop down menu (which is what you get with a select in HTML) and a text input (an input of type text). HTML doesn't have any native controls that are represented as comboboxes. If you have one then it is constructed with a pile of JavaScript and the answer depends on that JavaScript. Alternatively, you might have meant "a select element". Which is it? (It looks like a plain select element, but there might be some JS you aren't sharing) – Quentin Mar 09 '13 at 13:17
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Mar 09 '13 at 13:17

1 Answers1

0

How do I get the value in the combo box and store it in a variable?

Assuming you are talking about a plain select element.

  1. Submit the form it is in
  2. Read $_POST['foo'] or $_GET['foo'] where foo is the name of the select element

And use the variable in another PHP file?

  • include that file
  • Store the data in a session
  • Redirect to that file while passing the data in the query string
  • various other options depending on precisely what you want to acheive
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335