doushaju4901 2017-04-04 08:20
浏览 45
已采纳

实现AJAX后,PHP验证无法正常工作

I have code that validates the input inside the form and displays an error message in the div class="error" if condition is met. However, upon using AJAX to prevent page reload, the PHP validation no longer displays the message but still functions in the background. Does anyone why this is happening? How can I make the PHP validation work again? Thank you for help.

As you can see the validation error is supposed to show in $email_error

<form class="form_pro" action="index.php" method="post" role="form">

<input type="text" class="form" name="E-mail" placeholder="E-mail" value="<?= $email ?>">

<div class="error"><?= $email_error ?></div>

<button name="submit" type="submit" class="action_button"></button>

</form>

PHP

The error message here displays inside the "div" prior to adding AJAX code

if ($_SERVER["REQUEST_METHOD"] == "POST") {

 if (empty($_POST["E-mail"])) {
    $email_error = "E-mail is required";
  } else {
    $email = test_input($_POST["E-mail"]);

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Invalid email format"; 
    }
  }
}

jQuery

After adding this to prevent page reload on submit, the PHP error message no longer shows

$( document ).ready(function() {
    $(".form_pro").submit(function(e) {
        e.preventDefault();

        $.ajax({
            type: "POST",
            url: "php/interuni_process.php",
            data: $(this).serialize(), 
            success: function(data) {

            },
            error: function() {

            }
        });

    });
});
  • 写回答

5条回答 默认 最新

  • douchuitang0331 2017-04-04 08:32
    关注

    1) you need to echo the error message in server side and get the response from server and append it to that div

    HTML : htm_page.php

    <form class="form_pro" action="index.php" method="post" role="form">
    
        <input type="text" class="form" name="E-mail" placeholder="E-mail" value="<?= $email ?>">
    
        <div class="error"></div>
    
        <button name="submit" type="submit" class="action_button"></button>
    
        </form>
    

    PHP :destination.php

       if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
            $email_error='';
    
         if (empty($_POST["E-mail"])) {
            $email_error = "E-mail is required";
          } else {
            $email = test_input($_POST["E-mail"]);
    
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
              $email_error = "Invalid email format"; 
            }
          }
    
              echo $email_error;
        }
    

    AJAX : html_page.php

        $( document ).ready(function() {
            $(".form_pro").submit(function(e) {
                e.preventDefault();
    
                $.ajax({
                    type: "POST",
                    url: "destination.php",
                    data: $(this).serialize(), 
                    success: function(data) {
    
                     if(data.trim() !='')
                     {
                        $('.error').html('<h1>'+data+'</h1>');
                     }
    
                    },
                    error: function() {
    
                    }
                });
    
            });
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题