HTML and PHP Form with Checkboxes

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 10791 Views - Last Post: 06 December 2011 - 05:10 AM Rate Topic: -----

#1 kimmi_baby  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 18-April 11

HTML and PHP Form with Checkboxes

Posted 05 December 2011 - 07:44 PM

Hi,
I have a question about checkboxes.

I have a html/PHP form that has a few questions and also check boxes.

I'm having issues when I select multiple check boxes, it only submits one on the options in the email.

Can someone please help me with this?

<?php
/* Set e-mail recipient */
$myemail  = "[email protected]";

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




$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 
Business Name: $BusinessName
Contact Number: $ContactPhone
E-mail: $Email
Services Required: $ServicesRequired

Websites similar to what you want: $SimilarSites

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();
}
?>



Services Required is the checkboxes.

[code]
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">

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">
		
<textarea rows="8" cols="40" name="Details"></textarea> 

    <input type="submit" class="submitquotebutton" name="submit" value="submit!">
    
    </form>


[/code]

Is This A Good Question/Topic? 0
  • +

Replies To: HTML and PHP Form with Checkboxes

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6543
  • View blog
  • Posts: 30,648
  • Joined: 10-May 07

Re: HTML and PHP Form with Checkboxes

Posted 05 December 2011 - 07:45 PM

You will need to parse in the entire checkbox array : ServicesRequired, & parse the elements to see which hold a value of checked.
Was This Post Helpful? 0
  • +
  • -

#3 kimmi_baby  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 18-April 11

Re: HTML and PHP Form with Checkboxes

Posted 05 December 2011 - 07:50 PM

Thank you for your reply. Would doing this change the way I'd need to display the message in the email?
Was This Post Helpful? 0
  • +
  • -

#4 kimmi_baby  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 18-April 11

Re: HTML and PHP Form with Checkboxes

Posted 05 December 2011 - 08:06 PM

I've tried using:

foreach($_POST['ServicesRequired'] as $value) { 
$check_msg .= "Checked: $value\n"; 
} 



I get the following error message:

Warning: Invalid argument supplied for foreach() in /home/purpleb/public_html/website/testscript.php on line 17

Warning: Cannot modify header information - headers already sent by (output started at /home/purpleb/public_html/website/testscript.php:17) in /home/purpleb/public_html/website/testscript.php on line 63
Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: HTML and PHP Form with Checkboxes

Posted 05 December 2011 - 09:01 PM

If you need PHP to parse treat values are arrays then you must add [] after the element's name. eg.
<input type="checkbox" name="example[]" value="1">
<input type="checkbox" name="example[]" value="2">
<input type="checkbox" name="example[]" value="3">



When this is POSTed you can treat it as an array.
foreach($_POST['example'] as $value)
{
    // ... code
}



In your code PHP wasn't treating your checkboxes as an array of values so therefore foreach will output the Invalid argument... warning.

As for your Cannot modify information... warning you cannot output anything before sending headers or starting sessions. This includes echos, prints, var_dumps, print_r etc... Even if PHP outputs an error it counts as output. Revise your code so you don't need to output anything before calling the header function.
Was This Post Helpful? 0
  • +
  • -

#6 kimmi_baby  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 18-April 11

Re: HTML and PHP Form with Checkboxes

Posted 06 December 2011 - 12:53 AM

View Postcodeprada, on 06 December 2011 - 04:01 AM, said:

If you need PHP to parse treat values are arrays then you must add [] after the element's name. eg.
<input type="checkbox" name="example[]" value="1">
<input type="checkbox" name="example[]" value="2">
<input type="checkbox" name="example[]" value="3">



When this is POSTed you can treat it as an array.
foreach($_POST['example'] as $value)
{
    // ... code
}



In your code PHP wasn't treating your checkboxes as an array of values so therefore foreach will output the Invalid argument... warning.

As for your Cannot modify information... warning you cannot output anything before sending headers or starting sessions. This includes echos, prints, var_dumps, print_r etc... Even if PHP outputs an error it counts as output. Revise your code so you don't need to output anything before calling the header function.



Thanks for your reply. I added the [] and now when I receive the email it says Services Required: Array.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4125
  • View blog
  • Posts: 13,016
  • Joined: 08-June 10

Re: HTML and PHP Form with Checkboxes

Posted 06 December 2011 - 02:36 AM

View Postkimmi_baby, on 06 December 2011 - 08:53 AM, said:

Thanks for your reply. I added the [] and now when I receive the email it says Services Required: Array.

that’s correct. the string representation of an array is "Array". that is because you want to print an array directly. if you want to print each value, you can use the implode() function.

PS. checkboxes and radio buttons are only submitted when they are checked.
Was This Post Helpful? 0
  • +
  • -

#8 DeWire  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-December 08

Re: HTML and PHP Form with Checkboxes

Posted 06 December 2011 - 03:12 AM

View PostDormilich, on 06 December 2011 - 03:36 AM, said:

PS. checkboxes and radio buttons are only submitted when they are checked.


Which means you should make sure that you are working with an array before you do your implode - use is_array() or isset()

This post has been edited by DeWire: 06 December 2011 - 03:19 AM

Was This Post Helpful? 0
  • +
  • -

#9 kimmi_baby  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 18-April 11

Re: HTML and PHP Form with Checkboxes

Posted 06 December 2011 - 04:37 AM

I'm using the following code but it still isn't working... Any suggestions? Thanks for everyones replies so far

<?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  = "[email protected]"; 

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

?> 

Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4125
  • View blog
  • Posts: 13,016
  • Joined: 08-June 10

Re: HTML and PHP Form with Checkboxes

Posted 06 December 2011 - 05:00 AM

what is not working? it doesn’t help us if we’re not told what exactly is going wrong.
Was This Post Helpful? 0
  • +
  • -

#11 kimmi_baby  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 18-April 11

Re: HTML and PHP Form with Checkboxes

Posted 06 December 2011 - 05:02 AM

Sorry about that!

It now just says the last checkbox that was selected. It doesn't list all of them..
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4125
  • View blog
  • Posts: 13,016
  • Joined: 08-June 10

Re: HTML and PHP Form with Checkboxes

Posted 06 December 2011 - 05:04 AM

maybe you have forgotten to update the HTML adding [] to the checkbox names.

besides that you should get a couple of warnings since you try to append strings to a non-existant variable (line 41)

This post has been edited by Dormilich: 06 December 2011 - 05:05 AM

Was This Post Helpful? 0
  • +
  • -

#13 kimmi_baby  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 18-April 11

Re: HTML and PHP Form with Checkboxes

Posted 06 December 2011 - 05:05 AM

I've definitely done that :( Just checked it...
Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4125
  • View blog
  • Posts: 13,016
  • Joined: 08-June 10

Re: HTML and PHP Form with Checkboxes

Posted 06 December 2011 - 05:06 AM

see part 2 of the updated post
Was This Post Helpful? 0
  • +
  • -

#15 kimmi_baby  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 18-April 11

Re: HTML and PHP Form with Checkboxes

Posted 06 December 2011 - 05:08 AM

I haven't received any warnings.. Could that be the problem?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2