If statement logic is screwed up

Whenever the form is submitted, no email is sent and all I get is the “Error” so I guess the


<?php if(!isset($hasError)) ?>

is the only thing being run


  
<?php 
/* 
Template Name: Bobs Contact Form 
*/ 
?> 
 

<?php get_header(); ?> 
 
<div class="map"> 
<iframe width="480" height="494" frameborder="0" scrolling="no"  marginheight="0" marginwidth="0"  src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Napolitano+Realty,+939+Orange+Avenue,+Coronado,+CA+92118-2609&amp;aq=2&amp;sll=37.0625,-95.677068&amp;sspn=40.409448,93.076172&amp;ie=UTF8&amp;hq=Napolitano+Realty,&amp;hnear=939+Orange+Ave,+Coronado,+San+Diego,+California+92118&amp;ll=32.686543,-117.179229&amp;spn=0.006295,0.006295&amp;output=embed"></iframe> 
</div> 


<?php 

//If the form is submitted 
if(isset($_POST['submitted'])) 
    { 
    //Check to make sure that the name field is not empty 
    if(!isset($_POST['contactName'])) { 
        $nameError = 'You forgot to enter your name.'; 
        $hasError = true; 
    } else { 
        $name = trim($_POST['contactName']); 
    } 

    //Check to make sure sure that a valid email address is submitted 
    $email = $_POST['email']; 
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) 
    { 
        $emailError = 'You entered an invalid email address.'; 
        $hasError = true; 
    } 
    //Check to make sure comments were entered     
    if(!isset($_POST['comments'])) 
    { 
        $commentError = 'You forgot to enter your comments.'; 
        $hasError = true; 
    } else { 
        $comments = trim($_POST['comments']); 
    } 
    //If there is no error, send the email 
    if(!isset($hasError)) { 
    echo 'email sent'; 
    $phone = $_POST['phone']; 

    $to= 'example@example.com'; 
    $subject = 'Contact Form Submission from '.$name; 
    $body = 'Name: '.$name." \
\
".'Email: '.$email." \
\
".'Phone: '.$phone." \
\
".'Comments: '.$comments; 
    $headers = 'From: My Site <'.$to.'>' . "\\r\
" . 'Reply-To: ' . $email; 

    $emailSent = mail($to, $subject, $body, $headers); 
    } else { 
    echo 'Error'; 
    } 
    if($emailSent) { 
?> 
    <div class="thanks"> 
        <h1 style="text-transform: none;">Thanks, <?=$name;?></h1> 
        <p>Your email was successfully sent. I will be in touch soon.</p> 
    </div> 

<?php } 
} else { ?> 
 

<div class="contact-form" style="padding:25px; float:left;  margin-top: 50px;  background-color: rgba(195,166,96,.13); height:  444px; width: 400px;"> 
<h1 style="text-align:center; margin-left: -25px; margin-top:  -25px; width: 420px; color: white; font: 700 30px/1.1 georgia,'times new  roman','sans-serif'; text-transform: uppercase;">Contact  Form</h1> 
        <form action="<?php the_permalink(); ?>" id="contactForm" method="post"> 
     
            <ol class="forms" style="margin-top:25px; width:400px; clear:none; margin-left:0"> 
                <li><label for="contactName">Name</label> 
                    <input type="text" name="contactName" id="contactName" class="requiredField" /> 
                    <?php if($nameError != '') { ?> 
                        <span class="error"><?php echo $nameError; ?></span> 
                    <?php } ?> 
                </li> 
                <li><label for="email">Email</label> 
                    <input type="text" name="email" id="email" class="requiredField email" /> 
                    <?php if($emailError != '') { ?> 
                        <span class="error"><?php echo $emailError; ?></span> 
                    <?php } ?> 
                </li> 
                <li><label for="phone">Phone</label> 
                    <input type="text" name="phone" id="phone" /> 
                </li> 
                <li class="textarea"><label for="commentsText">Comments</label> 
                    <textarea name="comments" id="commentsText"  rows="10" cols="30" class="requiredField"  style="height:150px"></textarea> 
                    <?php if($commentError != '') { ?> 
                        <span class="error"><?php echo $commentError; ?></span> 
                    <?php } ?> 
                </li> 
                <li class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" /> 
                <button type="submit" style="float: right;  padding-top: 9px; padding-bottom: 8px; padding-left: 6px; padding-right:  6px; font-family: georgia, 'times new roman',sans-serif;  
                background-color: #F47D3B; color: white; text-transform:uppercase">Submit</button></li> 
            </ol> 
        </form> 
</div>     
<?php 
} 
?> 
 
<?php get_footer(); ?>  

is the only thing being run…
Why?
the script is at http://crowntownbob.com/contact/
Thanks…

Right before this line

//If there is no error, send the email

add

var_dump($hasError); die;

Won’t work. His logic is testing for its set status.

OP, ALWAYS, ALWAYS, ALWAYS initialize your variables. Do not get in the habit of flagging things on whether or not they are set. So INTIALIZE…


$hasError = false;

//If the form is submitted 
if(isset($_POST['submitted'])) 
    { 

And then later, when you test it, test to see if was toggled over to true.


    //If there is no error, send the email 
    if($hasError) {
     // Handle error here.
} else { 
    echo 'email sent'; 
    $phone = $_POST['phone']; 


And as illustrated there, avoid unnecessary use of the not operator.

My main guess is that $hasError, does in fact have an error. So a var_dump shows what’s in it.

You didn’t read his code. He’s merely setting it to ‘true’. Something else, earlier in his program, could be setting it. Your test will show what’s in it, but not necessarily why - and in any event for the long term health of his code he should init that variable. He also could be setting it to false and forgetting that he set it false. If that’s the case isset($hasError) will return true even if the value is false.

There’s a time and place for isset, but this isn’t it. That language construct (it is not a function due to how it works) isn’t a panacea for the more diligent approach of declaring variables before they are used.

Ah you’re right about $hasError just being a boolean, I glanced through it and thought he was storing the error message in it.

I do agree regarding the isset, but it still doesn’t address OP’s issue because the most likely chance is that $hasError is not set before the form handler, thus, while using isset isn’t 100% correct, it should work in this case.

k, changed the code…



<?php
/*
Template Name: Bobs Contact Form
*/
?>


<?php get_header(); ?>

<div class="map">
<iframe width="480" height="494" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Napolitano+Realty,+939+Orange+Avenue,+Coronado,+CA+92118-2609&amp;aq=2&amp;sll=37.0625,-95.677068&amp;sspn=40.409448,93.076172&amp;ie=UTF8&amp;hq=Napolitano+Realty,&amp;hnear=939+Orange+Ave,+Coronado,+San+Diego,+California+92118&amp;ll=32.686543,-117.179229&amp;spn=0.006295,0.006295&amp;output=embed"></iframe>
</div>


<?php
$hasError = false;

//If the form is submitted
if(isset($_POST['submitted']))
    {
    //Check to make sure that the name field is not empty
    if(!isset($_POST['contactName'])) {
        $nameError = 'You forgot to enter your name.';
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }

    //Check to make sure sure that a valid email address is submitted
    $email = $_POST['email'];
    if(!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        $emailError = 'You entered an invalid email address.';
        $hasError = true;
    }
    //Check to make sure comments were entered    
    if(!isset($_POST['comments']))
    {
        $commentError = 'You forgot to enter your comments.';
        $hasError = true;
    } else {
        $comments = trim($_POST['comments']);
    }
    //If there is no error, send the email
    if($hasError) {
    echo $hasError;
    echo 'Error';
    } else {
    echo 'email sent';
    $phone = $_POST['phone'];

    $to= 'example@example.com';
    $subject = 'Contact Form Submission from '.$name;
    $body = 'Name: '.$name." \
\
".'Email: '.$email." \
\
".'Phone: '.$phone." \
\
".'Comments: '.$comments;
    $headers = 'From: My Site <'.$to.'>' . "\\r\
" . 'Reply-To: ' . $email;

    $emailSent = mail($to, $subject, $body, $headers);
    }
    if($emailSent) {
?>
    <div class="thanks">
        <h1 style="text-transform: none;">Thanks, <?=$name;?></h1>
        <p>Your email was successfully sent. I will be in touch soon.</p>
    </div>

<?php }
} else { ?>


<div class="contact-form" style="padding:25px; float:left; margin-top: 50px;  background-color: rgba(195,166,96,.13); height: 444px; width: 400px;">
<h1 style="text-align:center; margin-left: -25px; margin-top: -25px; width: 420px; color: white; font: 700 30px/1.1 georgia,'times new roman','sans-serif'; text-transform: uppercase;">Contact Form</h1>
        <form action="<?php the_permalink(); ?>" id="contactForm" method="post">
    
            <ol class="forms" style="margin-top:25px; width:400px; clear:none; margin-left:0">
                <li><label for="contactName">Name</label>
                    <input type="text" name="contactName" id="contactName" class="requiredField" />
                    <?php if($nameError != '') { ?>
                        <span class="error"><?php echo $nameError; ?></span>
                    <?php } ?>
                </li>
                <li><label for="email">Email</label>
                    <input type="text" name="email" id="email" class="requiredField email" />
                    <?php if($emailError != '') { ?>
                        <span class="error"><?php echo $emailError; ?></span>
                    <?php } ?>
                </li>
                <li><label for="phone">Phone</label>
                    <input type="text" name="phone" id="phone" />
                </li>
                <li class="textarea"><label for="commentsText">Comments</label>
                    <textarea name="comments" id="commentsText" rows="10" cols="30" class="requiredField" style="height:150px"></textarea>
                    <?php if($commentError != '') { ?>
                        <span class="error"><?php echo $commentError; ?></span>
                    <?php } ?>
                </li>
                <li class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" />
                <button type="submit" style="float: right; padding-top: 9px; padding-bottom: 8px; padding-left: 6px; padding-right: 6px; font-family: georgia, 'times new roman',sans-serif; 
                background-color: #F47D3B; color: white; text-transform:uppercase">Submit</button></li>
            </ol>
        </form>
</div>    
<?php
}
?>

<?php get_footer(); ?> 


Now, when I submit the blank form, I get "!Error so I gather the $hasError variable is being set to 1, shouldn’t it either be true or false?

First, change $commentError, $emailError,$nameError, to an array

$formError = “You forgot to enter your name”;

and so on, this is a better method than making new variables for every error.

Then inside if ($hasError), do a print_r($formError);

now where getting somewhere,
changed the code to



<?php
/*
Template Name: Bobs Contact Form
*/
?>


<?php get_header(); ?>

<div class="map">
<iframe width="480" height="494" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Napolitano+Realty,+939+Orange+Avenue,+Coronado,+CA+92118-2609&amp;aq=2&amp;sll=37.0625,-95.677068&amp;sspn=40.409448,93.076172&amp;ie=UTF8&amp;hq=Napolitano+Realty,&amp;hnear=939+Orange+Ave,+Coronado,+San+Diego,+California+92118&amp;ll=32.686543,-117.179229&amp;spn=0.006295,0.006295&amp;output=embed"></iframe>
</div>


<?php
$hasError = false;

//If the form is submitted
if(isset($_POST['submitted']))
    {
    //Check to make sure that the name field is not empty
    if(!isset($_POST['contactName'])) {
        $formError[0] = 'You forgot to enter your name.';
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }

    //Check to make sure sure that a valid email address is submitted
    $email = $_POST['email'];
    if(!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        $formError[1] = 'You entered an invalid email address.';
        $hasError = true;
    }
    //Check to make sure comments were entered    
    if(!isset($_POST['comments']))
    {
        $formError[2] = 'You forgot to enter your comments.';
        $hasError = true;
    } else {
        $comments = trim($_POST['comments']);
    }
    //If there is no error, send the email
    if($hasError) {
    echo $hasError;
    echo 'Error';
    print_r($formError);
    } else {
    echo 'email sent';
    $phone = $_POST['phone'];

    $to= 'example@example.com';
    $subject = 'Contact Form Submission from '.$name;
    $body = 'Name: '.$name." \
\
".'Email: '.$email." \
\
".'Phone: '.$phone." \
\
".'Comments: '.$comments;
    $headers = 'From: My Site <'.$to.'>' . "\\r\
" . 'Reply-To: ' . $email;

    $emailSent = mail($to, $subject, $body, $headers);
    }
    if($emailSent) {
?>
    <div class="thanks">
        <h1 style="text-transform: none;">Thanks, <?=$name;?></h1>
        <p>Your email was successfully sent. I will be in touch soon.</p>
    </div>

<?php }
} else { ?>


<div class="contact-form" style="padding:25px; float:left; margin-top: 50px;  background-color: rgba(195,166,96,.13); height: 444px; width: 400px;">
<h1 style="text-align:center; margin-left: -25px; margin-top: -25px; width: 420px; color: white; font: 700 30px/1.1 georgia,'times new roman','sans-serif'; text-transform: uppercase;">Contact Form</h1>
        <form action="<?php the_permalink(); ?>" id="contactForm" method="post">
    
            <ol class="forms" style="margin-top:25px; width:400px; clear:none; margin-left:0">
                <li><label for="contactName">Name</label>
                    <input type="text" name="contactName" id="contactName" class="requiredField" />
                    <?php if($formError[0] != '') { ?>
                        <span class="error"><?php echo $formError[0]; ?></span>
                    <?php } ?>
                </li>
                <li><label for="email">Email</label>
                    <input type="text" name="email" id="email" class="requiredField email" />
                    <?php if($formError[1] != '') { ?>
                        <span class="error"><?php echo $formError[1]; ?></span>
                    <?php } ?>
                </li>
                <li><label for="phone">Phone</label>
                    <input type="text" name="phone" id="phone" />
                </li>
                <li class="textarea"><label for="commentsText">Comments</label>
                    <textarea name="comments" id="commentsText" rows="10" cols="30" class="requiredField" style="height:150px"></textarea>
                    <?php if($formError[3] != '') { ?>
                        <span class="error"><?php echo $formError[3]; ?></span>
                    <?php } ?>
                </li>
                <li class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" />
                <button type="submit" style="float: right; padding-top: 9px; padding-bottom: 8px; padding-left: 6px; padding-right: 6px; font-family: georgia, 'times new roman',sans-serif; 
                background-color: #F47D3B; color: white; text-transform:uppercase">Submit</button></li>
            </ol>
        </form>
</div>    
<?php
}
?>

<?php get_footer(); ?> 

But now when it runs I runs, I get
1ErrorArray ( [1] => You entered an invalid email address. )
but when I fill out the form, I get email sent

This is another candidate for “STOP OPENING AND CLOSING PHP SO MUCH!!!” – slow, needlessly complex, often outright POINTLESS (see your first instance)…

Though I think a lot of your problem is trusting undefined as a result… I’d probably make an empty array $errors, then add your strings to it as associative indexes… that way to test all you do is count($errors)>0

Also, you’re only running your form if $_POST[‘submitted’] – that will never show if submitted with errors. I’d put that form into a function, and call it on each of them as appropriate.

… and swing a MASSIVE axe at all that bloated wasteful presentational markup, list abuse and other nonsensical redundancies. DIV for nothing around it, h1 doing LEGEND’s job, ol for nothing inside it when you’ve already got perfectly good semantic meanings…

Also, the trim could result in empty, so that should be done BEFORE you check if it’s valid, and you’re only checking isset – and an empty result can still be ‘set’ which is why you should check ‘empty’ as well. Also wouldn’t hurt to detect if the values for the form are already set so when there’s an error the user isn’t sent an empty form.


<?php
/*
	Template Name: Bobs Contact Form
*/

$errors=array();

function formPostValueCheck($target) {
	return (
		isset($_POST[$target]) ?
		'value="'.htmlspecialchars($_POST[$target]).'"' :
		''
	);
}

function formErrorCheck($target) {
global $errors;
	return (
		isset($errors[$target]) ?
		'<p class="error">'.$errors[$target].'</p>' :
		''
	);
}

function contactForm() {
global $errors;
echo '
	<form action="',$_SERVER['PHP_SELF'],'" id="contactForm" method="post">
	
		<fieldset>
		
			<legend><span>Contact Form</span></legend>
			
			<label for="contactName">Name</label>
			<input
				type="text"
				name="contactName" id="contactName"
				class="requiredField"
				',formPostValueCheck('contactName'),'
			/>',formErrorCheck('contactName'),'
			<br />
			
			<label for="email">Email</label>
			<input
				type="text"
				name="email" id="email"
				class="requiredField email"
				',formPostValueCheck('email'),'
			/>',formErrorCheck('email'),'
			<br />
			
			<label for="phone">Phone</label>
			<input
				type="text"
				name="phone" id="phone"
				',formPostValueCheck('phone'),'
			/>
			<br />
			
			<label for="commentsText">Comments</label>
			<textarea
				name="comments" id="commentsText"
				rows="10" cols="30"
				class="requiredField"
			>',(
				isset($_POST['comments']) ?
				htmlspecialchars($_POST['comments']) :
				''
			),'</textarea>
			',formErrorCheck('comments'),'
			<br />
			
		</fieldset>
		
		<div class="submitsAndHiddens">
			<!--
				I like to put non-data fields/controls in a div, not a fieldset
			-->
			<input type="hidden" name="submitted" id="submitted" value="true" />
			<button type="submit">Submit</button>
		</div>
		
	</form>';
}

get_header();

if (isset($_POST['submitted'])) {

	/*
		do your TRIM first if possible since it could result in
		an empty string!!! Also, empty results can still be 'set'.
	*/
	
	// Check to make sure that the name field is not empty
	$name=isset($_POST['contactName']) ? trim($_POST['contactName']) : '';
	if (empty($name)) {
		$errors['contactName']='You forgot to enter your name.';
	}

	// Check to make sure sure that a valid email address is submitted
	if (
		isset($_POST['email']) &&
		!filter_var($email=$_POST['email'],FILTER_VALIDATE_EMAIL)
	) $errors['email']='You entered an invalid email address.';
	
	// Check to make sure comments were entered
	$comments=isset($_POST['comments']) ? trim($_POST['comments']) : '';
	if (empty($_POST['comments'])) {
		$errors['comments']='You forgot to enter your comments.';
	}
		
	//If there is no error, send the email
	if (count($errors)==0) {
		echo 'email sent';
		$phone=isset($_POST['phone']) ? $_POST['phone'] : '';
		$to='example@example.com';
		$subject='Contact Form Submission from '.$name;
		
		$body='
Name: '.$name.'

Email: '.$email.'

Phone: '.$phone.'

Comments: '.$comments;

		$headers='From: My Site <'.$to.'>'."\\r\
".'Reply-To: '.$email;

		$emailSent=mail($to, $subject, $body, $headers);
		if ($emailSent) {
			echo '
				<div class="thanks">
					<h1>Thanks, ',$name,'</h1>
					<p>
						Your email was successfully sent. I will be in touch soon.
					</p>
				</div>';
		}
	} else {
		echo '
			<h1>There were errors</h1>';
		contactForm();
	}
	
} else contactForm();

get_footer();

Should do the trick… Oh, and I removed the DIV/IFRAME not just for testing – I’d probably NOT put that on the same page as that form, it makes the reload painful as all git… Seriously, takes so long to load most people probably won’t bother using the form.

Naturally everything else you were doing there belongs in the external stylesheet. Inlining presentation isn’t just a waste of bandwidth, it makes the already painful ‘html in your php’ harder to maintain and makes PHP work harder – since you’re making it output more.

… and remember my mantra; if you’re doing <?php ?> more than once per file, you’re probably doing it all wrong. (I REALLY would like to see <?php ?> removed from PHP entirely so it can behave more like a REAL programming language)

ok, I (once again) made an update to the script, so let me know if its sound
here it is




<?php
/*
Template Name: Bobs Contact Form
*/
?>


<?php get_header(); ?>

<div class="map">
<iframe width="480" height="494" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Napolitano+Realty,+939+Orange+Avenue,+Coronado,+CA+92118-2609&amp;aq=2&amp;sll=37.0625,-95.677068&amp;sspn=40.409448,93.076172&amp;ie=UTF8&amp;hq=Napolitano+Realty,&amp;hnear=939+Orange+Ave,+Coronado,+San+Diego,+California+92118&amp;ll=32.686543,-117.179229&amp;spn=0.006295,0.006295&amp;output=embed"></iframe>
</div>


<?php
$hasError = false;
$name = $_POST['contactName'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];

//If the form is submitted
if(isset($_POST['submitted']))
    {
    //Check to make sure that the name field is not empty
    if(empty($name)) {
        $formError[0] = 'You forgot to enter your name.';
        $hasError = true;
    }
    //Check to make sure sure that a valid email address is submitted
    if(!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        $formError[1] = 'You entered an invalid email address.';
        $hasError = true;
    }
    //Check to make sure comments were entered    
    if(empty($comments))
    {
        $formError[2] = 'You forgot to enter your comments.';
        $hasError = true;
    }
    //If there is no error, send the email
    if(!isset($hasError)) {
    echo 'email sent';
    $to= 'lukemaxpro@excite.com';
    $subject = 'Contact Form Submission from '.$name;
    $body = 'Name: '.$name." \
\
".'Email: '.$email." \
\
".'Phone: '.$phone." \
\
".'Comments: '.$comments;
    $headers = 'From: My Site <'.$to.'>' . "\\r\
" . 'Reply-To: ' . $email;

    $emailSent = mail($to, $subject, $body, $headers);
    }
    if($emailSent) {
?>
    <div class="thanks">
        <h1 style="text-transform: none;">Thanks, <?=$name;?></h1>
        <p>Your email was successfully sent. I will be in touch soon.</p>
    </div>

<?php }
} else { ?>


<div class="contact-form" style="padding:25px; float:left; margin-top: 50px;  background-color: rgba(195,166,96,.13); height: 444px; width: 400px;">
<h1 style="text-align:center; margin-left: -25px; margin-top: -25px; width: 420px; color: white; font: 700 30px/1.1 georgia,'times new roman','sans-serif'; text-transform: uppercase;">Contact Form</h1>
        <form action="<?php the_permalink(); ?>" id="contactForm" method="post">
    
            <ol class="forms" style="margin-top:25px; width:400px; clear:none; margin-left:0">
                <li><label for="contactName">Name</label>
                    <input type="text" name="contactName" id="contactName" class="requiredField" />
                    <?php if(empty($formError[0])) { ?>
                        <span class="error"><?php echo $formError[0]; ?></span>
                    <?php } ?>
                </li>
                <li><label for="email">Email</label>
                    <input type="text" name="email" id="email" class="requiredField email" />
                    <?php if(empty$formError[1])) { ?>
                        <span class="error"><?php echo $formError[1]; ?></span>
                    <?php } ?>
                </li>
                <li><label for="phone">Phone</label>
                    <input type="text" name="phone" id="phone" />
                </li>
                <li class="textarea"><label for="commentsText">Comments</label>
                    <textarea name="comments" id="commentsText" rows="10" cols="30" class="requiredField" style="height:150px"></textarea>
                    <?php if(empty(($formError[2])) { ?>
                        <span class="error"><?php echo $formError[3]; ?></span>
                    <?php } ?>
                </li>
                <li class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" />
                <button type="submit" style="float: right; padding-top: 9px; padding-bottom: 8px; padding-left: 6px; padding-right: 6px; font-family: georgia, 'times new roman',sans-serif; 
                background-color: #F47D3B; color: white; text-transform:uppercase">Submit</button></li>
            </ol>
        </form>
</div>    
<?php
}
?>

<?php get_footer(); ?> 

First, I instanciate all the variables (and the ones submitted from the form).
Then I heck if the $name field has been filled out, if it hasn’t assign a value to the array $formError[0] and set a value to $hasError
Then check if the email address is valid, if it isn’t set a valur to $formError[1] and set a value to $hasError
Then I check to see if any comments have been filled out, if it hsnt, set a value to $formError[2] and also to $hasError
If all three fields are good, then check if the $formError variable has been set, if it hasnt, send an email to me with mail()
Then I check if the email has been sent,and if it has I output athank you div.
Finally if the form hasn’t been submitted I display a form (which check if those 3 fields have been filled out along the way.

Thanks…

thankks deathshadow, used your script
Does this get run cause all I get is emai sent with no thanks div and the email doesn’t seem to go through…


        $emailSent=mail($to, $subject, $body, $headers);
        if ($emailSent) {
            echo '
                <div class="thanks"> 
                    <h1>Thanks, ',$name,'</h1> 
                    <p>
                        Your email was successfully sent. I will be in touch soon.
                    </p> 
                </div>';
        }

Well, we’re missing error conditions from MAIL() – so that’s probably mail dysfunction :smiley:

Try using this instead on that if statement:


	if ($emailSent) {
		echo '
		<div class="thanks">
		<h1>Thanks, ',$name,'</h1>
		<p>
		    Your email was successfully sent. I will be in touch soon.
		</p>
		</div>';
	} else {
		echo '
			<h1>ERROR - Problem with PHP Mail() function</h1>';
			<h2>TO</h2>
			<pre>',print_r($to),'</pre>
			<h2>SUBJECT</h2>
			<pre>',print_r($subject),'</pre>
			<h2>BODY</h2>
			<pre>',print_r($body),'</pre>
			<h2>HEADERS</h2>
			<pre>',print_r($headers),'</pre>';
	}

See if any of those values don’t ‘look right’. Might also help to dig into /var/log/messages or wherever your server is storing it’s logs, to see what it’s saying on that end about what went wrong with mail();

k, got that error thing, but some of the vars look screwey, heres the source code


lukemaxpro@excite.com             
<h1>ERROR - Problem with PHP Mail() function</h1>            
<h2>TO</h2>             
<pre>lukemaxpro@excite.com1</pre>            
 <h2>SUBJECT</h2>            
 <pre>Contact Form Submission from www1</pre>             
<h2>BODY</h2>             
<pre> Name: www Email: lukemaxpro@excite.com Phone: Comments: wwwwwwwwww1</pre>            
 <h2>HEADERS</h2>             
<pre>From: My Site <lukemaxpro@excite.com> Reply-To: lukemaxpro@excite.com1</pre>    

in the first line, I included


echo $to;

where is that 1 coming from (should I be using the . instead of the ,)

printr returns a 1 if the value is set, 0 if it is not… did you strip out the white-space characters when cutting/pasting? If not somethings wrong with the $header var… and $body.

and no, you shouldn’t use string addition on the echo…

Apart from the whitespace characters being nowhere to be found – I can’t see anything else really wrong with the output… you’ll have to dig into your e-mail server’s log files to figure out what’s bombing… you must have stripped them since that 1 should be on it’s own line all by itself – and why I wrap said output in PRE.

I’ll try it on my setup later today and see what it does here.

how do i make sure all the whitespace characters have been stripped out? /I’m using dreamweaver and I dont/cant see them if there hiding.

But thanks for helping…

Did I mention that 99% of the time you load my code into Dreamweaver, hit save, it breaks? That’s an exaggeration, but it’s what it feels like most of the time…

I have a stupid question: what are you running it on? A real server, a local server like XAMPP? Did your old version work and send mails?

The reason I’m asking is I just tested the code on my server, and it works fine… doesn’t work locally on XAMPP, but that’s because I don’t HAVE a local e-mail server running in the test environment. If you don’t have sendmail or some other mail service available in your testing environment, it won’t work. Upload it to a server, runs great.

its a friends server, all he gave me was his username/password for the wordpress login (so I cant get at the servers logs) but I dont like working on a site when I dont have access to the php.ini file or something like that, I also used the script on my server and everything is good. So I guess its the servers issue blocking mail() so hes got to find a way around that.