-2

I want to submit a form with js function, and in the php action page I want to check if the form is submitted or not, here is my code:

<form method="post" action="index.php" name="form1">

        <fieldset>

        ...

        </p><div class="clearfix"></div>
        <input type="button" id="send1" onclick="validateForm1();" class="comment_submit" value="SUBMIT" name="send1"><p></p>

        </fieldset>

        </form>

and this is the js:

function validateForm1(){

                        var name = document.getElementById('fullName');
                        ...

                        if(isNotEmpty(name))
                           ...
                             document.forms['form1'].submit();
                    }

so how I can check if the form is submitted or not, without using if(!empty($_POST)) because I have 2 forms will be submitted to the same action page.

Mohammad
  • 3,449
  • 6
  • 48
  • 75

2 Answers2

2

Check whether your fields are filled.

if(!empty($_POST["field1_from_form1"]) && !empty($_POST["field2_from_form1"]) /* && ... */) {
    // Form 1 is filled.
}

And for the other form...

if(!empty($_POST["field1_from_form2"]) && !empty($_POST["field2_from_form2"]) /* && ... */) {}

You can also set a hidden input into each form...

<input type="hidden" name="formID" value="1" /> <!-- Form 1 -->
<input type="hidden" name="formID" value="2" /> <!-- Form 2 -->

... and check it:

if(isset($_POST["formID"]) && $_POST["formID"] == 1) // ...
if(isset($_POST["formID"]) && $_POST["formID"] == 2) // ...

For your next questions on Stack Overflow, please use the site's search feature to avoid duplicating already-answered questions.

John WH Smith
  • 2,743
  • 1
  • 21
  • 31
  • Its not cleaned solution, it needs lot of unnecesarry conditions. – Kamil Aug 10 '14 at 12:46
  • There *are* necessary to distinguish both forms. I suggest you read about GET and POST requests in the HTTP protocol standard. Plus, the alternative I give at the end of my answer only requires one test per condition. – John WH Smith Aug 10 '14 at 12:47
  • The solution that i'm provide below is simplest and better.. and will do same as yours... – Kamil Aug 10 '14 at 12:49
  • 1
    Stack Overflow is not a competition. Every one is free to provide an answer, which will be evaluated by the community :) – John WH Smith Aug 10 '14 at 12:50
  • Yes it's not competition. but i think that the answer must be useful and economic and simple as much as posible, but when you mark a good and simple answers as incorrect so people which are beginners will learn very bad coding ways.... – Kamil Aug 10 '14 at 13:07
-2
if($_SERVER['REQUEST_METHOD'] === 'POST') {
    if($_POST['myHiddenInput'] === 'form1'){
        //process form 1
    }
    else if($_POST['myHiddenInput'] === 'form2'){
       //process form2
    }
}

And add some hidden input into form to recognition which one form you posted.

Kamil
  • 990
  • 1
  • 7
  • 24
  • He can validate it after check if form is send... so i don't understand your stupid downvote... He asking how can check if form is posted without checking submit form by submit button. So he can check it like this, and validate it with some hidden input... – Kamil Aug 10 '14 at 12:39
  • 2
    *He can validate it after* : that's precisely what he is asking for, and what you fail to answer here. The request method will *always* be POST as long as you send at least one form. Downvotes are nothing stupid, this is how our community evaluates its contents :) – John WH Smith Aug 10 '14 at 12:44
  • The request method is always post, but REQUEST method will never return POST, when you dont send a POST request, so it's a valid solution from ZEND framework dude. From ZEND request class. And i update my answer how to do it.. inside. – Kamil Aug 10 '14 at 12:51
  • John it's insignificantly if someone use ZF or not, i just tell you that is a valid check if form is send as POST that's all. – Kamil Aug 10 '14 at 12:55
  • this isn't what he is asking for. – Krupal Shah Aug 10 '14 at 12:55
  • 1
    @krupalshah He asking how to check if form is posted ( without using submit button ) and how to check which oone form is posted, try my code and the code will do it right.... – Kamil Aug 10 '14 at 12:58
  • @Kamil ok removed the downvote. – Krupal Shah Aug 10 '14 at 13:03