-1

我需要帮助将复选框添加到我的联系表单中,我将其作为 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部分代码

<?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
}
?>

我需要帮助添加复选框组,以便我可以从每个组中选中一个框,它会发送一封电子邮件,其中包含从每个组中选择的复选框的列表。

4

1 回答 1

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
}
?>
于 2013-07-11T05:10:16.147 回答