PDA

View Full Version : PHP Contact form not working



60Fairlane
August 3rd, 2005, 07:22 PM
This basic contact form on my web page is not working. This seems pretty basic, but for some reason it's not sending the form. Any Help/Suggestions would be greatly appreciated - TIA.

Regards,
Brian

Here is the form code:

<form method="post" action="contactme.php" >
<p align="center">
<b><font size="2" face="Verdana">Name: <input type="text" name="name" size="30"/>
<br>
<br>
Phone: <input type="text" name="phone" size="30">
<br>
<br>
Email: <input type="text" name="email" size="30">
<br>
<br>
Message: <textarea name="message" cols="30" rows="5"> </textarea>
<br>
<br>
<input type="submit" value="Send Form"> </font></p>
</form>

and the PHP form code:

<?PHP
if(isset($_POST['submit'])) {
$to = "brian@thedentistschoice.com";

$re = "Feedbackfromcontactpage";

$name_field = $_POST['name'];
$phone_field = $_POST['phone'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Message: $message\n Phone: $phone_field\n";
echo "Data has been submitted to $to!";

mail($to,$re,$body);
} else {

echo "Thank you for submitting your contact information. We will contact you as soon as possible.";

}
?>

bwh2
August 3rd, 2005, 08:12 PM
are you getting any error messages? what is the output on the page after you submit?

i would echo the variables at the end of your if statement to make sure the variables are being sent correctly.

Ankou
August 4th, 2005, 01:36 AM
isset($_POST['submit']) -- You're going to need to pass 'submit' in there somewhere. Currently you're not doing that.


<input type="submit" name="submit" value="Send Form">

That may work for you. Otherwise just put in a hidden field with submit set to something....


<input type="hidden" name="submit" value="ok">


Just some advice here...

You're outputting to the user that the information was sent even before you try to mail it. You may want to build the email and then check to see if it was sent before you say it was sent. If it's not sent then you can at least let the user know. Also if the info was submitted you send the data and then your else statement (if submit is not set) tells the user that you received their contact info - which doesn't seem to be the case.

Going off your code, something like:


<?php
if(isset($_POST['submit'])) {
$to = "brian@thedentistschoice.com";
$re = "Feedbackfromcontactpage";

$name_field = $_POST['name'];
$phone_field = $_POST['phone'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Message: $message\n Phone: $phone_field\n";

if(mail($to,$re,$body)){ echo "Data has been submitted to $to!"; }
else{ echo "Error in sending data. Please contact .... "; }

} else {

echo "There was an error in obtaining your information. Data was not sent.";

}
?>


BTW - I didn't test the code so some adjustments may be necessary.

60Fairlane
August 4th, 2005, 11:32 AM
Ankou,

Thank you! It works now :thumb2: - you were right! The only adjustment that I had to make was to insert a { in front of "else" on line 13 (came up with an error message first time).

One quick question, is it possible to have a html file come up after the form is submitted? What I would like to do is have a thanks.htm come up instead of the "data has been submitted or whatever text message on a white screen. So far, when I have put any links in there they have just come up as a hyperlink on the next page.

Thanks again,
Brian

Ankou
August 4th, 2005, 04:53 PM
Glad it works. I missed a { did I? Hmmm that's what I get for posting so late at night. LOL


Yeah you can have whatever you want come up on the page...



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<title>Email</title>
</head>
<body>

<?php
if(isset($_POST['submit'])) {
$to = "brian@thedentistschoice.com";
$re = "Feedbackfromcontactpage";

$name_field = $_POST['name'];
$phone_field = $_POST['phone'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Message: $message\n Phone: $phone_field\n";

if(mail($to,$re,$body)){
?>

<h1>Thank You!</h1>
<p>
Data has been submitted to $to! <br />
We appreciate your email and..... <br />
PUT WHATEVER YOU WANT IN HERE....
</p>

<?php
}else{
?>

<h1>Error! Email failed to send.</h1>
<p>
Error in sending data. Please contact ....
AGAIN WHATEVER YOU WANT TO PUT IN HERE...
</p>

<?php
}
} else {
?>

<h1>Error! Information not vaild or not obtained.</h1>
<p>
There was an error in obtaining your information. Data was not sent.
AGAIN WHATEVER YOU WANT TO PUT IN HERE...
</p>

<?php
}
?>

</body>
</html>


You may want to check over the opening and closing of those PHP blocks... I did them rather quickly.

60Fairlane
August 4th, 2005, 06:02 PM
Ankou,

Thanks for the quick reply. Ok, I see how you've laid that out :) I guess what I was hoping to do was: after the contact info was emailed another html page would pop-up (ie one that I already created that is a separate web page - thanks.htm) But I'm guessing that would be another script altogether.

Thanks again,
Brian

Ankou
August 4th, 2005, 07:18 PM
Oh I see what you're saying. Well you could do that with some javascript. Of course you'd have to watch out for people who may have alread closed that other window, or for those who have pop-up blockers.

But... use onload in the body tag in the page that outputs the code above (that email form stuff). The onload would call window.open that would load the HTML page (thanks.htm). And there you go.

The advantage of using the code I have above is that you have all the users information so you could make a thank you page that is more taylored to the user. Like... "Thanks Frank for emailing us. The information that you submitted is as follows:" and then output the message that was actually emailed to you. But that's just a matter or preference I guess.

60Fairlane
August 5th, 2005, 11:28 AM
Ah good point, I didn't think through the possibilities about the customization part. I would have to separate my "name field" into two parts - first name and last name to accomplish what you are saying (re: Thanks frank), right? Would I just reference the name field in the "We appreciate your" area? Thanks again for all of your help - I'm learning a ton (steep curve ahead :lol: )

Ankou
August 5th, 2005, 01:45 PM
Would I just reference the name field in the "We appreciate your" area? Thanks again for all of your help - I'm learning a ton (steep curve ahead :lol: )

Yeah you could just referenece the name field in that area - of course because of the way I have it set up you'd need to have....



$body = "From: $name_field\n E-Mail: $email_field\n Message: $message\n Phone: $phone_field\n";

if(mail($to,$re,$body)){
?>

<h1>Thank You!</h1>
<p>
Data has been submitted to <?php echo $to; ?>! <br />
<?php echo $name_field; ?>, we appreciate your email and..... <br />
PUT WHATEVER YOU WANT IN HERE....
</p>

<?php
}else{
?>

<h1>Error! Email failed to send.</h1>
<p>



I didn't want to post the whole script again so I just used part of it.

BTW - I just noticed in the previous code I posted I had used $to in an area that wasn't inclosed in a PHP section - Opps. That wouldn't work (it's fixed in the code above). Sorry if that confused you at all.

andres
August 5th, 2005, 02:30 PM
and dont forget to close the If statement there.... :D

Ankou
August 5th, 2005, 04:11 PM
and dont forget to close the If statement there.... :D

Which if statement? The one in my previous post? Because that's just a snippet of the overall code I was using... and the if statement is closed. :D


Notice:


$body = "From: $name_field\n E-Mail: $email_field\n Message: $message\n Phone: $phone_field\n";

if(mail($to,$re,$body)){
?>

<h1>Thank You!</h1>
<p>
Data has been submitted to <?php echo $to; ?>! <br />
<?php echo $name_field; ?>, we appreciate your email and..... <br />
PUT WHATEVER YOU WANT IN HERE....
</p>

<?php
}else{ // See the IF statment is closed here before the ELSE...
?>

<h1>Error! Email failed to send.</h1>
<p>

Unless I missed closing some other IF statement...

andres
August 5th, 2005, 04:38 PM
come on buddy, you know what I mean... dont forget to close the IF ... ELSE ... statement (you know that happens every once in a while to everybody).... and I knew that was just a snippet of code because you didnt wanna post the whole thing (you said it too)...

hl
August 5th, 2005, 04:53 PM
guys, learn to indent :D

that way, if you finish the code and somethings not closed, the levels won't be at 0.

Ankou
August 5th, 2005, 10:43 PM
@andres ... Gottcha - just the way you worded you post. I didn't want 60Fairlane to think something was missing in the full code (not the snippet).


@.harish - my code's indented in my file. When I type/copy it in with the indents and preview it, it comes up fine. But then in the reply to thread area -- well all the indenting is gone. That's kind of pain. But I'll try to remember to re-indent after I preview the post. ;)

60Fairlane
August 6th, 2005, 12:29 AM
Hi Ankou,

Thanks for the correction. I must've missed something a couple of posts back :huh: - is there something I need to correct?

Thanks,
Brian

Ankou
August 6th, 2005, 01:13 AM
I'm almost lost myself with everything going on... play it safe and use the code below. I tested it and it works....



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Email</title>
</head>
<body>

<?php
if(isset($_POST['submit'])) {
$to = "brian@thedentistschoice.com";
$re = "Feedbackfromcontactpage";

$name_field = $_POST['name'];
$phone_field = $_POST['phone'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Message: $message\n Phone: $phone_field\n";

if(mail($to,$re,$body)){
echo "
<h1>Thank You!</h1>
<p>
Data has been submitted to $to! <br />
We appreciate your email and..... <br />
PUT WHATEVER YOU WANT IN HERE....
</p>";
}else{
echo "
<h1>Error! Email failed to send.</h1>
<p>
Error in sending data. Please contact ....
AGAIN WHATEVER YOU WANT TO PUT IN HERE...
</p>";
} // END ELSE of IF(mailto(...))
}else{ // END IF(isset(...)) / Start ELSE
echo "
<h1>Error! Information not vaild or not obtained.</h1>
<p>
There was an error in obtaining your information. Data was not sent.
AGAIN WHATEVER YOU WANT TO PUT IN HERE...
</p>";
} // END ELSE of IF(isset(...))
?>

</body>
</html>

60Fairlane
August 8th, 2005, 05:05 PM
Hi Ankou,

I Hope you weren't getting lost on my account. :cantlook:

Thanks again!!

Cheers,
Brian