PHP Contact Form Help

KeybladeSephi

New Member
Messages
19
Reaction score
0
Points
0
Hello, everyone; I am new to PHP and have no idea how to create a contact table. I've searched and searched on google, but still don't get the gist of it. I think you're supposed to make a php document and then incorporate the php document in the html document inside of a form (method="GET" action="the PHP document"). I don't know how to code it though, or how to make the form in html. Could someone please help me? I just want a fairly simple one with a Name, Email Address, and Comment Box. Thank you very much :cool:
 

rockee

New Member
Messages
120
Reaction score
0
Points
0
Try a ready made contact form script from the best free script repository on the net at:
http://www.hotscripts.com

Click on the PHP link then click on Scripts and Programs.

Find and click on Form Processors

Watch out for the Commercial scripts and ignore them as there are many free scripts that are often better and all you would ever need.

Look for scripts that have been recently updated as they may have the latest features available.

They all mostly come with installation instructions.

If you want to code your own then I hope your not in a rush as the learning curve is fairly steep.

You could start with the PHP manual from here:
www.php.net/docs.php

Regards,
Rocky
 
Last edited:

KeybladeSephi

New Member
Messages
19
Reaction score
0
Points
0
Thanks, guys. I've been studying more and I think I get a lot of the code. I copied some code that was guaranteed to work (I made sure of that ;)), however, the "Comments or Questions" won't appear in the email that is sent to me. Here is the code for the PHP document:

Code:
<? /* Courtesy of Robert Packer */

$name     = $_POST['name'];
$address  = $_POST['address'];
$state    = $_POST['state'];
$city     = $_POST['city'];
$zip      = $_POST['zip'];
$country  = $_POST['country'];
$phone    = $_POST['phone'];
$email    = $_POST['email'];
$comments = $_POST['comments'];
$fax      = $_POST['fax'];
$error_msg = "";
$msg = "";

if(!$name){
    $error_msg .= "Your name \n";
}
if($name){
    $msg .= "Name: \t $name \n";
}

if(!$email){
    $error_msg .= "Your email \n";
}
if($email){
    if(!eregi("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\._\-]+\.[a-zA-Z]{2,4}", $email)){
        echo "\n<br />That is not a valid email address.  Please <a href=\"javascript:history.back()\">return</a> to the previous page and try again.\n<br />";
        exit;
    }            
    $msg .= "Email: \t $email \n";
}
$sender_email="";

if(!isset($name)){
    if($name == ""){
        $sender_name="Web Customer";
    }
}else{
    $sender_name=$name;
}
if(!isset($email)){
    if($email == ""){
        $sender_email="Customer@website.com";
    }
}else{
    $sender_email=$email;
}
if($error_msg != ""){
    echo "You didn't fill in these required fields:<br />"
    .nl2br($error_msg) .'<br />Please <a href="javascript:history.back()">return</a> to the previous page and try again.';
    exit;
}
$mailheaders  = "MIME-Version: 1.0\r\n";
$mailheaders .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$mailheaders .= "From: $sender_name <$sender_email>\r\n";
$mailheaders .= "Reply-To: $sender_email <$sender_email>\r\n"; 
mail("KeybladeSephi@netscape.net","Email From Site",stripslashes($msg), $mailheaders);
header("Location: http://keybladesephi.exofire.net"); /* Redirect browser */
?>
And here is the code for my HTML document (the form part):

Code:
<form method="post" action="contact.php">
    <table width="514" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td width="139" valign="top"><div align="right">Name:&nbsp;</div></td>
            <td width="5" rowspan="3" valign="top">&nbsp;</td>
            <td width="370"><input name="name" type="text" class="textInput" />
              <br />
              &nbsp;</td>
        </tr>
        <tr>
            <td valign="top"><div align="right">E-mail:&nbsp;</div></td>
            <td><input name="email" type="text" class="textInput" />
              <br />
&nbsp;            </td>
        </tr>
        <tr>
            <td valign="top"><div align="right">Comments or Questions:&nbsp;</div></td>
            <td>
                <textarea name="comments_or_questions" cols="50" rows="10" class="textInput"></textarea>            </td>
        </tr>
            <tr><td colspan="2">&nbsp;</td>
            <td>
                <input type="submit" name="submit" value="Submit" />
                &nbsp;
                <input type="reset" name="reset" value="Reset" />            </td>
        </tr>
    </table>
</form>
Could you please tell me what's wrong? Thank you.
 

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
Try replacing
PHP:
$comments = $_POST['comments'];

With

PHP:
$comments = $_POST['comments_or_questions'];

Edit: Try this code which I wrote, it works just fine. I tested it.

PHP:
<?php 

$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments_or_questions'];
$valid_email = false;

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) 
{
  $valid_email=true;
}

if(!$name or !$comments or ($valid_email==false))
{
    echo ("You didn't fill in all the required fields:<br />
    Please <a href=\"javascript:history.back()\">return</a> to the previous page and try again.");
}
else
{
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	$headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
	$headers .= 'Reply-To: '.$email. "\r\n";	
	
	mail("KeybladeSephi@netscape.net","Email From Site", stripslashes($comments), $headers);
	header("Location: http://keybladesephi.exofire.net"); /* Redirect browser */
}
?>
 
Last edited:

KeybladeSephi

New Member
Messages
19
Reaction score
0
Points
0
Oh thank you so much, verbsite; I just forgot to change the values for "comments"
Edit:
Hey you guys are a lot better than me at PHP; I have a pet peeve of mine that makes me really vexed when the pages aren't consistent. In my code, if the email address is not valid, it'll go to the "contact.php" page and say "You didn't fill in the required fields; please return to the previous page and try again." However, the page does not have any style; just a white background with black text. Is there a way to redirect to a page of my choosing with the correct style settings? I'm pretty sure the code that I have to edit is this:

Code:
if(!$name or !$comments or ($valid_email==false))
{
    echo ("You didn't fill in all the required fields:<br />
    Please <a href=\"javascript:history.back()\">return</a> to the previous page and try again.");
}
Edit:
Oh never mind; I figured out how to add styles to my php document. Thanks guys!
 
Last edited:
Top