0

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.

Buttons with $OrderNumber showing

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.

8
  • you need to start the session with session_start() before you are able to store/retrieve data in/from the $_SESSION superglobal Commented Jan 18, 2017 at 10:25
  • Your HTML is invalid. You can't wrap <td> in a <form>. Commented Jan 18, 2017 at 10:27
  • Use a button instead of input. Commented Jan 18, 2017 at 10:28
  • 1
    value="<?php echo $OrderNumber; ?>" Commented Jan 18, 2017 at 10:29
  • @putvande why we cannot use <td> in <form>? Commented Jan 18, 2017 at 10:31

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Why should anyone "try this"? What did you change? Why was it changed? Why was that a necessary change?
i add hidden filed @Qirel and yes it is necessary change
My point is that you should always add an explanation in your answer what you changed and why. The answer by @LPChip has this, which makes it a better answer.
1

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();

Comments

0

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.

Comments

-1

Try this:

<?php
session_start();
if(!empty ($_POST['Submit']) )
{
    $_SESSION['OrderID'] = $_POST['Submit'];
    header("location:NextPage.php");
}    
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.