Reputation: 95
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;
}
Upvotes: 1
Views: 16789
Reputation: 38436
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):
$state_list= <<<HTML
<select name="state"> <option value="0"></option> <option value="1">ok</option> <option value="2">not-ok</option> </select>
HTML;
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:
$state = $_POST['state'];
UPDATE (outputting the form?)
From your most-recent edit, it looks like you're not putting the actual select
list inside of a <form></form>
block (which you'll need to do to actually submit the value). A simple example is like this:
echo '<form method="get">';
echo $state_list;
echo '<input type="submit" value="Submit Form" />';
echo '</form>';
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:
while($row = mysql_fetch_array($result1)) {
echo "<tr>";
echo "<td>" . $row['task'] . "</td>";
echo "<td>" . $row['task_desc'] . "</td>";
echo "<td>";
echo '<form method="get"><input type="hidden" name="task" value="' . $row['task'] . '" />';
echo $state_list;
echo '<input type="submit" value="Submit" /></form>';
echo "</td>";
echo "</tr>";
}
The problem with this method is that you'll have a Submit
button 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 the select
list 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 the submit
-button from the output loop above.
Upvotes: 1
Reputation: 10712
PHP is a server side language, you have to submit the form and then retrieve the data via POST or GET - something like this..
<form method="GET">
<select name="state">
<option value="0"></option>
<option value="1">ok</option>
<option value="2">not-ok</option>
</select>
<input type="submit" value="Submit">
</form>
and then to get the data back you use the relevant $_GET['state']; or $_POST['state'];
<?php echo $_GET['state']; ?>
It is also common practice to check that the GET or POST variable exists before using it, much like this.
<?php
if(isset($_GET['state'])){
echo $_GET['state'];
}
?>
Upvotes: 1