Page 1 of 1

Simple Contact Form Help

Posted: Tue Feb 02, 2010 10:00 am
by lkalik
Hello, I am very new to PHP and just created my first small contact form, however I can not get the form to send to my email. I would also like to know what code I can include that once I hit submit the page redirects to a certain page of my site. Below is the HTML code for my form: (the form page is called about_new.html)
<form METHOD=POST ACTION="contact_form.php">
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<h3 align="right" class="fieldname">Name:</h3></td>
<td>&nbsp;<INPUT NAME="name" TYPE="text" class="form" size="27"></td></tr>
<tr><td><h3 align="right" class="fieldname">E-mail:</h3></td><td><INPUT NAME="email" TYPE="text" class="form" size="27"></td></tr>
<tr><td valign="top"><h3 align="right" class="fieldname">Message:</h3></td>
<td><TEXTAREA NAME="message" COLS="25" ROWS="4" class="message"></TEXTAREA></td></tr>
<td><br> </td><td><br>
<input name="submit" type="submit" class="sendbtn" id="Submit" value="Submit" /></td></tr>
</table>
</form>

The following is the php code in a file called contact_form.php
<?php
if(isset($_POST['submit'])) {
$to = "lauren.kalik@yahoo.com";
$subject = "IP Contact Form";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];

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

echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";
}
?>

Any help would be greatly appreciated! Thanks in advance!

Re: Simple Contact Form Help

Posted: Tue Feb 02, 2010 10:38 am
by aravona
Post in code tags ^_^

One way to redirect :

Code: Select all

echo "<head><meta HTTP-EQUIV='REFRESH' content='0; url=where u wanna go to'></head>";
This is just the way I use in if statement when redirecting, dunno if its the best way to do it or not but it works.

Re: Simple Contact Form Help

Posted: Tue Feb 02, 2010 11:35 am
by lkalik
Thanks for your help...your suggestion on the redirect worked beautifully for me.

I am unfamilar with the tag ^_^ that you are suggesting I use for the first problem. Can you be more specific on where this should be used? Sorry for the stupid questions...like I said I am a beginner here.

Re: Simple Contact Form Help

Posted: Wed Feb 03, 2010 4:02 am
by aravona
lol I meant tags to post up your code - takes up less space on the page and makes it easier to read :)

Re: Simple Contact Form Help

Posted: Wed Feb 03, 2010 6:36 am
by lkalik
haha...ok here you go.
Below is the HTML code for my form: (the form page is called about_new.html)

Code: Select all

<form METHOD=POST ACTION="contact_form.php"> 
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<h3 align="right" class="fieldname">Name:</h3></td>
<td>&nbsp;<INPUT NAME="name" TYPE="text" class="form" size="27"></td></tr>
<tr><td><h3 align="right" class="fieldname">E-mail:</h3></td><td><INPUT NAME="email" TYPE="text" class="form" size="27"></td></tr>
<tr><td valign="top"><h3 align="right" class="fieldname">Message:</h3></td>
<td><TEXTAREA NAME="message" COLS="25" ROWS="4" class="message"></TEXTAREA></td></tr>
<td><br> </td><td><br>
<input name="submit" type="submit" class="sendbtn" id="Submit" value="Submit" /></td></tr>
</table>
</form>
The following is the php code in a file called contact_form.php

Code: Select all

<?php
if(isset($_POST['submit'])) {
$to = "lauren.kalik@yahoo.com";
$subject = "IP Contact Form";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
 
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
 
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";
}
?>