Help with PHP/HTML Contact Form (Basic)

Hey Guys,
I’m trying to get this contact form to work but it doesnt seem to want too. It will send the user an email but I never see the contact form email. I have checked for spelling errors and everything is correct. I got the code from a site I got working a while ago that still works I just swapped out the fields.

HTML:
[php]

		<div class="col grid3">
			<label for="firstname">First Name:</label>
			<input type="text" name="firstname" id="firstname" class="full-width" required />
		</div>
		<div class="col grid3">
			<label for="lastname">Last Name:</label>
			<input type="text" name="lastname" id="lastname" class="full-width" required />
		</div>
		<div class="col grid3">
			<label for="phone">Phone:</label>
			<input type="text" name="phone" id="phone" class="full-width" required />
		</div>
		<div class="col grid3">
			<label for="email">Email:</label>
			<input type="text" name="email" id="email" class="full-width" required />
		</div>
		
		<div class="col grid6">
			<label for="bid">Bid Amout:</label>
			<input type="text" name="bid" id="bid" class="full-width" required />
		</div>

I agree to the terms and conditions.

			<input  type="submit" value="Submit Bid" align="right"/>
		<div class="clear"></div>
	</form>[/php]

PHP:
[php]<?php

// first clean up the input values
foreach($_POST as $key => $value) {
if(ini_get(‘magic_quotes_gpc’))
$_POST[$key] = stripslashes($_POST[$key]);

$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

$ip=$_SERVER[‘REMOTE_ADDR’];
$email_to = "[email protected]";
$email_subject = “Painting Bid | TEST”;

$email_message .= “First Name: “.$_POST[“firstname”].”\n”;

$email_message .= “Last Name: “.$_POST[“lastname”].”\n”;

$email_message .= “Email: “.$_POST[“email”].”\n”;

$email_message .= “Phone: “.$_POST[“phone”].”\n”;

$email_message .= “Bid: “.$_POST[“bid”].”\n”;

$userEmail = filter_var( $_POST[‘email’],FILTER_VALIDATE_EMAIL );

if( ! $userEmail ){

exit;

}

//email headers

$headers = 'From: '.$_POST[“email”]."\r\n".

'Reply-To: '.$_POST[“email”]."\r\n" .

‘X-Mailer: PHP/’ . phpversion();

echo (mail($email_to, $email_subject, $email_message, $headers) ? " TEST ");

$ip=$_SERVER[‘REMOTE_ADDR’];
$email_to = $_POST[“email”];
$email_subject = “TEST”;

$email_message1 = " TEST
";

//email headers

$headers = 'From: '.$_POST[“email”]."\r\n".

'Reply-To: '.$_POST[“email”]."\r\n" .

‘X-Mailer: PHP/’ . phpversion();

echo (mail($email_to, $email_subject, $email_message1, $headers) ? “”:"");

exit();

// test input values for errors
$errors = array();
if(strlen($fname) < 2) {
if(!$fname) { $errors[] = “You must enter a name.”; }
else { $errors[] = “Name must be at least 2 characters.”; }
}
if(!$email) { $errors[] = “You must enter an email.”; }
else if (!validEmail($email)) { $errors[] = “You must enter a valid email.”; }

if($errors) {
// output errors to browser and die with a failure message
$errortext = “”;
foreach($errors as $error) {
$errortext .= “

  • ”.$error."
  • ";
    }
    die(“The following errors occured:
      ”. $errortext ."
    ");
    }

    // check to see if email is valid
    function validEmail($email) {
    $isValid = true;
    $atIndex = strrpos($email, “@”);
    if (is_bool($atIndex) && !$atIndex) { $isValid = false; }
    else {
    $domain = substr($email, $atIndex+1);
    $local = substr($email, 0, $atIndex);
    $localLen = strlen($local);
    $domainLen = strlen($domain);
    if ($localLen < 1 || $localLen > 64) { $isValid = false; } // local part length exceeded
    else if ($domainLen < 1 || $domainLen > 255) { $isValid = false; } // domain part length exceeded
    else if ($local[0] == ‘.’ || $local[$localLen-1] == ‘.’) { $isValid = false; } // local part starts or ends with ‘.’
    else if (preg_match(’/\.\./’, $local)) { $isValid = false; } // local part has two consecutive dots
    else if (!preg_match(’/^[A-Za-z0-9\-\.]+$/’, $domain)) { $isValid = false; } // character not valid in domain part
    else if (preg_match(’/\.\./’, $domain)) { $isValid = false; } // domain part has two consecutive dots
    else if (!preg_match(’/^(\\.|[A-Za-z0-9!#%&`_=\/$’*+?^{}|~.-])+$/’, str_replace("\\","",$local))) {
    // character not valid in local part unless local part is quoted
    if (!preg_match(’/^"(\\"|[^"])+"$/’, str_replace("\\","",$local))) { $isValid = false; }
    }
    if ($isValid && !(checkdnsrr($domain,“MX”) || checkdnsrr($domain,“A”))) { $isValid = false; } // domain not found in DNS
    }
    return $isValid;
    }

    ?>

    [/php]

    Thanks again!

    This seems to be a major issue when using the default mail client. I would highly suggest using something like PHPMailer and setting up a [email protected] address specifically for sending emails to prevent emails from being caught by spam filters ( Hotmail and GMail ar VERY bad about doing this).

    itskater91,
    An email library as Astonecipher mentioned is good especially if you are planning a lot of different emails
    from various pages on your site.

    I do use the PHP mail function on several sites I work on with no problems at all. Except one odd server,
    my own which is hosted on GoDaddy.com. Seems they will not mail unless you tell them you are sending from
    their server. A minor change needed for that server.

    I suggest that if you want to stay with the PHP mail function instead of going to an add-in library, that you
    start by just testing a simple email test file first. Take your code and create a test page to send a hard
    coded email and see if you receive it. Just something like this might work:
    [php]

    <?php $ip=$_SERVER['REMOTE_ADDR']; $email_to = "[email protected]"; $email_subject = "Painting Bid | TEST"; $email_message = "Testing emails... Sent from IP: " . $ip . "\r\n"; //email headers $headers = 'From: '[email protected]' . "\r\n"; 'Reply-To: '[email protected]' . "\r\n"; 'X-Mailer: PHP/' . phpversion(); if (mail($email_to, $email_subject, $email_message, $headers)) { echo "email sent!"; } else { echo "email failed!"; } ?>

    [/php]
    You should replace the email addresses to your own instead of [email protected] as this would spawn an
    error on most server’s email systems. This is just a test to make sure you can send emails from your
    server. If it works, then add in parts of your form data step by step testing along the way. Hope that
    helps somehow…

    I see what you guys are saying. I have used this code before on Rack Space and Hostway and I didn’t have any problems. I tried the code you provided and plugged in my email but Dream Weaver says theirs an error on line 8 and 9. When I actually tried the code I got the same result. No email and when I press submit it shows a white page.

    Do you guys think you could point me towards a simple code to work? I don’t need the error messages or anything just a simple php form that will work with these text fields.

    Thanks guys! I really appreciate it!

    Sorry, somehow those two lines when copied replaced semicolons with periods… I fixed it in that post!
    Retry it…

    Okay, I’m so close!

    I got the email to send to me with this PHP code:

    [php]<?php

    $EmailFrom = "[email protected]";
    $EmailTo = "[email protected]";
    $Subject = “Test”;
    $fullname = Trim(stripslashes($_POST[‘fullname’]));
    $phone = Trim(stripslashes($_POST[‘phone’]));
    $email = Trim(stripslashes($_POST[‘email’]));
    $cemail = Trim(stripslashes($_POST[‘cemail’]));
    $bid = Trim(stripslashes($_POST[‘bid’]));

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

    // prepare email body text
    $Body = “”;
    $Body .= "Full Name: ";
    $Body .= $fullname;
    $Body .= “\n”;
    $Body .= "Phone: ";
    $Body .= $phone;
    $Body .= “\n”;
    $Body .= "Email: ";
    $Body .= $email;
    $Body .= “\n”;
    $Body .= "Confirm Email: ";
    $Body .= $cemail;
    $Body .= “\n”;
    $Body .= "Bid Amount: ";
    $Body .= $bid;
    $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=contactthanks.php”>”;
    }
    else{
    print “<meta http-equiv=“refresh” content=“0;URL=error.htm”>”;
    }
    ?>[/php]

    The only problem is the fields come out blank like this in the email:

    Full Name:
    Phone:
    Email:
    Confirm Email:
    Bid Amount:

    FYI I change the emails to test for the forum.

    Okay, good, we are closer to solving the puzzle…

    Now, first, you should take these lines:
    $Body = “”;
    $Body .= "Full Name: ";
    $Body .= $fullname;
    $Body .= “\n”;
    and just make it one line like this… I think it is much easier to read. One line per line in the email…
    $Body = "Full Name: " . $fullname . “\n”;
    (Of course the next line would use .= … Hope that makes sense! )

    Now, for the passing of the data issue… Your form has an action, but, no method. Some server’s default to
    “POST”, but, you really should tell it to post so that it does not “get”… therefore try this for the form line in
    the HTML"

    Also, most programmers just have the code post to itself with the PHP and HTML all on the same page. In
    this manner, you can input, validate and show the results and only use one page. Things like success or
    failing are displayed above the the form. Not sure if that makes sense to you, but, it will eventually…

    See if that works!

    Bingo!
    You were right it was the method post that was the problem!
    This fixed the problem

    Congratulations! Very nice feeling to fix a programming puzzle…

    We will be looking for your next one… :wink:

    Sponsor our Newsletter | Privacy Policy | Terms of Service