Jump to content

Redirecting if $_POST contains no data


JCS1988

Recommended Posts

I am using an HTML form that posts the data to confirm.php which I created. There the users sees a confirmation screen and the data is mailed out to my email address. All of this works great, except for when the user navigates directly to the confirm.php, skipping over the form page. The form is mailed out with all fields blank. I did get a solution to this problem awhile ago but it no longer works. I'm guessing the newer PHP doesn't support it.

 

What I want is for PHP to check and see if data exists. If no data is present the user should be redirected to index.html, if there is data they will see the confirmation and the email will be sent to me. The first few lines are what originally would check for data and if none was present they would be directed to index.html. I just need something that will provide the same function as this one did before. Thanks!

 

 

<?php
if (sizeof($_POST)==0)
{
header("Location:index.html");
}
else
{
$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

// Checkbox handling
$field_14_opts = $_POST['field_14'][0].",". $_POST['field_14'][1].",". $_POST['field_14'][2];

mail("jessesimmers@gmail.com","Website Form","Form data:

First Name: " . $_POST['field_1'] . " 
Last Name: " . $_POST['field_2'] . " 
Address: " . $_POST['field_3'] . " 
City: " . $_POST['field_4'] . " 
State: " . $_POST['field_5'] . " 
Zip Code: " . $_POST['field_6'] . " 
Home Phone: " . $_POST['field_7'] . " 
Cell Phone: " . $_POST['field_8'] . " 
Email: " . $_POST['field_9'] . " 
Best Time To Reach You: " . $_POST['field_10'] . " 
Best Day To Reach You: " . $_POST['field_11'] . " 
Number of Windows: " . $_POST['field_12'] . " 
I Am Interested In: $field_14_opts

");

?>

Link to comment
Share on other sites

Technically if they submit a form $_POST will always have at least the submit value set.

 

Because it is set like an array you could try something like:

 

<?php
if(isset($_POST) && count($_POST) > 1) {
  //do stuff
  // form was submitted
} else {
//do something else
// form submitted but no values were set
}
?>

 

So the $_POST variable has to have at least 2 or more values.

Link to comment
Share on other sites

if (isset($_POST))
{
   header("Location:index.html");
}
else

 

By the way, $_POST is *always* set. Unless someone unsets it. :D

 

 

Is that right? You learn something new everyday..

 

I normally don't use POST as a way to check, I normally check for a SESSION to exist.

 

But for POST, if he wants to double double make sure, he can include a hidden type say: myform

and set the value to true when they hit submit.

 

Then on the confirm page, he can check to see if $_POST['myform'] == true

 

But that's kind of  a hack way to do things...

 

He should really invest in sessions....

 

Link to comment
Share on other sites

Hey,

 

from your example, id just check to make sure the submit button has been pressed, so if you have a submit button with the name of "go", do this:

 

if(isset($_POST['go']))

{

    // They have pressed submit.

} else {

    // Somebody is cheating...

    header("Location: index.html");

    exit(); // for that warm feel good security feeling.

}

 

Thanks,

Justin

 

Link to comment
Share on other sites

Hey,

 

from your example, id just check to make sure the submit button has been pressed, so if you have a submit button with the name of "go", do this:

 

if(isset($_POST['go']))

{

    // They have pressed submit.

} else {

    // Somebody is cheating...

    header("Location: index.html");

    exit(); // for that warm feel good security feeling.

}

 

Thanks,

Justin

 

 

Baaaad idea. Some browsers wont send the submit button unless it's actually been clicked. So all those people who press enter to submit the form will be confused.

 

And ken is right - $_POST is always set. Just check it's size with count.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.