我需要帮助,在我的联系表单中添加复选框,我将其作为html。
<form action="contact.php" method="post">
Your name<br><input type="text" name="cf_name"><br>
Your e-mail<br><input type="text" name="cf_email"><br>
Message<br><textarea name="cf_message"></textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
这是我的代码的部分
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'example@example.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to help@helpme.com');
window.location = 'contact.html';
</script>
<?php
}
?>
我需要帮助添加复选框组,以便我可以从每个组中选中一个框,它会发送一封电子邮件,其中列出了从每个组中挑选的复选框。
答案 0 :(得分:0)
Use this code
<form action="contact.php" method="post">
Your name<br><input type="text" name="cf_name"><br>
Your e-mail<br><input type="text" name="cf_email"><br>
Message<br><textarea name="cf_message"></textarea><br>
Check boxes: <input type="checkbox" name="abc[]" value="Car">Car<br/>
<input type="checkbox" name="abc[]" value="Bus">Bus<br/>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
// check boxess
$check_box_values = "Check box value: ";
if(isset($_POST['abc'])){
foreach($_POST['abc'] as $value){
$check_box_values .= $value;
$check_box_values .= ', ';
}
}
// end check boxess
$mail_to = 'example@example.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message."\n".$check_box_values;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to help@helpme.com');
window.location = 'contact.html';
</script>
<?php
}
?>