Forum Moderators: coopster

Message Too Old, No Replies

Submitting checkbox values from a HTML feedback form

         

garion1

11:50 am on Oct 4, 2008 (gmt 0)

10+ Year Member



I have tried a few tutorials on the web, but cannot get the checkboxes to work. I have the following HTML form:

<form method="post" action="contact.php">
<table width="600" align=left>
<tr>
<td width="62" class="bodytextbold">Name:</td>
<td width="377"><input name="Name" class="bodytext" size=50 maxlength="50"></td>
</tr>
<tr>
<td class="bodytextbold">Email:</td>
<td><input name="Email" class="bodytext" size=50 maxlength="50"></td>
</tr>
<tr>
<td class="bodytextbold">Company:</td>
<td><input name="Company" class="bodytext" size=50 maxlength="50"></td>
</tr>
<tr>
<td class="bodytextbold">Phone:</td>
<td><input name="Phone" class="bodytext" size=50 maxlength="50"></td>
</tr>
<tr>
<td class="bodytextbold">Installation:</td>
<td>
<table class="bodytext">
<tr>
<td>Building new house:</td>
<td><input type="radio" name="Installation" value="New"></td>
<td width="25">&nbsp;</td>
<td>Existing house:</td>
<td><input type="radio" name="Installation" value="Existing"></td>
</tr>
</table> </td>
</tr>
<tr>
<td class="bodytextbold">Preferences:</td>
<td>
<table class="bodytext">
<tr>
<td>Home theatre:</td>
<td><input name="Preferences[]" type="checkbox" value="Theatre"></td>
<td width="25">&nbsp;</td>
<td>Distributed Audio and Video:</td>
<td><input name="Preferences[]" type="checkbox" value="Distributed"></td>
<td width="25">&nbsp;</td>
<td>Home Automation:</td>
<td><input name="Preferences[]" type="checkbox" value="Automation"></td>
</tr>
</table> </td>
</tr>
<tr>
<td valign="top" class="bodytextbold">Message:</td>
<td><textarea name="Message" cols=50 rows=5 class="bodytext"></textarea></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit name="send" value="Submit">&nbsp;&nbsp;&nbsp;<input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>

And I have the following PHP file (contact.php):


<?php
$to = 'name@domain.co.za' ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Website Contact";

$fields = array();
$fields{"Name"} = "Name";
$fields{"Company"} = "Company";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$fields{"Installation"} = "Installation Type";
$fields{"Message"} = "Message";

$body = "You have received the following information:\n\n";
foreach($fields as $a => $b){
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}

$send = mail($to, $subject, $body, $headers);
if($send)
{header( "Location: success.html");}
else
{header( "Location: error.html");}
?>

The form works fine besides the checkboxes. I have removed the checkbox code from the PHP file as I cannot get it to work.

How can I get the output email to display which checkboxes have been checked?

dublinmike

1:55 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



I think the problem is that you are passing an array to sprintf. Try something like:


foreach($fields as $a => $b)
{
if($a == 'Preferences' && is_array($_REQUEST[$a]))
{
$body .= sprintf("%20s:\n",$b);
foreach($_REQUEST[$a] as $preference)
{
$body .= "> $preference\n";
}
}
else
{
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
}

I haven't tested this and you'll also need to add 'Preferences' into your fields array. I'd say that's the issue though.

daveginorge

2:17 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



The "method" on your form is "POST", this means that all your HTML elements that carry the data will be available in the SUPER GLOBAL $_POST.

eg.
$value = $_POST['HTML_element_name']

Therefor

The checkbox name "Preferences" will be passed in the SUPER GLOBAL $_POST or $_GET (If method "GET" is used) and will be an array of the "Preferences" values.

$cb_value = $_POST['Preferences'}; creates an array $cb_value.

You can the loop through the $cb_value array to discover the values

$i = 0;
$n = count($cb_value);

while ($i < $n) {
echo $cb_value[$i] . "\r\n";
$i++;
}

Hope this helps
Dave

garion1

6:14 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



Thanks for the help. I am not a PHP coder, so I will try and make sense of your posts.
I appreciate the help though.

garion1

6:23 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



dublinmike, your suggestion worked perfectly.
Thank you so much!