我在表单中有几个复选框。我试图将这些值反映到电子邮件中。 这是HTML:
<input id="checkbox1" type="checkbox" name="work-check[]"><label for="checkbox2">App Design</label>
<input id="checkbox2" type="checkbox" name="work-check[]"><label for="checkbox1">Design</label>
<input id="checkbox3" type="checkbox" name="work-check[]"><label for="checkbox2">Email Design</label>
<input id="checkbox4" type="checkbox" name="work-check[]"><label for="checkbox1">Layout Design</label>
<input id="checkbox5" type="checkbox" name="work-check[]"><label for="checkbox1">Poster Design</label>
<input id="checkbox6" type="checkbox" name="work-check[]"><label for="checkbox2">Web Design</label>
这是PHP:
<?php
if ($_POST["submit"]) {
if(!filter_var($_POST[email], FILTER_VALIDATE_EMAIL)) {
echo '<div data-alert class="alert-box warning">E-mail is not valid</div>';
$error=true;
}
if($error==false) {
$result='<div data-alert class="alert-box success">Form Submitted</div>';
$headers = array("From: myemail@mail.com",
"Reply-To: myemail@mail.com",
"X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("\r\n", $headers);
// the message
// the message
$msg = "
First Name: ".$_POST['first_name']."
Last Name: ".$_POST['last_name']."
Email: ".$_POST['email']."
Options: ".implode(",", $_POST['work-check']);
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
$to = "myemail@mail.com";
$subject = "New Request";
mail($to, $subject, $msg, $headers);
}
}
echo $result;
?>
我想我已经设置好了我应该得到一个叫做工作检查的数组。我只是不熟悉php,知道如何遍历该数组并设置我得到的值。 我发现这样的事情我认为可能正朝着正确的方向发展:
// The array will only contain the checked value of forms
foreach($golfer as $g) {
// can use checkbox value here
}
我不确定php代码中应该去哪里。
答案 0 :(得分:3)
将其存储为字符串
//Store the array from the post as an array
$arr = $_POST['work-check'];
//implode the array into a string variable
$options = implode( ",", $arr );
或者,将其保存为数组并按照您想要的方式迭代它。
$arr = $_POST['work-check'];
foreach ($arr as &$value) {
...
}
unset($value); // break the reference with the last element
答案 1 :(得分:1)
给复选框一个值 这应该给你一个想法:
<?php
if ( isset( $_POST['submit'] ) ) {
$work = implode( '<br>', $_POST['work'] );
}
?>
<!DOCTYPE html>
<html>
<body>
<?= $work ?>
<form method="post">
<input type="checkbox" name="work[]" value="Design"> Design<br>
<input type="checkbox" name="work[]" value="Email Design"> Email Design<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>