Jump to content

Project - Contact Form, Check Boxes and Text Input Fields to E-Mail.


bpence

Recommended Posts

Hello All:

 

Trying to work with PHP on a contact form with a jQuery Validation to make certain that the visitors fill out the required information. I'll try to show everything that I have, and then the error I am getting when the visitor hits "submit."

 

I don't know PHP all that well, and trying to learn my way through it. I used a couple of tutorials to add the features I needed and did my own styling on the live site.  Here is the PHP that is currently in the header of my markup:

 

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

//Check to make sure that the First name field is not empty
if(trim($_POST['firstname']) == '') {
	$hasError = true;
} else {
	$firstname = trim($_POST['firstname']);
}
    
    //Check to make sure that the Last name field is not empty
if(trim($_POST['lastname']) == '') {
	$hasError = true;
} else {
	$lastname = trim($_POST['lastname']);
}
    
    //Check to make sure that the Street Address 01 field is not empty
if(trim($_POST['street01']) == '') {
	$hasError = true;
} else {
	$street01 = trim($_POST['street01']);
}
    
    //If Street02 is filled out, give it a value
    
    $street02  = $_POST['street02'];
    
    //Check to make sure that the City field is not empty
if(trim($_POST['city']) == '') {
	$hasError = true;
} else {
	$city = trim($_POST['city']);
}
    
    //Check to make sure that the State field is not empty
if(trim($_POST['state']) == '') {
	$hasError = true;
} else {
	$state = trim($_POST['state']);
}
    
    //Check to make sure that the Zip field is not empty
if(trim($_POST['zip']) == '') {
	$hasError = true;
} else {
	$zip = trim($_POST['zip']);
}
    
    //If Email is filled out, give it a value 
    
    $email  = $_POST['email'];
    
    //If Telephone is filled out, give it a value 
    
    $telephone  = $_POST['telephone'];
    
    //Default Subject Value 
    
    $subject  = "VMC Inquiry";
    
    //Check checkboxes 
    foreach($_POST['check']  as  $value)  {

$check_msg = "Checked: $value\n";

} 

    //If Message is filled out, give it a value 
    
    $comment  = $_POST['comment'];


//If there is no error, send the email
if(!isset($hasError)) {
	$emailTo = 'xxxx.xxxx@gmail.com'; //Put your own email address here
	$body = "Name: $firstname $lastname \n\nStreet Address: $street01 \n\nStreet Address*: $street02 \n\nCity: $city \n\nState: $state \n\nZip: $zip \n\nEmail*: $email \n\nTelephone*: $telephone \n\nCheck Box: $check_msg \n\nMessage:\n $comment";
	$headers = 'From: XXXXX <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

	mail($emailTo, $subject, $body, $headers);
	$emailSent = true;
}
}
?>

 

So basically I am using classes to say whether or not something is required, which ties into the jQuery validation. If it isn't required, whatever the visitor types into the box is put into something like "$telephone" which is then printed in the e-mail.

 

The markup for the forms in the body is the following:

 

<p class="contact-text-right">For more information, or to have a list of our properties mailed to you, please fill out the form below.</p>
    <div id="contact-wrapper">
            <?php if(isset($hasError)) { //If errors are found ?>
        <p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
            <?php } ?>
            <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
            <p class="accept"><strong><?php echo $firstname;?>,Your Email Successfully Sent!</strong></p>	<?php } ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
        <div id="names">
            <label for="firstname"><strong>First Name:</strong></label>
            <input type="text" name="firstname" id="firstname" value="" class="required" />
            <label for="lastname"><strong>Last Name:</strong></label>
            <input type="text" name="lastname" id="lastname" value="" class="required" />
        </div>
        <div id="address01">
            <label for="street01"><strong>Street Address:</strong></label>
            <input type="text" name="street01" id="street01" value="" class="required" />
        </div>
        <div id="address02">
            <label for="street02"><strong>Street Address*:</strong></label>
            <input type="text" name="street02" id="street02" value="" />
        </div>
        <div id="city">
            <label for="city"><strong>City:</strong></label>
            <input type="text" name="city" id="city" value="" class="required city" />
            <label for="state"><strong>State:</strong></label>
            <input type="text" name="state" id="state" value="" class="required state" />
            <label for="zip"><strong>Zip Code:</strong></label>
            <input type="text" name="zip" id="zip" value="" class="required zip" />
        </div>
        <div id="email">
            <label for="email"><strong>E-mail*:</strong></label>
            <input type="text" name="email" id="email" value="" class="email"/>
            <label for="telephone"><strong>Telephone*:</strong></label>
            <input type="text" name="telephone" id="telephone" value="" class="telephone" />
            <p class="bottom">*Optional fields.</p>
        </div>
        <div class="checkbox">
            <p><input  type="checkbox"  name="check[]"  value="properties">XXX</p>  
            <p><input  type="checkbox"  name="check[]"  value="contact-regarding">XXXX</p>
            <p class="indent">(Please list properties or ask specific questions in the space below.)</p>        
        </div>
        <div id="comment">
            <label for="comment"></label>
            <textarea name="comment" id="comment" ></textarea>
        </div>
        <div id="button">
        <input type="submit" value="Send Message" name="submit" />
        </div>
    </form>

 

All seems well until I hit submit on the live site. I think get this error returned to me:

 

Warning: Invalid argument supplied for foreach() in ......./html/development/contact.php on line 64

 

The e-mail goes through, with all the information, no problems! I just can't get this error message to go away.

Line 64 of contact.php is where the checkbox coding is:

 

    //Check checkboxes 
    foreach($_POST['check']  as  $value)  {

$check_msg = "Checked: $value\n";

} 

 

 

I know I've submitted a lot of code here, and tried to narrow you down to the exact spot I think the problem is, but hopefully some of you PHP gurus can pick out the flaw in a heartbeat.

 

I really appreciate all the help.

 

B

 

 

 

Link to comment
Share on other sites

Update:

 

If I remove the check boxes from the PHP and the markup, then upload to the live site, and submit the form, I do not get a error. It has to be with the check boxes:

 

The PHP:

 

    //Check checkboxes 
    foreach($_POST['check']  as  $value)  {

$check_msg = "Checked: $value\n";

} 

 

And the markup:

 

        
<div class="checkbox">
            <p><input  type="checkbox"  name="check[]"  value="properties">Please send me a list of all VMC properties.</p>  
            <p><input  type="checkbox"  name="check[]"  value="contact-regarding">Please contact me with more information regarding:</p>
            <p class="indent">(Please list properties or ask specific questions in the space below.)</p>        
</div>

 

Any thoughts to why there is an error?

Link to comment
Share on other sites

Thanks ProjectFear for your feedback.

 

Nope, I wasn't checking any checkboxes. I don't want to require the visitor to check the checkboxes either. If they do check it, its working right now that in the email it says what value they checked so my client knows what information to send them. So I try to implement your suggestion then, right?

 

B

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.