Forum Moderators: coopster

Message Too Old, No Replies

Check Boxes in Php Contact Form Not Being Emailed / Displayed

         

cmbcorp

1:11 am on Jan 29, 2009 (gmt 0)

10+ Year Member



hi,

i got some issues with my contact form.

basically i fill in the form and it gets emailed back with all information but no check boxes are being registered.

Below is the code for the contact form:


<form method="post" action="sendtraining.php">
<p>
<label>Department:</label>
<select name="sendto">
<option value="email@example" selected="selected">Training Department</option>
</select></label>
<br />
<label><span class="style1">*</span> Name</label>
<input name="Name" value="Your Name" type="text" size="30" />
<label><span class="style1">*</span> Company Name</label>
<input name="Company" value="Company Name" type="text" size="30" />
<label><span class="style1">*</span> Email</label>
<input name="Email" value="Your Email" type="text" size="30" />
<label><span class="style1">*</span> Phone Number</label>
<input name="Phone" value="Your Phone Number" type="text" size="30" />
<label>Additional Training Modules </label>
<input type="checkbox" name="check[]" value="y" />
Reporting<br />
<input type="checkbox" name="check[]" value="y" />
Advanced Stock Management (Promotions, Audit trails, Deletions, Transfers etc.)<br />
<input type="checkbox" name="check[]" value="y" />
POS Training overview<br />
<input type="checkbox" name="check[]" value="y" />
Customer Managment (Special pricing, loyalty, customer profiling)
<br />
<label>Other (Please specify below) </label>
<textarea name="Message" rows="5" cols="5"></textarea>
<br />
<input type="submit" name="send" value="Submit" />
<br />
<span class="style1">*</span>Iindicates a
field is required</p>
</form>

and here is the php script file:


<?php
$to = $_REQUEST['sendto'];
$from = $_REQUEST['Email'];
$name = $_REQUEST['Name'];
$company = $_REQUEST['Company'];
$phone = $_REQUEST['Phone'];

$headers = "From: $from";
$subject = "Conctact Form";

$fields = array();
$fields{"Name"} = "Name";
$fields{"Company"} = "Company";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$fields{"Message"} = "Message";
$check_msg .= "Checked: $value\n";

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

$headers2 = "From: noreply@example.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for for your query. A Sales representitive will be in contact with you shortly!.";

if($from=='') {print "You have not entered an email, please go back and try again";}
if($name=='') {print "You have not entered a name, please go back and try again";}
if($phone=='') {print "You have not entered a phone number, please go back and try again";}
if($company=='') {print "You have not entered a company name, please go back and try again";}

else
{
$send = mail($to, $subject, $body, $check_msg, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{
header("Location: http://www.example.com/thankyou.html");
}
else
{
echo "We encountered an error sending your mail, please notify sales@example.com";
}
}

?>

Can someone please take a look and see what im doing wrong?

It would be very much appreciated.

Thanks.

Jase.

[edited by: eelixduppy at 3:03 am (utc) on Jan. 29, 2009]
[edit reason] exemplified [/edit]

whoisgregg

1:35 am on Jan 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, cmbcorp!

The first thing I noticed is that your processing script never actually does anything with the checkbox data. Notice these lines which grab the posted data and store it into variables?

$to = $_REQUEST['sendto']; 
$from = $_REQUEST['Email'];
$name = $_REQUEST['Name'];

At the very least, it would help to add another one that references the passed "check" values:

$check = $_REQUEST['check'];

Now, what you'll find is that value is invariably filled with "Array" because multiple values can be sent. (That's why the checkboxes have the square brackets in their name, to tell PHP to store those in an array.)

So you'll need to do something to make the array values human readable. Perhaps implode would be enough?

$check = implode(', ', $_REQUEST['check']);

Or you could loop through the values and do something with them?

foreach($_REQUEST['check'] as $index=>$value){
$body.= sprintf("%20s: %s\n",'check box',$value);
}

Try out some of this code and post back any questions you have. :)

cmbcorp

1:41 am on Jan 29, 2009 (gmt 0)

10+ Year Member



Thank you so much! i really appriciate your help.

I have did what you suggested as per my code below.

However the email i receive still does not display the checkbox values.

Your help is very much appriciated.


<?php
$to = $_REQUEST['sendto'];
$from = $_REQUEST['Email'];
$name = $_REQUEST['Name'];
$company = $_REQUEST['Company'];
$phone = $_REQUEST['Phone'];
$check = $_REQUEST['check'];
$check = implode(', ', $_REQUEST['check']);

$headers = "From: $from";
$subject = "Conctact Form";

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

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

$headers2 = "From: noreply@example.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for for your query. A Sales representitive will be in contact with you shortly!.";

if($from=='') {print "You have not entered an email, please go back and try again";}
if($name=='') {print "You have not entered a name, please go back and try again";}
if($phone=='') {print "You have not entered a phone number, please go back and try again";}
if($company=='') {print "You have not entered a company name, please go back and try again";}

else
{
$send = mail($to, $subject, $body, $check_msg, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{
header("Location: http://www.example.com/thankyou.html");
}
else
{
echo "We encountered an error sending your mail, please notify sales@example.com";
}
}

?>

[edited by: eelixduppy at 3:04 am (utc) on Jan. 29, 2009]
[edit reason] exemplified [/edit]