1

i have here code in populating my drop down list using data from the database. It works fine but i got stuck on getting user's selected item and put it in a variable, can anyone help? here's the code:

<?php   
// declare database connection variables.
$host = "localhost";
$username = "root";
$password = "";
$db_name = "sample";
$tbl_name = "tbl_company";

// connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT type FROM $tbl_name";
$result = mysql_query($sql) or die(mysql_error());

$dropdown = "<select name='items' class='select'>";

while ($row = mysql_fetch_assoc($result)) {
    $dropdown .= "\r\n<option value='{$row['type']}'>{$row['type']}</option>";
}
$dropdown .= "\r\n</select>";

echo $dropdown; 
?>
user249563
  • 13
  • 5

3 Answers3

0

I assume you are presenting the drop-down list as an HTML form? If so then I'm going to assume you are using a "select" element, in which case you need to give the element a "name" (HTML attribute) so that when you submit the form (using HTTP POST, presumably) your PHP code can look in the POST data for a variable of that same name, containing that selected value. Hope this isn't too off the mark!

  • If the select element is a multi-select then the HTTP Post data will return an ARRAY for that element, not a simple type, therefore you would need to iterate over the $items and retrieve each index's value. The other thing you should check is whether your form submission really is using POST; it could be using GET. – user3360243 Feb 27 '14 at 12:56
0

I am not sure if I understand what you are looking for exactly but from what I get you need to give your select item a name that can be used in in a HTTP POST. Then if you check to see if POST has been actioned you can set a variable to be equal to this and then display it later on.

For Example:

if(isset($_POST){
$variable = $_POST['name'];}

echo $variable;

This is probably in the most simple form or maybe I miss understood what you needed but hope this helps in some way.

Ella_AC
  • 1
  • 1
0

As per your above comments, you seem to be doing it wrong, change your code to:

<?php   
// declare database connection variables.
$host = "localhost";
$username = "root";
$password = "";
$db_name = "sample";
$tbl_name = "tbl_company";

// connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT type FROM $tbl_name";
$result = mysql_query($sql) or die(mysql_error());

$dropdown = "<form action='' method='post'>"; //form tag added here,  
                                             //this is absolutely needed

$dropdown .= "<select name='items' class='select'>";

while ($row = mysql_fetch_assoc($result)) {
    $dropdown .= "\r\n<option value='{$row['type']}'>{$row['type']}</option>";
}
$dropdown .= "\r\n</select>";
$dropdown .= "<input type='submit' name='submit' value='Submit'>";
$dropdown .= "</form>"; //closing the form tag
echo $dropdown; 

//checking if user has submitted the form
if(isset($_POST['submit']))
{
  $var = $_POST['items'];
  echo $var;
}
?>

Edited:

As per the OP's below comment:

$dropdown = "<form action='' method='post'>"; //form tag added here,  
                                             //this is absolutely needed

while ($row = mysql_fetch_assoc($result)) {
    $dropdown .= "\r\n<input type='checkbox' name='items[]' value='{$row['type']}'>{$row['type']}";
}
$dropdown .= "<input type='submit' name='submit' value='Submit'>";
$dropdown .= "</form>"; //closing the form tag
echo $dropdown; 

//checking if user has submitted the form
if(isset($_POST['submit']))
{
  //$_POST['items'] is now a multi dimensional array
  //because we named our checkboxes as items[]
  //if you don't understand how, learn about input array
  //it's a little long to be explained here
  //therefore, you need the foreach statement to retrieve its values
  foreach($_POST['items'] as $var){
   echo $var; //prints all the checked values
  }
}
AyB
  • 11,609
  • 4
  • 32
  • 47
  • whoa ! thanks a lot ! it's working now :))) Godbless :) – user249563 Feb 27 '14 at 13:31
  • hi again :) i changed drop down list into check box, what i did was i changed the – user249563 Feb 27 '14 at 19:16
  • @user249563 Please check my updated answer for your new requirement – AyB Feb 28 '14 at 09:00
  • thank you again @I Can Has Cheezburger :)) sorry for late reply . – user249563 Mar 01 '14 at 02:11
  • i tried to alter my table and use $var to add column to it, but it didn't work :( is it possible to use array on it ? – user249563 Mar 01 '14 at 12:19
  • i already solved my problem with my table :) but one last thing, how could i retain the checked item here ? – user249563 Mar 01 '14 at 12:44
  • @user249563 Sorry, I didn't get what you meant, how about you make a new question with the code and your problem? – AyB Mar 01 '14 at 16:20
  • [stackoverflow.com/questions/22116211/displaying-data-fields-as-checkboxes-retain-checked-value-and-set-value-as-1-wh](http://stackoverflow.com/questions/22116211/displaying-data-fields-as-checkboxes-retain-checked-value-and-set-value-as-1-wh) here's the link of my new question – user249563 Mar 01 '14 at 16:27