PHP Check which form was submitted
143
I am working on a website where I have 2 forms on 1 page. I use 1 PHP script to check the forms. But if I submit my second form on the page, my website submits the first form. How can I check which form is submitted?
<!--// Form 1-->
<form method="post">
<input type="text" name="test">
<input type="submit" name="submit">
<input type="hidden" name="page_form" value="1">
</form>
<!--// Form 2-->
<form method="post">
<input type="text" name="test">
<input type="submit" name="submit">
<input type="hidden" name="page_form" value="2">
</form>
PHP:
if(isset($_POST['submit'])) {
$forms = array(1, 2);
foreach($forms as $form) {
if($_POST['page_form'] == $form) {
// All my form validations which are for every form the same.
}
}
}
Answer
Solution:
What to do it this way:
And check which form was submitted:
Answer
Solution:
I just tested this and i have no problems what so ever:
index.html
submit.php
trying submit with form 1 and data aaa result:
trying submit with form 2 and data bbb result:
So i cant see what it is that is not working
Answer
Solution:
I don't understand what your question is.
Though a bit convoluted, the code you show should work fine.
If it's a simpler variant you're looking for, I would rather do it that way:
EDIT:
try this out. I would be surprised if it did not work.
Now if you don't want a special handling for different forms, simply do this:
Answer
Solution: