Web Design Forum: PHP contact form - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

PHP contact form Coding error Rate Topic: -----

#1 User is offline   Twan 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 44
  • Joined: 29-March 11
  • Reputation: 2
  • Gender:Male
  • Experience:Beginner
  • Area of Expertise:Designer

Posted 01 February 2012 - 07:59 AM

Hi all,

I have created a working form using both jquery and php validation.

I only know the bare minimum on PHP and have used a script from this guy - http://www.benjamink...php_validation/

Here is my php code:

<?php

	$myemail = 'my@mail.com';//<-----Put Your email address here.
	$title = 'Title';
	$name = $_POST['name'];
	$company = $_POST['company'];
	$house = $_POST['house'];
	$street = $_POST['street'];
	$postcode = $_POST['postcode'];
	$daynum = $_POST['daynum'];
	$evenum = $_POST['evenum'];
	$email = $_POST['email'];
	$how = $_POST['how'];
	$information = $_POST['information'];
	
	foreach($_POST['product'] as $value) {
		$product_msg .= " - $value";
	} 
	
$errors = array(); // set the errors array to empty, by default
$fields = array(); // stores the field values
$success_message = "";

if (isset($_POST['submit']))
{
  // import the validation library
  require("validation.php");

  $rules = array(); // stores the validation rules

  // standard form fields
  
  $rules[] = "required,name,Name is required.";
  $rules[] = "required,house,House number is required.";
  $rules[] = "required,street,Street is required.";
  $rules[] = "required,postcode,Post Code is required.";
  $rules[] = "required,daynum,Daytime Number is required.";

  $errors = validateFields($_POST, $rules);

  // if there were errors, re-populate the form fields
  if (!empty($errors))
  {  
    $fields = $_POST;
  }
  
  // no errors! redirect the user to the thankyou page (or whatever)
  else 
  {
    
    // here you would either email the form contents to someone or store it in a database. 
    // To redirect to a "thankyou" page, you'd just do this:
    // header("Location: thanks.php");

	$to = $myemail; 
	$email_subject = "Quote: $name";
	$email_body = "You have received a new quote from $name. ".
	" Here are the details:\n \n Name: $name\n \n Company: $company\n \n House: $house\n \n Street: $street\n \n Postcode: $postcode\n \n Daytime Number: $daynum\n \n Evening Number: $evenum\n \n Email: $email\n \n Products: $product_msg\n \n Additional Information: $information\n \n How: $how";
	
	$headers = "From: $title\n"; 
	$headers .= "Reply-To: $email";
	
	mail($to,$email_subject,$email_body,$headers);
	//redirect to the 'thank you' page
	header('Location: thank-you.html');
  }
} 
          
?>


firstly i get the error - Warning: Invalid argument supplied for foreach()on line 16

I know it is referring to the code snippet for gathering the product checkbox values but I do not know why it is outputting this error.

If anyone could help me it would be greatly appreciated as it is the final bug before the project is finished.

Also any tips on general php contact form handler goodness would be great!

Thank you all!
0

#2 User is offline   simplypixie 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 62
  • Joined: 23-November 11
  • Reputation: 11
  • Gender:Female
  • Location:Shropshire
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 01 February 2012 - 09:38 AM

Can you show the snippet of your form that posts the product array?
0

#3 User is offline   GalaxyTramp 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 120
  • Joined: 09-December 11
  • Reputation: 16
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 01 February 2012 - 09:52 AM

You are getting this error because your POST value "($_POST['product'] as $value)" is not an array. You need to put all your POST values into an ARRAY before you can loop through them.

We need to see the code for your form as simplypixie says.
0

#4 User is offline   Twan 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 44
  • Joined: 29-March 11
  • Reputation: 2
  • Gender:Male
  • Experience:Beginner
  • Area of Expertise:Designer

Posted 01 February 2012 - 09:56 AM

View PostGalaxyTramp, on 01 February 2012 - 09:52 AM, said:

You are getting this error because your POST value "($_POST['product'] as $value)" is not an array. You need to put all your POST values into an ARRAY before you can loop through them.

We need to see the code for your form as simplypixie says.


Ahhh okay, thank you.

I followed a tutorial online and it worked previously before I added the validation.
It still sends the email with the correct values, just has that error message at the top of the screen.

heres my code snippet for that part of the form -

	<div id="product">
								
		<div class="radio-btn">
			<label for="conservatory">Conservatory</label>
			<input type="checkbox" value="conservatory" id="conservatory" name="product[]" validate="required:true"/>
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="windows">Windows</label>
			<input type="checkbox" value="windows" name="product[]" />
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="fascias">Fascias</label>
			<input type="checkbox" value="fascias" name="product[]" />
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="doors">Doors</label>
			<input type="checkbox" value="doors" name="product[]" />
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="rooftrim">Rooftrim</label>
			<input type="checkbox" value="rooftrim" name="product[]" />
		</div><!--radio-btn-->
		
	</div><!--product-->


Thank you.
0

#5 User is offline   GalaxyTramp 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 120
  • Joined: 09-December 11
  • Reputation: 16
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 01 February 2012 - 09:58 AM

Quote

Can you show the snippet of your form that posts the product array?


:)
0

#6 User is offline   Twan 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 44
  • Joined: 29-March 11
  • Reputation: 2
  • Gender:Male
  • Experience:Beginner
  • Area of Expertise:Designer

Posted 01 February 2012 - 10:07 AM

View PostGalaxyTramp, on 01 February 2012 - 09:58 AM, said:

:)


Sorry, I am not sure which part exactly to show you as like i said, not too great with the PHP.

So ive just posted in the whole form :) sorry.

    <form action="<?=$_SERVER['PHP_SELF']?>" method="post" id="contact-form" class="cmxform">
    
    <?php
    
    // if $errors is not empty, the form must have failed one or more validation 
    // tests. Loop through each and display them on the page for the user
    if (!empty($errors))
    {
      echo "<div class='phperror'>Please fix the following errors:\n<ul>";
      foreach ($errors as $error)
        echo "<li>$error</li>\n";
    
      echo "</ul></div>"; 
    }
    
    if (!empty($message))
    {
      echo "<div class='notify'>$success_message</div>";
    }
    ?>
    
    <fieldset>    
		<label for="name">Name:*</label>
		<label for="name" class="error">Please enter your name.</label>
		<input class="text" type="text" id="name" name="name" value="<?=$fields['name']?>"/>
    	        <label for="company">Company Name:</label>
		<input class="text" type="text" id="company" name="company">
		<label for="house">House Number:*</label>
		<label for="house" class="error">Please enter your house number.</label>
		<input class="text" type="text" id="house" name="house" value="<?=$fields['house']?>">
		<label for="street">Street Name:*</label>
		<label for="street" class="error">Please enter your street name.</label>
		<input class="text" type="text" id="street" name="street" value="<?=$fields['street']?>">
		<label for="postcode">Post Code:*</label>
		<label for="postcode" class="error">Please enter your post code.</label>
		<input class="text" type="text" id="postcode" name="postcode" value="<?=$fields['postcode']?>">
		<label for="daynum">Daytime Number:*</label>
		<label for="daynum" class="error">Please enter your number.</label>
		<input class="text" type="text" id="daynum" name="daynum" value="<?=$fields['daynum']?>">		
		<label for="evenum">Evening Number:</label>
		<input class="text" type="text" id="eve-num" name="evenum">		
		<label for="email">Email Address:</label>
		<input class="text" type="text" id="email" name="email">
	</fieldset>
	
	<fieldset>
	
	<label>Product:</label>	
	<div id="product">
								
		<div class="radio-btn">
			<label for="conservatory">Conservatory</label>
			<input type="checkbox" value="conservatory" id="conservatory" name="product[]" validate="required:true"/>
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="windows">Windows</label>
			<input type="checkbox" value="windows" name="product[]" />
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="fascias">Fascias</label>
			<input type="checkbox" value="fascias" name="product[]" />
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="doors">Doors</label>
			<input type="checkbox" value="doors" name="product[]" />
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="rooftrim">Rooftrim</label>
			<input type="checkbox" value="rooftrim" name="product[]" />
		</div><!--radio-btn-->
		
	</div><!--product-->
	
	<label for="information">Additional Information:</label>
	<textarea id="information" name="information"></textarea>
	
</fieldset>

<fieldset>
	
	<p>How Did You Hear About Us?</p>
		
	<div id="how">
			
		<div class="radio-btn">
			<label for="internet">Internet</label>
			<input type="radio" value="internet" id="internet" name="how"/>
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="letter">Marketing Letter</label>
			<input type="radio" value="letter" id="letter" name="how" />
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="referral">Referral</label>
			<input type="radio" value="referral" id="referral" name="how" />
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="advertising">Advertising</label>
			<input type="radio" value="advertising" id="advertising" name="how" />
		</div><!--radio-btn-->
			
		<div class="radio-btn">
			<label for="vans">Our Vans</label>
			<input type="radio" value="vans" id="vans" name="how" />
		</div><!--radio-btn-->
	
	</div><!--how-->
	
	<p class="submit">Once we have received your details we will call you to arrange a convenient time</p>
	
</fieldset>
						
<input type="submit" name="submit" value="SUBMIT" />
    
</form>

0

#7 User is offline   GalaxyTramp 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 120
  • Joined: 09-December 11
  • Reputation: 16
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 01 February 2012 - 10:13 PM

<?php
if (isset($_POST['submit']))
{
        $myemail = 'my@mail.com';//<-----Put Your email address here.
        $title = 'Title';
        $name = $_POST['name'];
        $company = $_POST['company'];
        $house = $_POST['house'];
        $street = $_POST['street'];
        $postcode = $_POST['postcode'];
        $daynum = $_POST['daynum'];
        $evenum = $_POST['evenum'];
        $email = $_POST['email'];
        $how = $_POST['how'];
        $information = $_POST['information'];
        
        foreach($_POST['product'] as $value) {
                $product_msg .= " - $value";
        } 


Try this
1

#8 User is offline   Twan 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 44
  • Joined: 29-March 11
  • Reputation: 2
  • Gender:Male
  • Experience:Beginner
  • Area of Expertise:Designer

Posted 02 February 2012 - 06:49 AM

View PostGalaxyTramp, on 01 February 2012 - 10:13 PM, said:

<?php
if (isset($_POST['submit']))
{
        $myemail = 'my@mail.com';//<-----Put Your email address here.
        $title = 'Title';
        $name = $_POST['name'];
        $company = $_POST['company'];
        $house = $_POST['house'];
        $street = $_POST['street'];
        $postcode = $_POST['postcode'];
        $daynum = $_POST['daynum'];
        $evenum = $_POST['evenum'];
        $email = $_POST['email'];
        $how = $_POST['how'];
        $information = $_POST['information'];
        
        foreach($_POST['product'] as $value) {
                $product_msg .= " - $value";
        } 


Try this


Thank you, worked a treat :)

Contact form works great now and is ready to send out but if anyone can see any improvements that could be made that would be very much appreciated. At the moment it says from an unknown sender and I'm not sure that looks too professional.

Thanks again.
0

#9 User is offline   Twan 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 44
  • Joined: 29-March 11
  • Reputation: 2
  • Gender:Male
  • Experience:Beginner
  • Area of Expertise:Designer

Posted 02 February 2012 - 08:22 AM

Sorry, I take that back.
The error does not appear when the page loads but now appears when the form validates.
:(
0

#10 User is offline   GalaxyTramp 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 120
  • Joined: 09-December 11
  • Reputation: 16
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 02 February 2012 - 09:11 AM

View PostTwan, on 02 February 2012 - 08:22 AM, said:

Sorry, I take that back.
The error does not appear when the page loads but now appears when the form validates.
:(


<?php
if (isset($_POST['submit']))
{
	

        $myemail = 'my@mail.com';//<-----Put Your email address here.
        $title = 'Title';
        $name = $_POST['name'];
        $company = $_POST['company'];
        $house = $_POST['house'];
        $street = $_POST['street'];
        $postcode = $_POST['postcode'];
        $daynum = $_POST['daynum'];
        $evenum = $_POST['evenum'];
        $email = $_POST['email'];
        $how = $_POST['how'];
        $information = $_POST['information'];
		
			Print_r ($_POST['product']);// Lets see what if anything that array contains

0

#11 User is offline   Twan 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 44
  • Joined: 29-March 11
  • Reputation: 2
  • Gender:Male
  • Experience:Beginner
  • Area of Expertise:Designer

Posted 02 February 2012 - 09:31 AM

View PostGalaxyTramp, on 02 February 2012 - 09:11 AM, said:

<?php
if (isset($_POST['submit']))
{
	

        $myemail = 'my@mail.com';//<-----Put Your email address here.
        $title = 'Title';
        $name = $_POST['name'];
        $company = $_POST['company'];
        $house = $_POST['house'];
        $street = $_POST['street'];
        $postcode = $_POST['postcode'];
        $daynum = $_POST['daynum'];
        $evenum = $_POST['evenum'];
        $email = $_POST['email'];
        $how = $_POST['how'];
        $information = $_POST['information'];
		
			Print_r ($_POST['product']);// Lets see what if anything that array contains



This is what I got.

Array ( [0] => conservatory [1] => windows [2] => fascias [3] => doors [4] => rooftrim )
Warning: Cannot modify header information - headers already sent by (output started at /home2/xxxxxxx/public_html/client/contact.php:19) in /home2/xxxxxxx/public_html/client/contact.php on line 66
0

#12 User is offline   GalaxyTramp 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 120
  • Joined: 09-December 11
  • Reputation: 16
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 02 February 2012 - 03:21 PM

View PostTwan, on 02 February 2012 - 09:31 AM, said:

This is what I got.

Array ( [0] => conservatory [1] => windows [2] => fascias [3] => doors [4] => rooftrim )
Warning: Cannot modify header information - headers already sent by (output started at /home2/xxxxxxx/public_html/client/contact.php:19) in /home2/xxxxxxx/public_html/client/contact.php on line 66


OK thats good the array is not empty. Take no notice of the header warning, we are getting this because we are outputting the array content on the page before the headers are sent.

I will take another look when I get a moment.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users