php - Ajax 不适用于我的所有表单

标签 php jquery sql ajax forms

我正在使用 ajax 来验证我的表单,但我的表单在 php 中处于 while 循环中,并且我的请求 ajax 仅适用于我的第一个表单。

我的代码:

Ajax :

<script type="text/javascript">

$(document).ready(function() {
$('#form_entourage').on('submit', function() {

    var nom = $('#entourage_nom_1').val();
    var prenom = $('#entourage_prenom_1').val();

    if(nom == '' || prenom == '') {
        alert('Les champs doivent êtres remplis');
    } else {
        $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            data: $(this).serialize(),
            dataType: 'json',
            success: function(json) {
                if(json.reponse == 'ok') {
                    alert('Tout est bon');
                } else {
                    alert(''+ json.reponse);
                }
            }
        });
    }
    return false;
});
});

</script>

PHP 表单: 我的 while 循环和 ajax 仅在 1 个表单上工作。 例如:当我有 3 个成员时,我的 ajax 验证仅对第一个成员起作用 成员,在我的第二个成员上,我重定向了我的 php 验证。

        while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
       printf ("

              <h3>Membre ".$numero_membre."</h3>");
       $numero_membre = $numero_membre+1;
       printf ("
<form class='form-horizontal' id='form_entourage' method='post' action='modif/mod_entourage.php' style='height: 670px;'>
          <span class='span5 control-group'>
            <label for='entourage_nom_1'>Nom</label>
            <input class='span5' type='text' name='entourage_nom_1' value='".$row["nom"]."' >
            <input type='hidden' name='id_entourage' value='".$row["id_entourage"]."' >
          </span>
          <span class='span5 control-group'>
            <label for='entourage_prenom_1'>Prénom</label>
            <input class='span5' type='text' name='entourage_prenom_1' value='".$row["prenom"]."' >
          </span>
<span class='span5 control-group'>
<input type='submit' name='mod_entourage' id='mod_entourage' class='btn btn-primary pull-right' value='Modifier' /></span>
          </form>
          <div class='clear'></div>
");
    }

有人可以帮助我吗?

最佳答案

您对所有表单使用相同的 ID。在表单 ids 中附加一个计数器变量。它肯定适合你。你的表单 ID 可以是这样的

 <form id="form_entourage_ 1" data-idval="1" .. >..Similarly change id for other elements</form>

 <form id="form_entourage_ 2" data-idval="2" .. >..</form>

 <form id="form_entourage_ 3" data-idval="3" .. >..</form>

Jquery:-

   $("[id^='form_entourage']").on('submit', function() {
         var clicked_id = $(this).data('idval');

         var nom = $('#entourage_nom_'+clicked_id).val();
         var prenom = $('#entourage_prenom_'+clicked_id).val();
         ...........
         ...........
    });

关于php - Ajax 不适用于我的所有表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16980848/

相关文章:

sql - "Flexible"(?) Rails 数据库

php - SQL返回错误结果

php - (Laravel) 表关系和外键的用法

jquery - Blueimp 画廊 : Always show "blueimp-gallery-controls" or how to unbind click handler to show and hide "blueimp-gallery-controls"

php - MySql:如果值存在 UPDATE else INSERT

jquery - 在 JQuery 中按多个属性进行过滤

javascript - 页面加载后滚动条不调整大小

mysql - 如何在SQL中获取超过特定数量的第一行

javascript 在本地主机上工作但在托管服务器上失败

php - 在 div 中显示行