Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Form with Checkboxes

the mail() function

Moderators: macek, egami, gesf

Form with Checkboxes

Postby kimmibaby » Mon Dec 12, 2011 2:34 am

I have a contact form with checkboxes. Everything else works fine when the email is sent except the checkboxes. It says array instead of the checked options. I originally had code that only displayed one checked option instead of all.

If anyone could help me out and show me what's actually wrong, I'd so much appreciate it! I've been stuck on it for a while.

Code: Select all
<?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@example.com.au";
$subject = "Online Quote Request Form"

/* 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($_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");


/* 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();

?>
 



Code: Select all
<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>
kimmibaby
New php-forum User
New php-forum User
 
Posts: 1
Joined: Mon Dec 12, 2011 2:20 am

Re: Form with Checkboxes

Postby egami » Mon Dec 12, 2011 6:19 am

$ServicesRequired = ''; // set a blank var, otherwise the log file throws an error..

foreach($_POST['ServicesRequired'] as $key => $value) {
$ServicesRequired .= $value."\r\n<br>";
}

There is no need to do any checking on this variable really, since it's already hard coded.
Or, simply check to make sure that at least one service was checked..

if ($ServicesRequired == '') {
throw your error here..
}
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron