Checking If Form Has Been Submitted - PHP

Checking if form has been submitted - PHP

For general check if there was a POST action use:

if ($_POST)

EDIT: As stated in the comments, this method won't work for in some cases (e.g. with check boxes and button without a name). You really should use:

if ($_SERVER['REQUEST_METHOD'] == 'POST')

How to check if form was submitted

One solution is to add a hidden input on both forms:

<input type="hidden" name="form1" value="name" />

and:

<input type="hidden" name="form2" value="pass" />

Then to check which one has been submitted:

if(isset($_POST['ok']) && isset($_POST['form1'])){

// For the first form

}

And:

if(isset($_POST['ok']) && isset($_POST['form2'])){

// For the second form

}

Best way to check if form was submitted (php)

While your code would achieve the same result in most cases, it is not the same as the isset call.

What your code does is checks if the REQUEST_METHOD is POST. That is, it checks if the user made a POST request to access the current page.

What the isset does is checks if something with the name of Submit was sent via POST. This usually happens when your submit button is <input name="Submit" type="submit" value="Submit" />, as clicking that (or hitting enter in a text field and it's the first submit button) will result in $_POST['Submit'] being set.

To see the different behaviours, compare the results of curl -X POST your-url.com/page.php with curl -F Submit=submit your-url.com/page.php.

Check if form submitted and do something

<input type="submit" value="Register" name "register"/>
^

You're missing an = there.

It should be:

<input type="submit" value="Register" name="register"/>

It's betterto use isset() instead of empty(), in this case:

if(isset($_POST['register']))
{
echo "Option A <br>";
}
else
{
echo "Option B <br>";
}

I'm not sure what Option A and B are, but with this code, you'll always see Option B when you load the page. The condition in your if block will execute to FALSE and hence the else block will executed. If you want to avoid this, you can use an elseif statement instead of an else.

how to check if form is submitted without submit button?

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.

Check if form is submitted

When you submit the form. A POST request is sent to the server.

You can avoid this by redirecting the page once you have completed the work that your script is doing with this:

header("Location: http://mypage.php");
die();

Now, the problem here is that you lose echoing data data, so you could add something to give a success message:

header("Location: http://mypage.php?success=true");
die();

Now, in your script you could have this where the output would go:

<?php
if ( isset( $_GET['success'] && $_GET['success'] == 'true' ) ) {
echo 'Your form has been submitted!';
}

This should avoid the trouble that you're having. There are other techniques though and you should use one that's right for you.

On a side note, POST requests are also resubmitted when you use forward and back buttons in your browser - you should be mindful of this, too.

Right method to check if user submitted a form

The method 1 and method 2 are the most appropriate ones.

If you validate the form using a $_GET['submitted'] == true this could easily be cracked by the end user which creates a security breach.

The best method i think is the first one

if($_SERVER['REQUEST_METHOD'] == 'POST')

This piece of code works good even on dynamically generated forms wherein you do not have to check a post variable.



Related Topics



Leave a reply



Submit