Jump to content

Request Form with Checkboxes


Wolverine68

Recommended Posts

I created a request form with checkboxes. No matter which box or boxes are checked, when the e-mail is sent, what shows up in the e-mail says "I'd like more information on the following programs: Array"

 

I've pasted the code from the html form and php form below:

[b]HTML form[/b]

<body background="#EEEEEE">
<img src="pics\um-cross3.gif" height="100" width="100"><img src="pics\title.gif"><br>
<h3 align="center">Information Request Form</h3>
<div>
<form action="cgi-bin/feedback5.php" method="post">

<p><font color="blue">Thank you for visiting our website. If you would like more information, please fill out all the information in the form below. When finished, click on the "Submit" button. Be sure to include your address, e-mail, or phone so that someone can follow up with you.</font></p>
<hr width="100%">
<p>Name:&nbsp&nbsp&nbsp&nbsp&nbsp<INPUT TYPE="text" SIZE="35" name="name"></p>
<p>E-mail:&nbsp&nbsp&nbsp&nbsp<INPUT TYPE="text" SIZE="35" name="email"></p>
<p>Phone:&nbsp&nbsp&nbsp&nbsp<INPUT TYPE="text" SIZE="35" name="phone"></p>
<p>Address:   <INPUT TYPE="text" SIZE="35" name="address"></p><br>
I would like more information on the following (check all that apply):<br>
<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Adult Sunday School">Adult Sunday School<br>
<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Bible Studies"">Bible Studies<br>
<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Children's Programs">Children's programs<br>
<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Missions">Missions<br>
<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Music">Music/Choir<br>
<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Youth">Youth group<br><br>
Please add any additional comments or questions in the box below:<br>
<TEXTAREA NAME="comments" ROWS=10 COLS=60>
</TEXTAREA>
<br><br>
<input type="submit" name="submit" value="Submit"><br<br>
<hr width="100%">

</div>
</body>

</html>
____________________________

[b]PHP CODE[/b]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<body>

<?php
$formBody="Name:$name\nEmail:$email\nPhone:$phone\nAddress:$address\nI'd like more information on the following programs: $Programs\nComments:$comments";
$headers = "From:$email";

if ($submit) {
mail("somename@someplace.com", "Church Information Request",$formBody, $headers);
}
if ($submit) {
print "Thank you. Your request has been submitted";
}


?>

</body>
</html>
_______________________

 

Anyone have suggestions?

Link to comment
Share on other sites

This is because the data being sent is still in the form of an array.

 

<form action="myform.php" method="POST">
  <input type="checkbox" name="Program[]" value="Value 1" />
  <input type="checkbox" name="Program[]" value="Value 2" />
</form>

 

<?php
print $_POST['Program'][0]; // Value 1
print $_POST['Program'][1]; // Value 2
?>

 

 

Or you could return every value which exists in the array by imploding them into a string:

 

<?php
print implode(', ', $_POST['Program']); // Value 1, Value 2
?>

Link to comment
Share on other sites

Like previous poster said, the checkboxes are coming in an array.

 

You have to loop through an array or hit the values directly using the index of the array.

 

Check out the following in the PHP manual

 

foreach()

while()

for()

 

Those are 3 loops that will allow you to loop through the results of an array. They are similar in how they work, but different in how they are used and called.

 

Link to comment
Share on other sites

It isn't strictly necessary to use a loop, however it would still work. Although, a loop could also put more strain on the server (not much) but regardless, there is no need especially when it's not necessary. But choose your preferred method.

Link to comment
Share on other sites

Right Wolphie,

 

I mentioned that you have to loop OR hit it using the index which is what you showed.

 

Sometimes calling it using the index isn't feasible and a loop is the only way to go. Especially if there are an indeterminate number of array items.

 

Not trying to negate what you said, just trying to help show all options sir.

 

Nate

Link to comment
Share on other sites

Look at what I have now:

 

HTML form

<html>

 

<head>

<title>Request Form</title>

</head>

 

<body background="#EEEEEE">

<img src="pics\um-cross3.gif" height="100" width="100"><img src="pics\title.gif"><br>

<h3 align="center">Information Request Form</h3>

<div>

<form action="cgi-bin/feedback6.php" method="post">

 

<p><font color="blue">Thank you for visiting our website. If you would like more information, please fill out all the information in the form below. When finished, click on the "Submit" button. Be sure to include your address, e-mail, or phone so that someone can follow up with you.</font></p>

<hr width="100%">

<p>Name:&nbsp&nbsp&nbsp&nbsp&nbsp<INPUT TYPE="text" SIZE="35" name="name"></p>

<p>E-mail:&nbsp&nbsp&nbsp&nbsp<INPUT TYPE="text" SIZE="35" name="email"></p>

<p>Phone:&nbsp&nbsp&nbsp&nbsp<INPUT TYPE="text" SIZE="35" name="phone"></p>

<p>Address:  <INPUT TYPE="text" SIZE="35" name="address"></p><br>

I would like more information on the following (check all that apply):<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Adult Sunday School">Adult Sunday School<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Bible Studies"">Bible Studies<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Children's Programs">Children's programs<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Missions">Missions<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Music">Music/Choir<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Youth">Youth group<br><br>

Please add any additional comments or questions in the box below:<br>

<TEXTAREA NAME="comments" ROWS=10 COLS=60>

</TEXTAREA>

<br><br>

<input type="submit" name="submit" value="Submit"><br<br>

<hr width="100%">

 

</div>

</body>

 

</html>

________________________

 

PHP Code:

 

<html>

<body>

<?php

$formBody="Name:$name\nEmail:$email\nPhone:$phone\nAddress:$address\nI'd like more information on the

 

following programs: $_POST['Programs'][0, 5]\nComments:$comments";

$headers = "From:$email";

 

if ($submit) {

mail("KenB624@yahoo.com", "Church Information Request",$formBody, $headers);

}

if ($submit) {

print "Thank you. Your request has been submitted";

}

 

 

?>

 

</body>

</html>

_________________

 

Upon submission, I get the following error:  "Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /u239/bm0umc0o/cgi-bin/feedback6.php on line 6"

 

 

 

Link to comment
Share on other sites

Please use code tags when posting so it makes it easier to view.....

 

Looks like the error is here following programs: $_POST['Programs'][0, 5]

 

I believe that is line 6... .there should be no , in the second set of [].

 

If your trying to do a 0  through 5 then a while loop or just hand code them

 

<?php

$x  = 0; 
while($x < 6)
{
   $_POST['Programs'][$x];
   $x++;
}

?>

 

or

 

<?php
$checkbox0 = $_POST['Programs'][0];
$checkbox1 = $_POST['Programs'][1];
$checkbox2 = $_POST['Programs'][2];
$checkbox3 = $_POST['Programs'][3];
$checkbox4 = $_POST['Programs'][4];
$checkbox5 = $_POST['Programs'][5];
?>

Link to comment
Share on other sites

Ok, it's working.  Here's how I did it:

 

PHP Code:

 

<html>

<body>

<?php

$checkbox0 = $_POST['Programs'][0];

$checkbox1 = $_POST['Programs'][1];

$checkbox2 = $_POST['Programs'][2];

$checkbox3 = $_POST['Programs'][3];

$checkbox4 = $_POST['Programs'][4];

$checkbox5 = $_POST['Programs'][5];

?>

<?php

$formBody="Name:$name\nEmail:$email\nPhone:$phone\nAddress:$address\nI'd like more information on the

 

following programs:$checkbox0, $checkbox1, $checkbox2, $checkbox3, $checkbox4,

 

$checkbox5\nComments:$comments";

$headers = "From:$email";

 

if ($submit) {

mail("KenB624@yahoo.com", "Church Information Request",$formBody, $headers);

}

if ($submit) {

print "Thank you. Your request has been submitted";

}

 

 

?>

 

</body>

</html>

_____________________

 

However, when the form is submitted and the e-mail arrives, the print out for the checkboxes is like so:

 

"I'd like more information on the following programs:  Adult Sunday School, Bible Studies,    ,  ,  ,  "

 

In the above instance, the first two boxes were checked and the last four left blank, but three additional commas appear as if it is recognizing the boxes that weren't checked. Why? 

 

 

 

 

Link to comment
Share on other sites

Wolphie,

 

One solution you suggested was the implode:

 

<?php

print implode(', ', $_POST['Program']); // Value 1, Value 2

 

?>

 

If I coded it like that, then it would have printed the values of the checked boxes in the browser.  How would I incorporate that into a string variable so that it gets sent in the e-mail?

Link to comment
Share on other sites

Spoke a little too soon.  Now, no matter which boxes are checked, the body of the e-mail that is received looks like this:

 

I'd like more information on the following programs: ,  ,  ,  , ,

 

<html>

<body>

<?php

$checkbox0 = $_POST['Programs'][0];

$checkbox1 = $_POST['Programs'][1];

$checkbox2 = $_POST['Programs'][2];

$checkbox3 = $_POST['Programs'][3];

$checkbox4 = $_POST['Programs'][4];

$checkbox5 = $_POST['Programs'][5];

?>

<?php

$formBody="Name:$name\nEmail:$email\nPhone:$phone\nAddress:$address\nI'd like more information on the

 

following programs: $checkbox0, $checkbox1, $checkbox2, $checkbox3, $checkbox4, $checkbox5

 

\nComments:$comments";

$headers = "From:$email";

 

if ($submit) {

mail("someemail@yahoo.com", "Church Information Request",$formBody, $headers);

}

if ($submit) {

print "Thank you. Your request has been submitted";

}

 

 

?>

 

</body>

</html>

________________________________

 

<html>

 

<head>

<title>Request Form</title>

</head>

 

<body background="#EEEEEE">

<img src="pics\um-cross3.gif" height="100" width="100"><img src="pics\title.gif"><br>

<h3 align="center">Information Request Form</h3>

<div>

<form action="cgi-bin/feedback.php" method="post">

 

<p><font color="blue">Thank you for visiting our website. If you would like more information, please fill out all the information in the form below. When finished, click on the "Submit" button. Be sure to include your address, e-mail, or phone so that someone can follow up with you.</font></p>

<hr width="100%">

<p>Name:&nbsp&nbsp&nbsp&nbsp&nbsp<INPUT TYPE="text" SIZE="35" name="name"></p>

<p>E-mail:&nbsp&nbsp&nbsp&nbsp<INPUT TYPE="text" SIZE="35" name="email"></p>

<p>Phone:&nbsp&nbsp&nbsp&nbsp<INPUT TYPE="text" SIZE="35" name="phone"></p>

<p>Address:  <INPUT TYPE="text" SIZE="35" name="address"></p><br>

I would like more information on the following (check all that apply):<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Adult Sunday School">Adult Sunday School<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Bible Studies"">Bible Studies<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Children's Programs">Children's programs<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Missions">Missions<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Music">Music/Choir<br>

<INPUT TYPE="checkbox" NAME="Programs[]" VALUE="Youth">Youth group<br><br>

Please add any additional comments or questions in the box below:<br>

<TEXTAREA NAME="comments" ROWS=10 COLS=60>

</TEXTAREA>

<br><br>

<input type="submit" name="submit" value="Submit"><br<br>

<hr width="100%">

 

</div>

</body>

 

</html>

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.