I have following code in my php page,
I’m trying to store the value of the selection list to a variable in my php page.
Can someone kindly help on this?
$state_list= <<<HTML <select name="state">
<option value="0"></option>
<option value="1">ok</option>
<option value="2">not-ok</option>
</select>
HTML;
echo "<table>";
while($row = mysql_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $row['task'] . "</td>";
echo "<td>" . $row['task_desc'] . "</td>";
echo "<td>"; echo $state_list; echo "</td>";
echo "</tr>";
}
echo "</table>";
if(isset($_GET['state'])) {
$stat=$_GET['state'];
echo $stat;
}
With your sample code, you’re storing the entire list into a variable in PHP (and it looks invalid, you’ll need to put the HTML on a second line, like this):
If you’re trying to get the actual submitted value of the select list (once the form is submitted), you can access it either through
$_GET['state'], or$_POST['state'], depending on the method your form is submitting such as:UPDATE (outputting the form?)
From your most-recent edit, it looks like you’re not putting the actual
selectlist inside of a<form></form>block (which you’ll need to do to actually submit the value). A simple example is like this:I’ve also included a
submit-button to give you a way to, well, submit the form =P.However, you’re going to encounter an issue with this method because you need a separate
select-list for every row in your results. Because of this, you’ll actually need a separate form for every row in your results (or some extra javascript code, but we’ll leave that one for a rainy day).Also, since you need a value for each individual row, you’ll need a second (hidden) field to tell which row was selected. Here’s a block of sample code that should be able to do this for you:
The problem with this method is that you’ll have a
Submitbutton on every single row. While this will work and I’ve seen it in numerous places before, it’s not really visually appealing. If this is unsuitable for your needs, you could update theselectlist to auto-submit when a value is selected. To do this, just update the<select>tag to:<select name="state" onchange="this.form.submit()">. If you do this, you could remove thesubmit-button from the output loop above.