创建警告框

Creating a alert box

本文关键字:警告 创建      更新时间:2023-09-26

我是网页设计的初学者,我设计了一个联系人页面,其中页面编码为:

    <div class="col-md-6">
        <h4 class="uppercase weight-700">Send us a message</h4>
            <form name="contactform" method="post" action="send_form_email.php">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="first_name">Tell us your name</label>
                        <input type="text" name="first_name" maxlength="50" size="30">
                    </div>
                </div>
                </div>
                <div class ="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="email">Tell us your email</label>
                        <input type="text" name="email" maxlength="80" size="30">
                    </div>
                </div>
                </div>
                <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="telephone">What&#39;s your number?</label>
                        <input type="text" name="telephone" maxlength="30" size="30">
                    </div>
                </div>
                </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="comments">Give us the details</label>
                        <textarea  name="comments" maxlength="1000" cols="28" rows="6"></textarea>
                    </div>
                    <div class="text-center">
                        <br/>
                        <button class="btn btn-primary btn-round" type="submit" onclick="myFunction()">Send Message </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

和PHP代码是(send_form_email.php):

<?php
if(isset($_POST['email'])) {
    $email_to = "email@mydomain.com";
    $email_subject = "Form Data";
    function died($error) {
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
    if(!isset($_POST['first_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }
    $first_name = $_POST['first_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+'.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
   $string_exp = /^[0-9]+$/';
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The telephone you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.'n'n";
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
    $email_message .= "First Name: ".clean_string($first_name)."'n";
    $email_message .= "Email: ".clean_string($email_from)."'n";
    $email_message .= "Telephone: ".clean_string($telephone)."'n";
    $email_message .= "Comments: ".clean_string($comments)."'n";
$headers = 'From: '.$email_from."'r'n".
'Reply-To: '.$email_from."'r'n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>

当用户单击提交按钮时,他被重定向到send_form_email.php页面。现在我只是想要在用户进入另一个页面的地方,错误消息和成功消息都应该显示在同一个页面的警告框中

您有两个选择。

  1. 你可以让表单提交到同一个页面,也就是说,将表单动作设置为与表单页面相同,然后处理表单数据。一旦有了错误,就可以适当地显示它们。这将意味着有html和php在同一页上,这不是最干净的选择。

  2. 使用ajax使用jQuery提交表单。这可能有点吓人,因为你刚刚开始。虽然这将是一种更干净,更流畅的方式,因为它不涉及页面刷新。

应该是这样的:

    $.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

Jquery Ajax Documentation

jQuery并不难学,是一个很好的学习工具。

当页面画出时,执行以下操作:

<script>
alert(<? echo $error_message ?>);
</script>

您在页面上设置的任何PHP变量都可以在我放置"$error_message"

的地方使用