Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-06-2011, 08:40 AM   PM User | #1
kimmi_baby
New Coder

 
Join Date: Jun 2010
Posts: 46
Thanks: 10
Thanked 0 Times in 0 Posts
kimmi_baby is an unknown quantity at this point
HTML PHP Email form with Checkboxes

Hi,
Im having some issues with a checkboxes when the email gets sent once the user submits the form.

When I receive the email, it's either empty or it says array. No matter what different code I try.

I'm really hoping someone can help me out! I'll appreciate any help!

Code:
<form name="quote_form" class="quote_form" method="POST" action="testscript.php">


*Name: <input type="text" name="Name" size="50" class="name">
Business/Organisation:	<input type="text" name="BusinessName" size="50" class="BusinessName">
	
*Contact Number:<input type="text" name="ContactPhone" size="50">
		
*Email:	<input type="text" name="Email" size="50">
             
                
Services Required (tick as many as required):
            
<input type="checkbox" name="ServicesRequired[]" value="CustomWebsiteDesign" /> Custom Website Design

              
<input type="checkbox" name="ServicesRequired[]" value="Ecommerce" /> E-Commerce

                
             
<input type="checkbox" name="ServicesRequired[]" value="TemplateDesign" /> Template Design

              
            
<input type="checkbox" name="ServicesRequired[]" value="HostingDomain" /> Hosting/Domain
              
                
               
     
<input type="checkbox" name="ServicesRequired[]" value="WebDevelopment" /> Web Development

                
              
<input type="checkbox" name="ServicesRequired[]" value="Photography" /> Photography

 
Are there any sites you want your website to be similar to? <input type="text" name="SimilarSites" size="50" > 
			
Will own artwork be supplied? (Logos, images etc) <input type="text" name="OwnArtworkSupplied" size="50">

What pages will you require and estimate of how many? <input type="text" name="Pages" size="50">
		
Will you want to update the website content yourself?<input type="text" name="UpdateOwnContent" size="50">
	    
Do you have a time frame?<input type="text" name="TimeFrame" size="50">
		
Do you have a budget you need to work with?<input type="text" name="Budget" size="50">

Enquiry: <textarea rows="8" cols="40" name="Details"></textarea> 

  
    <input type="submit" class="submitquotebutton" name="submit" value="submit!">
    
    </form>
Code:
<?php
/* Set e-mail recipient */
$myemail  = "info@purpleblaze.com.au";

/* Check all form inputs using check_input function */
$Name = check_input($_POST['Name'], "Enter your name");
$ContactPhone = check_input($_POST['ContactPhone'], "Enter your contact number");
$BusinessName = check_input($_POST['BusinessName']);
$Email = check_input($_POST['Email']);
$ServicesRequired = check_input($_POST['ServicesRequired']);
$SimilarSites = check_input($_POST['SimilarSites']);
$OwnArtworkSupplied = check_input($_POST['OwnArtworkSupplied']);
$Pages = check_input($_POST['Pages']);
$UpdateOwnContent = check_input($_POST['UpdateOwnContent']);
$TimeFrame = check_input($_POST['TimeFrame']);
$Budget = check_input($_POST['Budget']);



$Details = check_input($_POST['Details'], "Write your enquiry");

for ($i=0; $i<count($_POST['ServicesRequired']); $i++){ 
$ServicesRequired_req = addslashes($_POST['ServicesRequired'][$i]); 
}





/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $Email))
{
    show_error("E-mail address not valid");
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $Name 
Contact Number: $ContactPhone
Business Name: $BusinessName
E-mail: $email

Services Required : $ServicesRequired

Websites they want their website to be similar to: $SimilarSites

Will own artwork be supplied: $OwnArtworkSupplied

Pages Required and how many: $Pages

Will you want to update the website content yourself? $UpdateOwnContent

Do you have a time frame: $TimeFrame

Budget: $Budget


Enquiry:
$Details

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.php');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>
kimmi_baby is offline   Reply With Quote
Old 12-06-2011, 08:42 AM   PM User | #2
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Is there any text at all?
BluePanther is offline   Reply With Quote
Old 12-06-2011, 09:33 AM   PM User | #3
markspark100
New Coder

 
Join Date: May 2009
Location: Birmingham, England
Posts: 57
Thanks: 5
Thanked 5 Times in 5 Posts
markspark100 is an unknown quantity at this point
Quote:
Originally Posted by kimmi_baby View Post
Code:
$ServicesRequired = check_input($_POST['ServicesRequired']);
You can't do that because $_POST['ServicesRequired'] is an array and your function check_input() uses trim(), htmlspecialchars(), and stripslashes() all of which only accept a string: http://php.net/trim http://php.net/htmlspecialchars http://php.net/stripslashes

If you want to use your function you need to put a check in it to see whether your $data is an array and then loop through each value.

Mark
markspark100 is offline   Reply With Quote
Old 12-06-2011, 09:44 AM   PM User | #4
kimmi_baby
New Coder

 
Join Date: Jun 2010
Posts: 46
Thanks: 10
Thanked 0 Times in 0 Posts
kimmi_baby is an unknown quantity at this point
do you mean something like this?

Code:
foreach($_POST['ServicesRequired'] as $value) {   

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

3 }
kimmi_baby is offline   Reply With Quote
Old 12-06-2011, 10:09 AM   PM User | #5
markspark100
New Coder

 
Join Date: May 2009
Location: Birmingham, England
Posts: 57
Thanks: 5
Thanked 5 Times in 5 Posts
markspark100 is an unknown quantity at this point
Yes you could do that, then run your check_input() on $check_msg.

I think that should work then.

Mark
markspark100 is offline   Reply With Quote
Old 12-06-2011, 10:18 AM   PM User | #6
kimmi_baby
New Coder

 
Join Date: Jun 2010
Posts: 46
Thanks: 10
Thanked 0 Times in 0 Posts
kimmi_baby is an unknown quantity at this point
How would I actually do that? So sorry to be a pain
kimmi_baby is offline   Reply With Quote
Old 12-06-2011, 10:40 AM   PM User | #7
markspark100
New Coder

 
Join Date: May 2009
Location: Birmingham, England
Posts: 57
Thanks: 5
Thanked 5 Times in 5 Posts
markspark100 is an unknown quantity at this point
I haven't tried this but I think it should work.

It's all exactly the same as your original code apart from the highlighted part.

All you're doing is looping through each service and and appending to a string so that you can then run you function on it to return it for the email text.

Code:
<?php
/* Set e-mail recipient */
$myemail  = "info@purpleblaze.com.au";

/* Check all form inputs using check_input function */
$Name = check_input($_POST['Name'], "Enter your name");
$ContactPhone = check_input($_POST['ContactPhone'], "Enter your contact number");
$BusinessName = check_input($_POST['BusinessName']);
$Email = check_input($_POST['Email']);
foreach($_POST['ServicesRequired'] as $value) {   
   $ServicesRequired .= "Checked: $value\n";   
}
$ServicesRequired = check_input($ServicesRequired);
$SimilarSites = check_input($_POST['SimilarSites']);
$OwnArtworkSupplied = check_input($_POST['OwnArtworkSupplied']);
$Pages = check_input($_POST['Pages']);
$UpdateOwnContent = check_input($_POST['UpdateOwnContent']);
$TimeFrame = check_input($_POST['TimeFrame']);
$Budget = check_input($_POST['Budget']);



$Details = check_input($_POST['Details'], "Write your enquiry");

for ($i=0; $i<count($_POST['ServicesRequired']); $i++){ 
$ServicesRequired_req = addslashes($_POST['ServicesRequired'][$i]); 
}





/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $Email))
{
    show_error("E-mail address not valid");
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $Name 
Contact Number: $ContactPhone
Business Name: $BusinessName
E-mail: $email

Services Required : $ServicesRequired

Websites they want their website to be similar to: $SimilarSites

Will own artwork be supplied: $OwnArtworkSupplied

Pages Required and how many: $Pages

Will you want to update the website content yourself? $UpdateOwnContent

Do you have a time frame: $TimeFrame

Budget: $Budget


Enquiry:
$Details

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.php');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>
Mark.
markspark100 is offline   Reply With Quote
Users who have thanked markspark100 for this post:
kimmi_baby (12-06-2011)
Old 12-06-2011, 10:45 AM   PM User | #8
kimmi_baby
New Coder

 
Join Date: Jun 2010
Posts: 46
Thanks: 10
Thanked 0 Times in 0 Posts
kimmi_baby is an unknown quantity at this point
So should I be adding the highlighted part in?
kimmi_baby is offline   Reply With Quote
Old 12-06-2011, 11:06 AM   PM User | #9
markspark100
New Coder

 
Join Date: May 2009
Location: Birmingham, England
Posts: 57
Thanks: 5
Thanked 5 Times in 5 Posts
markspark100 is an unknown quantity at this point
Yes I've just tried it and it works but there are a couple of things wrong

1) in $message you have a variable called $email but it has been set as $Email - note capital E

2) for ($i=0; $i<count($_POST['ServicesRequired']); $i++){
$ServicesRequired_req = addslashes($_POST['ServicesRequired'][$i]);
}
- This part does nothing as $ServicesRequired_req isn't used after that.

So the code which I have tested and works is as follows:

PHP Code:
<?php


/* Functions we used */
function check_input($data$problem='')
{
    
$data trim($data);
    
$data stripslashes($data);
    
$data htmlspecialchars($data);
    if (
$problem && strlen($data) == 0)
    {
        
show_error($problem);
    }
    return 
$data;
}

function 
show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError?>

    </body>
    </html>
<?php
exit();
}

/* Set e-mail recipient */
$myemail  "info@purpleblaze.com.au";

/* Check all form inputs using check_input function */
$Name check_input($_POST['Name'], "Enter your name");
$ContactPhone check_input($_POST['ContactPhone'], "Enter your contact number");
$BusinessName check_input($_POST['BusinessName']);
$Email check_input($_POST['Email']);
foreach(
$_POST['ServicesRequired'] as $value) {   
   
$ServicesRequired .= "Checked: $value\n";   
}
$ServicesRequired check_input($ServicesRequired);
$SimilarSites check_input($_POST['SimilarSites']);
$OwnArtworkSupplied check_input($_POST['OwnArtworkSupplied']);
$Pages check_input($_POST['Pages']);
$UpdateOwnContent check_input($_POST['UpdateOwnContent']);
$TimeFrame check_input($_POST['TimeFrame']);
$Budget check_input($_POST['Budget']);



$Details check_input($_POST['Details'], "Write your enquiry");


/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/"$Email))
{
    
show_error("E-mail address not valid");
}

/* Let's prepare the message for the e-mail */
$message "Hello!

Your contact form has been submitted by:

Name: $Name 
Contact Number: $ContactPhone
Business Name: $BusinessName
E-mail: $Email

Services Required : $ServicesRequired

Websites they want their website to be similar to: $SimilarSites

Will own artwork be supplied: $OwnArtworkSupplied

Pages Required and how many: $Pages

Will you want to update the website content yourself? $UpdateOwnContent

Do you have a time frame: $TimeFrame

Budget: $Budget


Enquiry:
$Details

End of message
"
;

/* Send the message using mail() function */
mail($myemail$subject$message);

/* Redirect visitor to the thank you page */
header('Location: thanks.php');
exit();

?>
markspark100 is offline   Reply With Quote
Users who have thanked markspark100 for this post:
kimmi_baby (12-06-2011)
Old 12-06-2011, 11:13 AM   PM User | #10
kimmi_baby
New Coder

 
Join Date: Jun 2010
Posts: 46
Thanks: 10
Thanked 0 Times in 0 Posts
kimmi_baby is an unknown quantity at this point
Thank you for your reply! I've copied and pasted the code in but it only displays one of the check boxes, not all of the ones I've selected. What have I done wrong? :S
kimmi_baby is offline   Reply With Quote
Reply

Bookmarks

Tags
form, html, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:10 PM.


Advertisement
Log in to turn off these ads.