Hello to everyone, im having trouble getting the value of a combobox in HTML to php! Frist I import data from a mysql collum into the combobox, being this the value of the combobox! I want to be able to get the selected value when I submit my form. I tried using sessions but i always get the data of the last row inserted.
Heres the code that I have:

    <?php
    include "connect_database.php";
    $query = mysql_query("SELECT id_expense, category_name FROM expenses") or die(mysql_error());
    ?> 

    ......


    <select name="expenses" >
      <option value="0" selected="selected"> Choose</option>
         <?php
         while($row = mysql_fetch_assoc($query)){
            // $id=$row["id_expense"];  
            $category = $row["category_name"];
        ?>
      <option value= <?php $category ?> > <?php echo  $category ?> </option>
        <?php
          }?>
          </select>

How can i do this?

Recommended Answers

All 7 Replies

You should have defined action attribute in the <form> tag which is the name of the script that will process the data. The script should go something like this:

<?php
// if submited the value will be in the $_POST['expenses'] element of the $_POST array
// (provided that the method is POST, otherwise change it to $_GET).
// so check for the existence of $_POST['expenses'] (or $_POST['submit']) and use the value
if(isset($_POST['expenses'])) {

    $expenses = $_POST['expenses'];
}
?>

Hello thankyou for replying! I implemented your code but nothing apears! :( It is because of using databases? For each data on the collum Im adding a option to the combobox! Maybe it is the loop?

Post the whole script, please.

My code was just an example. You should adapt it to your particular script.

Hello this is from page1.php and i need to sent the option that i select form the combobox to insert_expenses.php

_______page1.php____________

<?php
//session_start();
include "connect_database.php";
$query = mysql_query("SELECT id_expense, category_name FROM expenses") or die(mysql_error());

?> 

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

</script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
  <script>

  $(document).ready(function() {
    $("#date_expense_id").datepicker({ dateFormat:
        'yy/mm/dd', changeMonth: true,
        changeYear: true, showAnim: '',
        showTime: true, duration: ''});

 });

 </script>
</head>

<body>
<form action="insert_expenses.php" method="post"> 
<table width="265" border="1">
  <tr>
    <td width="58">Date</td>
    <td width="144"><label for="textfield"></label>
      <input type="text" name="date" id="date_expense_id"></td>
    <td width="45" rowspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td>Merchant</td>
    <td><label for="textfield2"></label>
      <input type="text" name="merchant" id="textfield2"></td>
    </tr>
  <tr>
    <td>Category</td>
    <td><label for="textfield3"></label>
      <input type="text" name="category" id="textfield3"></td>
    <td>

      <select name="expenses" >
          <option value="0" selected="selected"> Choose</option>
          <?php
             while($row = mysql_fetch_assoc($query)){
                //$id=$row["id_expense"];  
                $category = $row["category_name"];
            ?>
                <option value= <?php $category ?> > <?php echo  $category ?> </option>
            <?php
          }?>
      </select>

      </td>
  </tr>
  <tr>
    <td>Amount</td>
    <td><label for="textfield4"></label>
      <input type="text" name="amount" id="textfield4"></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3">
      <input type="submit" name="button" id="button" value="Submit">
    </td>
    </tr>
</table>
</form>
</body>
</html>

______________________

_______insert_expenses.php_________

<?php
//session_start();
include "connect_database.php";

//$get_id = mysql_query("SELECT id_user FROM data_users");
//$got_id = mysql_fetch_array($get_id, MYSQL_ASSOC);

//$category_field = $_POST['category'];
//$category_chosen = $_POST['$options'];

$expenses = $_POST['expenses'];

if(isset($expenses)){
    echo $expenses;

}
else{
    echo "n";   
}

if(mysql_affected_rows() == 1){
    echo "S";   
}
else{
    echo "N".mysql_error();
}
?>

Sory to come back so late, I was away. The trouble was that the select element (dropdown) was not constructed correctly. Correct code on line 58 is:

<option value="<?php echo $category ?>"> <?php echo  $category ?> </option>

You missed double quotes for the value attribute and forgot to echo the category.

Hello no problem, thank you, its working fine now, it was a simple thing :) Thank you once again for helping :)

You are welcome. Please mark as solved. Happy coding :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.