jQuery/php-在页面上多个表单,只提交一个

jQuery/php-在页面上多个表单,只提交一个,php,jquery,ajax,Php,Jquery,Ajax,我的网站上有两个表单,我使用jQuery将它们提交到我的PHP脚本中 以下为表格: <form method="post" class="settings-form" id="passwordSettings"> <label id="npasswordbox" class="infoLabel">New Password: </label> <input type="password" name="npassword" size="50" v

我的网站上有两个表单,我使用jQuery将它们提交到我的PHP脚本中

以下为表格:

<form method="post" class="settings-form" id="passwordSettings">
  <label id="npasswordbox" class="infoLabel">New Password: </label>
  <input type="password" name="npassword" size="50"  value="" >

  <div class="move"></div> 

  <label id="cnpasswordbox" class="infoLabel">Confirm: </label>
  <input type="password"  name="cnpassword" size="50" value="" >

  <button class="btn" name="passwordSetings" style="margin-left:185px" type="submit">Save </button> 

</form><!-- end form -->
我的问题是,每当我提交其中一个表单时,我都会在#status div中看到带有$error的表单

如何在一页上有多个表单,但提交正确的表单?

$(function() {
    $('form#passwordSettings').submit(function(e){
        e.preventDefault(); // prevents the default action (in this case, submitting the form)
        $('#status').hide();
        $.post(
            'index.php?i=a&p=s', 
            $('form#passwordSettings').serialize(),
            function (data) {
                proccessPWData(data);
            }
        );
        return false;    
    });
});
或者你可以给它一个隐藏的输入字段

<input type="hidden" name="_normalSettings">
或者你可以给它一个隐藏的输入字段

<input type="hidden" name="_normalSettings">

这基本上就是对您问题的回答:“我如何在一个页面上有多个表单,但提交正确的表单?”

我在一个页面上有许多动态生成的表单,并将它们逐个发送到处理文件。这是一种简化形式:

<form name="form" id="form">
<!--form fields
hidden field could be used to trigger wanted process in the process file
-->
<input type="hidden" name="secret_process_id" value="1" />
<a class="button_ajax">Send form</a> 
</form>

<div id="process_msg<?php echo $id; ?>"></div>
您只需要一个流程文件/函数,就可以开始了。对于一个或几十个表单都可以正常工作。在那里,您可以执行以下操作:

if ($_POST['secret_process_id']==1){
  //do something
}

if ($_POST['secret_process_id']==2){
  //do something else
}

//etc.

这基本上就是对您问题的回答:“我如何在一个页面上有多个表单,但提交正确的表单?”

我在一个页面上有许多动态生成的表单,并将它们逐个发送到处理文件。这是一种简化形式:

<form name="form" id="form">
<!--form fields
hidden field could be used to trigger wanted process in the process file
-->
<input type="hidden" name="secret_process_id" value="1" />
<a class="button_ajax">Send form</a> 
</form>

<div id="process_msg<?php echo $id; ?>"></div>
您只需要一个流程文件/函数,就可以开始了。对于一个或几十个表单都可以正常工作。在那里,您可以执行以下操作:

if ($_POST['secret_process_id']==1){
  //do something
}

if ($_POST['secret_process_id']==2){
  //do something else
}

//etc.

尝试使用input type=“submit”作为提交按钮。那么它只会提交里面的表格。你能举个例子吗?不太清楚您的意思:-)您在status div中看到了哪个错误?没有pw或没有名字?我只是在状态栏中看到了整个表单检查我的答案ı认为它解决了您的问题。尝试使用input type=“submit”作为提交按钮。那么它只会提交里面的表格。你能举个例子吗?不太清楚您的意思:-)您在status div中看到了哪个错误?没有,或者没有名字?我只是在状态栏里看到了整个表格检查我的答案,认为它解决了你的问题。
$(document).ready(function() {
  $('.submit_ajax').click(function() { //serializes the parent form
    //alert($(this).serialize());
    dataString = $(this).parent().serialize();

    //if you want to echo some message right below the processed form
    var id = /id=\d+/.exec(dataString);
    var id = /\d+/.exec(id);

    $.ajax({
      type: 'post',
      url: '_process.php?ajax=1', //some or none parameters
      data: dataString,
      dataType: 'html',
        success: function(data) {
          $('#process_msg' + id).fadeIn(400);
          $('#process_msg' + id).html(data);
        }

     }); //end of $.ajax

return false;

  });
});
if ($_POST['secret_process_id']==1){
  //do something
}

if ($_POST['secret_process_id']==2){
  //do something else
}

//etc.