1

I have a form with two submit buttons and both submit buttons have a different name so I can differentiate which button was clicked. The problem are the hidden values. For button1, I would like to send value=1, for button2, I would like to send value=2 in a hidden value. But when button1 was clicked, then hidden value=2 goes into the POST instead of value=1.

<form action="destination.php" method="POST">
   <input type="hidden" name="car" value="1">
   <button type="submit" name="button1" value="button1">Button 1</button>

   <input type="hidden" name="car" value="2">
   <button type="submit" name="button2" value="button 2>Button 2</button>
</form>

How can I send hidden value=1 when button1 was clicked and hidden value=2 when button2 was clicked? Do I have to create a form for each submit button and the associated hidden values or is there a different way to pass hidden values with only one form?

2
  • You add an event handler to the button click and set the value of your hidden input based on whatever condition you need. Commented May 22, 2020 at 8:33
  • I noticed that there is a syntax error in your second button. Commented May 22, 2020 at 8:54

2 Answers 2

0

You can simply name your inputs differently like car1 and car2 and then listen for each. or even set a different ID to each input and listen for the ID.

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

Comments

0

On destination.php you could do:

if (isset($_POST['button1']) {
   $car = 1;
} else if (isset($_POST['button2']) {
   $car = 2;
}

And then you could get rid of the hidden input elements.

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.