dongyin2885 2011-11-28 22:26
浏览 61
已采纳

验证提交的表单,而不将PHP重定向到操作页面

I'd like to submit a form thanks to AJAX and display the error messages contained in "form_treatment.php" (form verification) without the php redirection to "form_treatment.php" when I submit. What have I to write in the following lign ?

$.post("form_treatment.php",name, ???); 

I have a basic form in form.php :

<form method="post" action="form_treatment.php" >
    <input type="text" name="user_name" value="Your name..." /> 
    <button type="submit" >OK</button>
</form> 

This form is treat in form_treatment.php :

if ( empty($_POST['user_name']) ){      
    echo 'You have to enter your name.';        
} else { 
    $already_existing = verify_existence( $_POST['user_name'] ); 
    // verification in the DB, return true or false

    if( $already_existing ){
        echo 'Name already used.';
    } else {    
        // Entering the name in the DB
    }
}
  • 写回答

2条回答 默认 最新

  • dsnd7200 2011-11-28 22:39
    关注

    You can try something like the following:

    <form id="myForm" method="post" action="form_treatment.php" >
        <span id="message"></span>
        <input type="text" name="user_name" value="Your name..." /> 
        <input type="submit" >OK</button>
    </form>
    

    And the javascript:

    $('#myForm').submit(function() {
      var myForm = $(this);
      $.ajax({
        type: 'POST',
        url: 'form_treatment.php',
        data: myForm.serialize(),
        success: function (data) {
           $('#message').html(data);
        }
      }); 
      return false;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题