HI,

I have a little problem passing the value of the drop down list from the first form to the second. I am able to display the data in the list box, then used $_POST to get the passing data in the the second form. However when I echo the $_POST data. It's empty.

Here is the code from the 1st from (firstform.php)

$strQuery = @mysql_query("SELECT DISTINCT test_id, test_name FROM questions");
if (!$strQuery){	
 exit('<p> Error retrieving data from questions table: '. mysql_error(). '</p>');}
  
echo "<p><select name=quiz value=''>Chose a test</option></p>";
while($nt=mysql_fetch_array($strQuery)){ 
echo "<option value=$nt[test_id]>$nt[test_name]</option>";}

<form action="secondform.php" method="post">
<p><input  type="submit" value="SUBMIT" /></p>

In the second.php :

$test_id=$_POST['quiz'];

when I echo $test. It's empty. What's wrong? I need the value $nt[test_id] for my next query in secondform.php.
Thank you.

Recommended Answers

All 4 Replies

<form> tag should be above <select> tag. <select> tag can't have value attribute. And always use

echo "<option value='".$nt['test_id']."'>".$nt['test_name']."</option>";

instead of

echo "<option value=$nt[test_id]>$nt[test_name]</option>";

(Thats the clean way of writing it).
P.S. Don't use @ before your queries. It will supress the errors and you wont be able to find out what's wrong!

Thank you for your input. I add a hidden field for test_id to go along with the submit. However it gave me the last test_id and not the one I selected.

<form action="secondform.php?id=$id" method="post">
<p><input type="hidden" name="id" value="<?php echo $id; ?>" />
<p><input  type="submit" value="SUBMIT" /></p>
</form>

Where are you selecting the id ? I can't understand your code. You need to post your complete code using code tags and explain your problem in detail.

After posting, I found a way to make it work. Here is the code:

if (!$_POST['formSubmit']): 
..... in here is the code to connect to the database and select the data from the table. 
...
while($row=mysql_fetch_array($strQuery, MYSQL_ASSOC))
{
if ($seld == "selected")
{$id=$row[test_id];}
else { $seld = ""; }
 $mylistbox .= "<option value='$row[test_id]'$seld>$row[test_name]</option>";

}//end while
?>
<p>
<form action="secondform.php?id=$id" method="post">
    <select name="quiz"><option value=''>Chose a test</option>
        <?php print $mylistbox;?>
    </select>
   <input  type="submit" name="formSubmit" value="SUBMIT" /></p>
</form>

I passed the $id to the next form in

<form action="secondform.php?id=$id" method="post">

Thank you for your help! :-)

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.