1

I have 2 forms on my page and 1 of them has 2 different ways to be submited, a submit button and a jQuery on click event, it looks something like this:

<script>
    $(document).ready(function() {
        $('#img_send_form').click(function() {
            $('#form2').submit();
        });
    });
</script>

<form name="form1" action="#" method="post">
    <input type="text" name="field1"/>
    <input type="submit" name="send1"/>
</form>
<form name="form2" action="#" method="post">
    <input type="text" name="field1"/>
    <input type="text" name="field2"/>
    <input type="text" name="field3"/>
    <input type="text" name="field4"/>
    <input type="text" name="field5"/>
    <input type="text" name="field6"/>
    <input type="text" name="field7"/>
    <input type="submit" name="send2"/>
</form>
<img src="xxx" id="img_send_form"/>

What is the best way to check if form2 was submmitted on php? Do I need to use isset for every form field ?

if (isset($_POST['field1'])||isset($_POST['field2'])||isset($_POST['field3'])||isset($_POST['field4'])||isset($_POST['field5'])||isset($_POST['field6'])||isset($_POST['field7']))

or is there another "better" way to do it?

1
  • add hidden field in both form and check hidden field value on server Commented Feb 25, 2014 at 8:45

4 Answers 4

2

Take Hidden Field with Same Name in Both Forms (but differ Ids if you need)

Then you will only need to check the that hidden field

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

Comments

1

just add a hidden field to the second form, and in PHP check if it's set, in this case was used the second form

Comments

1

Not necessary to take hidden fields,

PHP :

if(isset['send2'])) { echo "Form2 submitted !" ;?> }

1 Comment

it doesn't work to me when I send it using the image
0
<script>
    $(document).ready(function() {
        $('#img_send_form').click(function() {
            $('#form2').submit();
        });
    });
</script>

<form name="form1" action="#" method="post">
    <input type="text" name="field1"/>
    <input type="submit" name="send1"/>
</form>
<form name="form2" action="#" method="post">
    <input type="hidden" name="form2_send"/>
    <input type="text" name="field1"/>
    <input type="text" name="field2"/>
    <input type="text" name="field3"/>
    <input type="text" name="field4"/>
    <input type="text" name="field5"/>
    <input type="text" name="field6"/>
    <input type="text" name="field7"/>
    <input type="submit" name="send2"/>
</form>
<img src="xxx" id="img_send_form"/>

And php :

if(isset['form2_send'])) { echo "Form2 submitted !" ;?> }

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.