简体   繁体   English

while循环中相同id的表单 - 如何使用jQuery AJAX将用户提交表单的表单数据发布到php?

[英]forms of same id in while loop - how to post formdata of user submitted form to php with jQuery AJAX?

I have a while loop which creates forms like:我有一个 while 循环,它创建如下形式:

<?php
$i = 1;
while($i<10){
    ?>
<form id="update">
  <tr>
    <th scope="row"><?php echo $i;?></th>
    <td></td>
    <td></td>
    <td></td>
    <td><input type="text" maxlength="5" class="input-xs valid" name="plus" value="" /></td>
    <td><input type="submit" name="submit" class="btn btn-info btn-sm" value="Update" /></td>
    <td><input class="input-sm slip" name="slip" value="" disabled /></td>
    <td></td>
  </tr>
</form>

<?php $i++; }  ?>

I want to post the formdata of user-submitted form with jQuery AJAX.我想用 jQuery AJAX 发布用户提交表单的表单数据。

your question is not clear but here is the answer - if you are generating the forms on the basis of while loop, then you need to pas that $i in every where in the form您的问题不清楚,但这是答案 - 如果您基于 while 循环生成表单,那么您需要在表单的每个位置传递$i

<?php
            $i = 1;
        while($i<10){   

          ?>
    <form id="update_<?php echo $i ?>">
          <tr> 
          <th scope="row"><?php echo $i;?></th>
          <td></td>
          <td></td>
          <td></td>
          <td><input type="text" maxlength="5" class ="input-xs valid" id="plus_<?php echo $i?>"  name="plus" value=""></td>
          <td><input type="button" name="submit" class="btn btn-info btn-sm" value="Update" onclick="sub_form(<?php echo $i; ?>)"></td>
          <td><input class="input-sm slip" id="slip_<?php echo $i?>" name="slip" value="" disabled/></td>
          <td></td>
          </tr> 
         </form>
          <?php $i++; }  ?>

AJAX AJAX

function sub_form(val){
   var plus = document.getElementById('plus_'+val).value;
   var slip = document.getElementById('slip_'+val).value;
   $.ajax(
                {
                    type:"post",
                    url: "your url here",
                    data:{ plus:plus, slip:slip},
                    success:function(response)
                    {

                    }

                }
            );
}

hope it will work for you希望它对你有用

You should add a class in form HTML node as below:您应该在表单 HTML 节点中添加一个类,如下所示:

<form class="anyclass" action="add your action where you want to post form">

And after this, you can submit form as below:在此之后,您可以提交如下表格:

$(".anyclass").submit(function(e) {
e.preventDefault
$.ajax({
       type: "POST",
       url:  $(this).attr('action'),
       data:  $(this).serialize(), 
       success: function(data)
       {
               alert('form submitted herer');
       }
});

Hope it helps you.希望对你有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery Ajax Post-表单数据未正确提交 - jQuery Ajax Post - Form data is not submitted correctly 定位正确的表单输入值,同时在页面-PHP AJAX JQUERY上使用多个表单 - Target correct form input values, while using multiple forms on same the page -PHP AJAX JQUERY php jquery ajax表单提交两次 - php jquery ajax form submitted twice 使用formdata通过jQuery Ajax将带有文件输入,文本输入和多个复选框的表单发布到PHP - Post form with fileinput, textinput and multiple checkboxes to php via jquery ajax using formdata 通过jQuery和FormData的Ajax POST请求 - $ _POST在PHP上为空 - Ajax POST request via jQuery and FormData - $_POST empty on PHP 如何从使用AJAX提交的表单(与其他具有相同类名的表单)中选择和提取数据? - How to select and extract data from a form that is submitted with AJAX which is among other forms with the same class name? 如何在 JS 页面上使用 jQuery 从许多动态生成的 forms 获取提交的表单 ID - How to get submitted form id from many dynamically generated forms using jQuery on JS page 使用Ajax提交表单时,如何从while循环中的相同ID摆脱 - How to get rid from the same id in while loop when submitting form using ajax php中的jQuery ajax调用while循环仅返回第一个id - jQuery ajax call in php while loop only returns first id 对同一个 PHP 页面执行 AJAX 发布请求(Jquery 表单插件) - Perform AJAX post request to the same PHP page (Jquery Form Plugin)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM