duannv2081 2013-06-07 09:35
浏览 47
已采纳

Ajax不适用于我的所有表单

I'm using ajax to validate my forms but my form is in a while loop in php and my request ajax work only on my first forms.

My code :

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 Forms: with my loop while and my ajax work just on 1 form. ex: when i have 3 member my ajax validation work only on the first member and on my second member i redirected on my php validation.

        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>
");
    }

Someone can help me please?

  • 写回答

3条回答 默认 最新

  • douzi8112 2013-06-07 09:38
    关注

    your are using same id for all forms . append a counter variable in form ids. it will surely works for you. your form id can be like this

     <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();
             ...........
             ...........
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)