0
votes

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.cloned
I noticed that there is a syntax error in your second button.Daan

2 Answers

0
votes

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.

0
votes

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.