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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效