I am brand new to PHP and have run into an issue with my buttons in a table. When the button is clicked, it needs to get an ID number that is pulled from a database. I have no trouble getting the data and passing it to the next page using...
<form action="ThisPage.php" method="post">
<td><input name="Submit" type="submit" value=<?php echo $OrderNumber; ?> ></td>
</form>
Everything works perfectly, except that the $OrderNumber variable shows up on the button itself. I would like the button to display as "Edit", not the $OrderNumber variable that is showing in the attached image.
How could I make the button say "Edit", but apply a posted value of the $OrderNumber value? I have been unable to find a way to accomplish the passing of correct and unique data while also displaying a button that does not confuse the end user.
To get to the next page, I am checking for a posted value at the beginning of the PHP script. If there is a posted value, in this case, $OrderNumber, then the script carries the variables to the next page as shown...
<?php
if(!empty ($_POST['Submit']) )
{
$_SESSION['OrderID']=$_POST['Submit'];
header("location:NextPage.php");
exit;
}
?>
But if there is no posted data, the page runs and shows the rows representing the array of database entries. All is well except for this button label.
Try This
<form action="ThisPage.php" method="post">
<input type="hidden" name="order_id" value="<?php echo $OrderNumber; ?>" />
<td><input name="Submit" type="submit" value="Edit" /> </td>
</form>
<?php
if(!empty ($_POST['Submit']) )
{
$_SESSION['OrderID']=$_POST['order_id'];
header("location:NextPage.php");
exit;
}
?>
add hidden field because we can pass any value in form without display we have to add this method.
Try this:
<?php
session_start();
if(!empty ($_POST['Submit']) )
{
$_SESSION['OrderID'] = $_POST['Submit'];
header("location:NextPage.php");
}
?>
The best way is to use a second element. Your code would look like this:
<form action="ThisPage.php" method="post">
<td>
<input type="hidden" name="ordernumber" value="<? =$OrderNumber; ?>" />
<input name="Submit" type="submit" value="Edit"/>
</td>
</form>
On the next page, you can now use $_POST['ordernumber']
. To use sessions, make sure session_start(); is placed before retreiving or storing and make sure nothing has been printed to the screen, nor any headers have been output before issuing a session_start();
While it was short on explanation, the answer by Bhavin Sasapra worked perfectly.
The answer provided by LPChip was very close, but my script wouldn't run with;
<input type="hidden" name="ordernumber" value="<? =$OrderNumber; ?>" />
I had to use
value="<?php echo $OrderNumber; ?>" />
I am using PHP 5, so that may be the issue. I will be sure to specify should I have another question.
And it turns out that you can open a form inside of a table.