Php 联系人表单-如何在表单主体中显示错误消息数组的内容';s html
这是我第一次尝试在网站中使用php并向stackoverflow发布问题。我希望我没有发布太多的代码 目标是让Php 联系人表单-如何在表单主体中显示错误消息数组的内容';s html,php,arrays,foreach,contact-form,Php,Arrays,Foreach,Contact Form,这是我第一次尝试在网站中使用php并向stackoverflow发布问题。我希望我没有发布太多的代码 目标是让foreach循环的结果以表单的html显示数组的内容。我已经能够显示一条错误消息,但当存在多条错误消息时,只能显示一条。当前,代码在提交时返回“Array”,但有错误 <?php $to = "companyname@email.com"; $subject = "Contact Form"; if (isset($_POST["submit"])) { $nam
foreach
循环的结果以表单的html显示数组的内容。我已经能够显示一条错误消息,但当存在多条错误消息时,只能显示一条。当前,代码在提交时返回“Array”,但有错误
<?php
$to = "companyname@email.com";
$subject = "Contact Form";
if (isset($_POST["submit"])) {
$nam = strip_tags($_POST["name"]);
$ema = htmlspecialchars($_POST['email']);
$msg = strip_tags($_POST["message"]);
$error_msg=array(); //initiate array, not required
if (empty($nam) || !preg_match("/^[\s.'\-\pL]{1,60}$/u", $nam)) {
$error_msg[] = "<p>Please fill in your name with only letters, spaces and basic punctuation (. - ')</p>";
}
if (empty($ema) || !filter_var($ema, FILTER_VALIDATE_EMAIL)) {
$error_msg[] = "<p>Your email must have a valid format, such as emailname@mailhost.com</p>";
}
$limit = 1500;
if (empty($msg) || !preg_match("/^[0-9\/\-\_\s'\(\)\@!\?\.,:;\pL]+$/u", $msg) || (strlen($msg) > $limit)) {
$error_msg[] = "<p>The Message field must contain only letters, digits, spaces and basic punctuation ( ' - , . : ; / and parentheses), and has a limit of 1500 characters</p>";
}
$body =
"Name of sender: $nam\n\n" .
"Email of sender: $ema\n\n" .
"Message:\n\n" .
"$msg" ;
$body = wordwrap($body, 70);
$headers = "Reply-To: $nam <$ema>\r\n";
$headers .= "Return-Path: <$ema>\r\n";
$headers .= "From: $nam <$ema>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "X-Sender: $ema\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP" . phpversion() . "\r\n";
if ($error_msg) {
foreach ($error_msg as $error_output) {
$error_msg_list .= '<div class="error">' . $error_output . '</div>';
}
// if no errors, send the email
} else if (!$error_msg) {
mail ($to, $subject, $body, $headers);
$success_output = "Your message has been sent. We will get back to you shortly.";
$_POST = array(); //clears the form data after valid form submission
}
} //end isset
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact Us</title>
</head>
<body>
<div class="content">
<h1>Contact Us</h1>
<form method="post" action="index.php">
<?php
if (isset($error_msg_list)) {
echo $error_msg_list;
} else {
echo "<div class='success'>" . $success_output . "</div>";}
?>
<div>
<label for="name">Name</label>
<input name="name" type="text" id="name" placeholder="your name" value="<?php if (isset($_POST["name"])) {echo $nam;} ?>">
</div>
<div>
<label for="email">Email Address</label>
<input name="email" type="email" id="email" placeholder="your email address" value="<?php if (isset($_POST["email"])) {echo $ema;} ?>">
</div>
<div>
<label for="message">Message</label>
<textarea name="message" rows="10" id="message"><?php if (isset($_POST["message"])) {echo $msg;} ?></textarea>
</div>
<div>
<input type="submit" name="submit" value="Send">
</div>
</form>
</div>
</body>
</html>
如果您完成了此操作,代码将被写入
foreach ($error_msg as $error_output) {
$error_msg[] = $error_output;
}
还是这样做
foreach ($error_msg as $error_output) {
echo "<div class='error'>". $error_output . "</div>";
}
foreach($error\u msg作为$error\u输出){
回显“%$error_output.”;
}
还是这样做
而是这样做
$message = array();
foreach ($error_msg as $error_output) {
$message['key_you_want_or_use_indexing'] "<div class='error'>". $error_output . "</div>";
}
echo $message[0]; // for first error
$message=array();
foreach($error\u msg作为$error\u输出){
$message['key\u you\u want\u or\u use\u index']。$error\u output.“;
}
echo$消息[0];//第一个错误
如果数组中有消息,它将在每行中逐个显示它们
我编辑了代码,在创建了所有三个变量的foreach循环中添加了一个。=
if($error\u msg){
foreach($error\u msg作为$error\u输出){
$error_msg_list.=''.$error_output'.';
}
我将表单元素中的php更改为使用$error\u msg\u list
请解释将遍历每个元素的foreach循环,等等?$error\u msg已经是一个错误消息数组。@Luke foreach循环将遍历$error\u msg[]然后我可以使用echo列出所有出现的验证错误,例如$error\u msg。这确实有效,但会将结果放在body元素内html的顶部,我正尝试将这些结果作为isset()的一部分进行echo,以便在form元素内打印$error\u msg或$success\u msg。目前在isset()中有$error\u msg在提交表单时返回“Array”而不是列表。是否有方法让foreach循环在表单元素中创建div.error,方法与$success\u输出相同?foreach()的“do something”部分可以吗创建一个结果/容器,该结果/容器可以使用,并且在表单提交之前不会作为列表回显,因此能够在isset()中包含“do something”部分位于表单元素中?是初始化变量,创建容器或结果并将其存储在变量中,然后在需要的地方使用变量,如果有多个结果,则使用数组而不是变量并定义它的键,这样您将有单独的消息['key\u you\u want…'之后是否应该有一个“=”]?我尝试了-$message[0]=“.$error\u output.”和html if(isset($error\u msg\u list)){echo$error\u msg\u list[0];}else{echo'.$success\u output.“;}中,它返回了最后一个error\u msg\u列表,但不是全部三个。
if ($error_msg) {
foreach ($error_msg as $error_output) {
$error_msg_list .= '<div class="error">' . $error_output . '</div>';
}
<?php
if (isset($error_msg_list)) {
echo $error_msg_list;
} else {
echo "<div class='success'>" . $success_output . "</div>";}
?>