来自复选框的值通过邮件发送-PHP表单
我的后端有这个PHP代码来自复选框的值通过邮件发送-PHP表单,php,forms,checkbox,Php,Forms,Checkbox,我的后端有这个PHP代码 if(!empty($_POST['model_list'])) { foreach($_POST['model_list'] as $model) { } } else { throw new Exception('Choose the model.'); } 当我从前端收到请求时,$\u POST['model\u list']变量似乎不包含前端中指定的值 以下
if(!empty($_POST['model_list'])) {
foreach($_POST['model_list'] as $model) {
}
} else {
throw new Exception('Choose the model.');
}
当我从前端收到请求时,$\u POST['model\u list']变量似乎不包含前端中指定的值
以下是我的HTML代码:
<input type="checkbox" value="A" name="model_list[]"><span>A</span>
完整代码:
<?php
if (isset($_POST["submit"])) {
$name = (string) $_POST['name'];
$email = (string) $_POST['email'];
$message = (string) $_POST['message'];
$number = (string) $_POST['number'];
$tel = (string) $_POST['tel'];
$from = 'name@domain.tld';
$to = 'name@domain.tld';
$subject = 'Form';
$body = "Name: $name \n E-mail: $email \n Phone number: $tel \n Serial number: $number \n Model: $model \n Message: $message \n";
try {
if (!$name) {
throw new Exception('Write name.');
}
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Write correct e-mail');
}
if (!$message) {
throw new Exception('Write message');
}
if (mail ($to, $subject, $body, $from)) {
$result = "<center><div style='color:white;font-size:15px;font-weight:700;'>Your message has been sent</div></center>";
} else {
throw new Exception("Your message has not been sent, try again");
}
if(!empty($_POST['model_list'])) {
foreach($_POST['model_list'] as $model) {
}
} else {
throw new Exception('Choose the model.');
}
} catch(Exception $e){
$result = "<center><div style='color:white;font-size:25px;font-weight:700;'>" . $e->getMessage() . "</div></center>";
}
echo $result;
}
?>
要获取电子邮件中的所有模型,请使用内爆
:
if (!empty($_POST['model_list'])) {
$model = implode(', ', $_POST['model_list']);
} else {
$model = '';
}
要获取电子邮件中的所有模型,请使用内爆:
if (!empty($_POST['model_list'])) {
$model = implode(', ', $_POST['model_list']);
} else {
$model = '';
}
嗯,我编辑了PSOTY,在分配给$body
之前,您不会设置$model
。当我添加“$model=(string)$\u POST['model\u list']”时,我收到了一封带有“array”作为模型的电子邮件。事实上,在发送邮件之前,您不会设置$model
。邮件中应该包含什么?由于它是一个数组,也许您应该使用$model=infrade(',',$\u POST['model\u list'])代码>嗯,我编辑了PSOTY,在分配给$body
之前,您不会设置$model
。当我添加“$model=(string)$\u POST['model\u list']”时,我收到了一封带有“array”作为模型的电子邮件。事实上,在发送邮件之前,您不会设置$model
。邮件中应该包含什么?由于它是一个数组,也许您应该使用$model=infrade(',',$\u POST['model\u list'])代码>