创建一个警告框

时间:2014-07-28 19:57:31

标签: javascript php html

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

    <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页面。现在我只想取代用户转到另一个页面,错误消息和成功消息都应显示在警告框中的同一页面上。

2 个答案:

答案 0 :(得分:0)

当页面绘制出来时,请执行以下操作:

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

如果放置&#34; $ error_message&#34;

,您在页面上设置的任何PHP变量都可以使用。

答案 1 :(得分:0)

您有两种选择。

  1. 您可以将表单提交到同一页面,即将表单操作设置为与页面相同的表单,然后处理表单数据。一旦出现错误,您可以根据需要显示错误。这意味着将html和php放在同一页面上,这不是最干净的选择。

  2. 使用ajax使用jQuery提交表单。这可能是令人生畏的,因为你刚刚开始。虽然这样做更干净,更顺畅,但不会涉及页面刷新。

  3. 它会是这样的:

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

    Jquery Ajax Documentation

    jQuery并不是那么难以接受,并且是一个很好的学习工具。