du2229 2015-04-23 04:03
浏览 68

联系表单 - 如何在表单的主体中显示错误消息数组的内容

This is my first time trying to use php in a website and posting a question to stackoverflow. I hope I haven't posted too much code.

The goal is to have the results of a foreach loop display an array's content in the form's html. I'm have been able to display one error message, but not more than one when several exist. Currently the code returns 'Array' when submitted with errors.

   <?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 (.&nbsp;-&nbsp;')</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 (&nbsp;'&nbsp;-&nbsp;,&nbsp;.&nbsp;:&nbsp;;&nbsp;/ and parentheses), and has a limit of 1500 characters</p>";
}

$body = 
    "Name of sender: $nam

" .
    "Email of sender: $ema

" .
    "Message:

" .
    "$msg" ; 

$body = wordwrap($body, 70);

$headers = "Reply-To: $nam <$ema>
";
$headers .= "Return-Path: <$ema>
";
$headers .= "From: $nam <$ema>
";
$headers .= "MIME-Version: 1.0
";
$headers .= "X-Sender: $ema
";
$headers .= "Content-type: text/plain; charset=iso-8859-1
";
$headers .= "X-Priority: 3
";
$headers .= "X-Mailer: PHP" . phpversion() . "
";

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>
  • 写回答

2条回答 默认 最新

  • douleng0794 2015-04-23 04:17
    关注

    The Code is write you have done this

     foreach ($error_msg as $error_output) {
       $error_msg[] = $error_output;
     }
    

    OR do this

    foreach ($error_msg as $error_output) {
       echo "<div class='error'>". $error_output . "</div>";
     }
    

    Or do this

    Instead Do this

    $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 
    

    if the array have messages it will show them one by one in each line

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制