PHP Newbie question...please help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
12express
Forum Newbie
Posts: 3
Joined: Fri Jun 04, 2010 6:37 pm

PHP Newbie question...please help!

Post 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() );

}
?>
Last edited by 12express on Sun Jun 06, 2010 6:19 pm, edited 1 time in total.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Newbie question...please help!

Post 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.
12express
Forum Newbie
Posts: 3
Joined: Fri Jun 04, 2010 6:37 pm

Re: PHP Newbie question...please help!

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Newbie question...please help!

Post 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);
12express
Forum Newbie
Posts: 3
Joined: Fri Jun 04, 2010 6:37 pm

Re: PHP Newbie question...please help!

Post 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?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Newbie question...please help!

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP Newbie question...please help!

Post 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)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply