...

View Full Version : [PHP form not waiting to be filled in before being sent]



Pamster
01-27-2012, 03:05 AM
Hi

I am currently building my first website and I have got some issues with the php on my contact form. When I click onto the contact page, it automatically skips to the 'thank-you' page without the form being filled in. So I am getting a message to my email address, but it is obviously empty. I am not sure where the problem is arising, I have tried so many different bits of code - so I am listing the code for the <form> and the code for the <php>

Hope someone can help, sorry if this a really basic request - just can not seem to work it out. Thanks


Here is the code I am using for the form

<div id="full_width_small">

<form action="New_Form.php" id="thisform" method="post" >


<p><label for="name">Name:</label><br />
<input name="name" type="text" id="name" size="40" style="background-color: #D3E0F8" value=""/ ></p>

<p><label for="email">Email:</label><br />
<input name="email" type="text" id="email" size="40" style="background-color: #D3E0F8" value="" /></p>

<p><input type="checkbox" id="remember" name="remember" />
<label for="remember"><em>Remember this info?</em></label>
</p>

</form>
</div>



<div id="full_width_small">
<form action="New_Form.php" id="thisform" method="post">


<p><label for="message">Message:</label><br />
<textarea name="message" cols="55" rows="15" id="name" style="background-color: #D3E0F8" value="" /></textarea>
</p>
</p>
<input type="submit" name="submit" value="submit" />
</form>
</div>



Here is the code I am using for the php

</head>

<?php

$EmailFrom = "me@mysite.com";
$EmailTo = "me@mysite.com";
$Subject = "Online Form";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Comments = Trim(stripslashes($_POST['Message']));

// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Comments;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=thank-you.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=something-went-wrong.php\">";
}


?>


<body>

KuriosJon
01-27-2012, 03:09 AM
Is your PHP code on the same page as the HTML for the contact form?

If so, when you load the page, that PHP code is executing. Since none of the $_POST variables are set, your email sensibly fails to send (edit: or sends you a blank one, in your case)

Do this:


<?php

if($_POST) {

// put your PHP code to process your contact form here

}

?>

Edit to explain:

When you open that page, it executes all of the PHP code on it. Since you didn't POST to the page, all of the POST variables are empty. So you get a blank email. If you use if($_POST), you're basically just checking to see if the form was submitted before you actually run that code.

Pamster
01-27-2012, 10:38 AM
Thank you very much for getting back to me so quickly.

Your code worked, thanks

However my form that has been sent to my email is blank - any ideas as to why - Thanks

jmj001
01-27-2012, 11:43 AM
yep, your input names are lowercase and your $_POST vars are capitalized...

Pamster
01-27-2012, 12:27 PM
Thanks, so much

I am almost there.

I can now see a message in the 'message' line when I get sent an email, but the 'name' and 'email' are still blank...

Thanks guys for your help, it is appricated.

Fou-Lu
01-27-2012, 01:22 PM
That's because you have 2 forms in here. The form the submit button in is the successful one, so only its fields will be successful. Combine the forms into 1.

Pamster
01-27-2012, 02:03 PM
Thanks so much

Will combine them and see if it works, I suspected that this might be the case.

Any ideas on how I can visually split my form into two bits - I want the Name and Email in one box and then the Message in a box below it - just so it looks good.

As I said I am a newbie to this, so at the moment I am working on a trial and error basis, which is time consuming - So I really appricate all the help.

Fou-Lu
01-27-2012, 02:17 PM
forms are non-visual elements, so they don't create the display themselves. Use fieldsets or divs to split them up visually.

tangoforce
01-27-2012, 04:36 PM
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}


Delete that, it's all useless. You're setting $validationOK without even checking anything is set. Therefore it will AWAYS be true which renders the next few lines of code useless.

Pamster
01-27-2012, 05:03 PM
Thank you all very much.

It works - I can now receive messages from my website.

I can not believe that I have messed around with it for several days, came on here last night and you have all helped me sort it out. BIG BIG Thanks

I will repost my code, for anyone that has similiar problems - so they can see the code working.....I just need now to work out how to make ensure the sender fills in all the fields - as at the moment, you can click send and you dont need to fill in anything and I dont want to be bombarded with empty emails.

Thanks once again :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum