Page 1 of 1

PHP Newbie question...please help!

Posted: Fri Jun 04, 2010 6:43 pm
by 12express
I'm working with these codes to render a contact form, but I can't seem to find what's wrong with it...clearly I'm missing something, the validation works perfectly fine but after the form is submitted nothing happens. Your help would be greatly appreciated :)

Code: Select all

<?php

/************************
* Variables you can change
*************************/

$mailto = "";
$cc = "";
$bcc = "";
$subject = "";
$vname = "";


/************************
* do not modify anything below unless you know PHP/HTML/XHTML
*************************/


$email = $_POST['email'];

function validateEmail($email)
{
   if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $email))
	  return true;
   else
	  return false;
}


if((strlen($_POST['name']) < 1 ) || (strlen($email) < 1 ) || (strlen($_POST['message']) < 1 ) || validateEmail($email) == FALSE){
	$emailerror .= 'Error:';

	if(strlen($_POST['name']) < 1 ){
		$emailerror .= '<li>Enter name</li>';
	}

	if(strlen($email) < 1 ){
		$emailerror .= '<li>Enter email</li>';
	}

	if(validateEmail($email) == FALSE) {
		$emailerror .= '<li>Enter valid email</li>';
	}

	if(strlen($_POST['message']) < 1 ){
		$emailerror .= '<li>Enter message</li>';
	}

} else {

	$emailerror .= "<span>Your email has been sent successfully!</span>";



	// NOW SEND THE ENQUIRY

	$timestamp = date("F j, Y, g:ia");

	$messageproper ="\n\n" .
		"Name: " .
		ucwords($_POST['name']) .
		"\n" .
		"Email: " .
		ucwords($email) .
		"\n" .
		"Comments: " .
		$_POST['message'] .
		"\n" .
		"\n\n" ;

		$messageproper = trim(stripslashes($messageproper));
		mail($mailto, $subject, $messageproper, "From: \"$vname\" <".$_POST['e_mail'].">\nReply-To: \"".ucwords($_POST['first_name'])."\" <".$_POST['e_mail'].">\nX-Mailer: PHP/" . phpversion() );

}
?>

Re: PHP Newbie question...please help!

Posted: Fri Jun 04, 2010 6:54 pm
by Jonah Bron
Do you mean that the email doesn't send? Do you have error reporting on? Try echoing your validated variables right before the mail() function.

Re: PHP Newbie question...please help!

Posted: Sun Jun 06, 2010 6:25 pm
by 12express
Jonah Bron wrote:Do you mean that the email doesn't send? Do you have error reporting on? Try echoing your validated variables right before the mail() function.
Isn't that already set in the code? I'm not entirely sure where it's suppose to go. The form validates properly but nothing shows up in my inbox.

Re: PHP Newbie question...please help!

Posted: Sun Jun 06, 2010 8:27 pm
by Jonah Bron
There is no email in the $mailto variable. I'll assume you removed it to post the code here.

Do you get the "you email has been sent" message? If you do, then it might be a problem with the mail() function. Try it without all of the headers (like this)

Code: Select all

mail($mailto, $subject, $messageproper);

Re: PHP Newbie question...please help!

Posted: Sun Jun 06, 2010 10:23 pm
by 12express
Jonah Bron wrote:There is no email in the $mailto variable. I'll assume you removed it to post the code here.

Do you get the "you email has been sent" message? If you do, then it might be a problem with the mail() function. Try it without all of the headers (like this)

Code: Select all

mail($mailto, $subject, $messageproper);
Yup I removed the email variable, and I tried it without all the headers but that doesn't seem like it's working :( I modified the code below to send the email but don't know how I can add the validation portion:

Code: Select all

<?php

/* Subject and Emal variable */

	$emailSubject = '';
	$webMaster = '';
	
/* Gathering Data Variable */

	$nameField = $_POST['name'];
	$emailField = $_POST['email'];
	$messageField = $_POST['message'];

function validateEmail($emailField)
{
   if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $emailField))
	  return true;
   else
	  return false;
}
	
	$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Email: $emailField <br>
Message: $messageField <br>
<br><hr><br>
EOD;

	$headers = "From: $email\r\n";
	$headers .= "Content-type: text/html\r\n";
	$success = mail($webMaster, $emailSubject, $body, $headers);
	
/*  Results Rendered as HTML */

	$theResults = <<<EOD
<html>
<head>
<meta http-equiv="REFRESH" content="0;url=http://www.jimkwik.com/"></HEAD>
<BODY>
Your email has been sent successfully!
</BODY>
</html>
EOD;
echo "$theResults";

?>
But every time I'm trying to add this:

Code: Select all

if((strlen($_POST['name']) < 1 ) || (strlen($email) < 1 ) || (strlen($_POST['message']) < 1 ) || validateEmail($email) == FALSE){
	$emailerror .= 'Error:';

	if(strlen($_POST['name']) < 1 ){
		$emailerror .= '<li>Enter name</li>';
	}

	if(strlen($email) < 1 ){
		$emailerror .= '<li>Enter email</li>';
	}

	if(validateEmail($email) == FALSE) {
		$emailerror .= '<li>Enter valid email</li>';
	}

	if(strlen($_POST['message']) < 1 ){
		$emailerror .= '<li>Enter message</li>';
	}

} else {

	$emailerror .= "<span>Your email has been sent successfully!</span>";
After the function validateEmail($email) variable I get error messages. I must be missing something since it worked before but now it's not working after I modified it...any suggestions?

Re: PHP Newbie question...please help!

Posted: Sun Jun 06, 2010 11:39 pm
by Jonah Bron
Make a new, empty php file, and just put the mail function in to send a test email to yourself. Lets see if that works.

You said you get error messages now; do you mean your error messages, or PHP errors?

Re: PHP Newbie question...please help!

Posted: Mon Jun 07, 2010 12:38 am
by social_experiment
This might not be relevant but i see in some of your code you have different variables for the email address

Code: Select all

<?php $emailField = $_POST['email']; ?>
but when you test the variable you use the variable '$email'. (This is from the last code you pasted)