Forum Moderators: coopster
<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"> </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"> </td>
<td>Distributed Audio and Video:</td>
<td><input name="Preferences[]" type="checkbox" value="Distributed"></td>
<td width="25"> </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"> <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?
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.
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