我正在使用ajax / php / jquery的联系表单。
我在表单中添加了复选框。复选框的名称称为' selection []'
我很难获得复选框的值以输出到电子邮件文本。
当我使用.implode语句时,它循环遍历foreach语句并输出5次选中的复选框值。
我正在尝试在网站上为朋友工作,我不是PHP专家,我只是一个初学者。
请帮助。
下面是代码:
$from = 'Sales@ipsnypro.com';
$sendTo = 'dave.torres@aecom.com';
$subject = 'New message from IPSNYPRO.com';
$fields = array('name' => 'First Name:', 'surname' => 'Last Name:', 'phone' => 'Phone:', 'email' => 'Email:', 'message' => 'Message:', 'selection' => 'I am interested in:'); // array variable name => Text to appear in the email
$okMessage = 'Contact form has been successfully submitted. Thank you, We will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
try
{
$emailText = "You have new message from www.IPSNYPRO.com\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key] $value\n";
}
}
$headers = array('Content-Type: text/html; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
答案 0 :(得分:0)
重要的是要注意,浏览器通常不会在HTTP请求中发送checkbox,除非已经检查过。
<强>检查强>
- 当type属性的值为复选框时,此布尔属性的存在表示默认选择控件;否则会被忽略。
因此,明确检查您希望从表单中获取的值更为明智,而不是盲目地假设客户端将总是向您发送所有值(或仅限于那些您期望的值。
我假设您的input
数组包含表单上预期的foreach
字段的名称。因此,假设这是真的,我们可以安全地执行此操作而不是/* This will initialize the array with all keys in $fields with their default values
if they aren't already in $_POST, making sure you always get what you expect */
$data = array_intersect_key($_POST, $fields) + $fields;
foreach($data as $key => $value) {
echo htmlspecialchars($key) . " = " . htmlspecialchars($value) . "<br>\n";
}
循环。
htmlspecialchars
您应该使用htmlentities
或 library(ggplot2)
df <- data.frame(y=seq(1, 1e5, length.out=100), x=sample(100))
p <- ggplot(data = df, aes(x=x, y=y)) + geom_line() + geom_point()
p
来正确转义客户端提供的输入值,因为它们在此处不能被视为安全的HTML上下文。