Keeping selected value in drop down<

liunx

Guest
Hi I'm trying to get a drop down box to retain the value selected after it has been submitted. At the moment as soon as I click submit rhe drop down reverts to
print("<option selected> </option>");
Any ideas?

BobNZ

<tr>
<td><select style='width:100px' name="TblToEdit" size="1" >
<?php
$connection = mysql_connect("localhost", "userid", "pword");

mysql_select_db("HelpData", $connection);
$STblquery= "SHOW tables from HelpData ";
$result = mysql_query($STblquery, $connection) ;
print("<option selected> </option>");
while ($row = mysql_fetch_row($result))

{

print("<option > $row[0]</option>");

}
?>
</select></td>

<td><input type = "submit" name "submit" value = "Submit:" class="button" ></td>
<td> </td>
</tr>Well you don't give the options any values..

echo "<option value=\"".$row[0]."\">".$row[0]."</option>";

would take care of that, if the string in $row[0] is of appropriate format, of course. Without assigning values to the options, you won't have much luck trying to process the data sent by the form either.
Oh, and if my interpretation of your question is right, then this belongs in the HTML forum.Thanks but that doesn't do it. I want the user to select a value click on the apprpriate submit button and still be able to see the value he selected. At the moment as soon as the user clicks submit the value disappears. I changed the code using your example but that just shows the first index value all the time $row[0].

while ($row = mysql_fetch_row($result))

{

echo "<option value=\"".$row[0]."\">".$row[0]."</option>";

}
?>

BobNZ

PS I'm sure the post will be moved if it's in the wrong forum.you need to check which one they selected after they hit submit.

not sure what you are doing after you push submit. are you just loading the same page?Ah OK the first submit passes a value to another form on the same page that has a query that uses that value to populate the second drop down, the user also inputs a parameter into a text box. The value/variable from the second drop down and the text box value/variable get posted to another page.

Also I need to pass the first value/variable to the other page after it has been used in the second form At the moment I am storing it in a textbox on the second form, so that it gets passed with the other two values/variables. But I can't figure out how to make the text box hidden.

Hope this isn't to confusing.

BobNZ=:Othen you do this

while ($row = mysql_fetch_row($result))

{
if($row[0] == $_GET['TblToEdit']){
echo "<option value=\"".$row[0]."\" selected=\"selected\">".$row[0]."</option>";

} else {
echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
}
}
?>

to hide a text box you need to change it to a hidden element

<input type=\"hidden\" value=\"\" name=\"\">

that is the only way to hide it. you need to put in the value and name of that hidden elementHi Scoutt, the hidden stuff worked a treat :) thanks.

I can't get your code to work, can see what your trying to do but the drop down reverts to the $row[0] as soon as the user clicks on submit. I have attached a larger chunk of code if that helps.

BobNZoh sorry, change $_GET to $_POST. not sure what I was thinking. :)

if($row[0] == $_POST['TblToEdit']){

so actually to fit your code change it to this

if($row[0] == $TblToEdit){I'm definitely confused here lol.
Once again tried the code you suggested and still get the dropdown reverting to what ever is in $row[0] once submit is clicked.
Have uploaded screenshot might be easier to understand than my explanations.

BobNZyou had a few things wrong so check it out and see if it works for ya.Yea that did it. Thanks Scoutt, how is it a variable can be used/called before it is created?

if($row[0] == $_POST['TblToEdit'])

isn't this variable created after the user clicks submit?

$_POST['TblToEdit'])

BobNZyes that is true, but if it is not created then it will be "" zero

also you were using mysql_fetch_row which will only receive 1 row, I exchanged it to be mysql_fetch_array so it fetches all rowsOk clever stuff thanks Scoutt.

BobNZ
 
Back
Top