PHP联系一个函数中的脚本错误

时间:2015-11-07 17:03:44

标签: php forms contact

我有联系表单的HTML和PHP代码。当有人输入如下字符:š,đ,ž,č和ć(UTF-8)字符时,脚本报告的错误很少。

1。如何在$ first_name和$ last_name输入表单中添加UTF-8输入?

接下来的问题是,Java Script弹出错误或成功的消息。如果我输入名称框“šđžčć”并在姓氏框中输入“šđžčć”脚本,则会重新显示三个消息:

  1. 错误的名字
  2. 错误的姓氏
  3. 请再试一次(不完全是这样但有些消息)
  4. 这意味着 - 三个弹出窗口!你得到一个 - 你关闭它,然后第二个 - 关闭它,等等,这是令人沮丧的。我的第二个问题:

    2。如何重写这个PHP,并将所有消息和错误放在一个函数/值/消息中,只弹出一个说:错误的名字,姓氏,我爱你等。

    PHP代码:

    <?php
     
    if(isset($_POST['email'])) {
     
         
     
        // EDIT THE 2 LINES BELOW AS REQUIRED
     
        $email_to = "you@yourdomain.com";
     
        $email_subject = "Your email subject line";
     
         
     
         
     
        function died($error) {
     
            // your error code can go here
     
            echo "<script type='text/javascript'>alert('error')</script>";
     
            echo "<script type='text/javascript'>alert('these errors apears below')</script>";
     
            echo $error."<br /><br />";
     
            echo "<script type='text/javascript'>alert('please fix errors')</script>";
     
            die();
     
        }
     
         
     
        // validation expected data exists
     
        if(!isset($_POST['first_name']) ||
     
            !isset($_POST['last_name']) ||
     
            !isset($_POST['email']) ||
     
            !isset($_POST['telephone']) ||
     
            !isset($_POST['comments'])) {
     
            died(<script type='text/javascript'>alert('Submitted successfully!')</script>');       
     
        }
     
         
     
        $first_name = $_POST['first_name']; // required
     
        $last_name = $_POST['last_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 .= '<script type='text/javascript'>alert('wrong email !')</script>';
     
      }
     
        $string_exp = "/^[A-Za-z .'-]+$/";
     
      if(!preg_match($string_exp,$first_name)) {
     
        $error_message .= '<script type='text/javascript'>alert('invalid firs name')</script>';
     
      }
     
      if(!preg_match($string_exp,$last_name)) {
     
        $error_message .= '<script type='text/javascript'>alert('invalid last name')</script>';
     
      }
     
      if(strlen($comments) < 2) {
     
        $error_message .= '<script type='text/javascript'>alert('invalid message')</script>';
     
      }
     
      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 .= "Last Name: ".clean_string($last_name)."\n";
     
        $email_message .= "Email: ".clean_string($email_from)."\n";
     
        $email_message .= "Telephone: ".clean_string($telephone)."\n";
     
        $email_message .= "Comments: ".clean_string($comments)."\n";
     
         
     
         
     
    // create email headers
     
    $headers = 'From: '.$email_from."\r\n".
     
    'Reply-To: '.$email_from."\r\n" .
     
    'X-Mailer: PHP/' . phpversion();
     
    @mail($email_to, $email_subject, $email_message, $headers);  
     
    ?>
     
     
     
    <!-- include your own success html here -->
     
     
     
    echo "<script type='text/javascript'>alert('Submitted successfully!')</script>"
     
     
     
    <?php
     
    }
     
    ?>

0 个答案:

没有答案