duanquannan0593 2017-06-28 00:30
浏览 41

如何从多种形式提交每个表格

I'm trying to build a friend request system. I have registered users, which echo with an add button and each user has a form that is automatically created when a user creates an account. My problem is how do I submit each form? With what I have now when this users add button is submitted it will submit only the form of the first user in the database even when the add button is for the 4 or 5 user.

index.php

<div class="users_b">
     <?php
     include 'db.php';

        $sq = "select * from alert_users_account";
        $query = mysqli_query($con,$sq);
        while($row = mysqli_fetch_assoc($query)){

     ?>
     <div class="user_dis_p" id="user<?php echo $row['id']?>">
     <div id="user_img"><a href="alert_profile.php?id=<?php echo $row['id']?>"><img src="alert<?php echo $row['photo']; ?>">
     </a></div> <div id="user_fs">
     <a href="alert_profile.php?id=<?php echo $row['id']?>">
     <?php  echo $row['firstname']." "." "." "." ".$row['surname'];?></a><form id="upload_file_f">
     <input type="text" name="friend_id" id="friend_id" value="<?php echo $row['id']?>" readonly>
     <input type="submit" value="submit">
    </form>
</div>
</div>

<script>
$(document).ready(function(e){
$("#upload_file_f").on("submit",function(e){
e.preventDefault();

$.ajax({
   type: "post",
   url:"request.php",
   data:new FormData(this),
   contentType:false,
   processData:false,
   cache:false,
   success: function(data){
   $("#msg").html(data);
   }

});

});

});
</script>
  • 写回答

2条回答 默认 最新

  • dongxie548548 2017-06-28 00:47
    关注

    If you have

    <form id="upload_file_f"  ...
    
    <form id="upload_file_f"  .... '
    

    and

    $("#upload_file_f").on("submit", ....
    

    Well, you cant expect the forms to work because the ID's are supposed to be unique. Instead of re-using the Id, you must have a truly unique form id, or use a class or such as your jquery selector.

     $("form").on( "submit", ....
    
       //// $(this) within the on callback refers to the match of the selector that was clicked.
    

    if you look in your form callback there is no dependency there to the ID.

     <form class="myfrom" ....
    

    And

    $(".myform").on("submit",function(e){
        e.preventDefault();
    
        $.ajax({
           type: "post",
           url:"request.php",
           data:new FormData(this),
           contentType:false,
           processData:false,
           cache:false,
           success: function(data){
            $("#msg").html(data);
           }
    
        });
    });
    

    Should work fine. The only part that comes close to a dependency is this

    $("#msg").html(data);
    

    And likewise this could be a class ( .msg ) for example then you just do something like this

    $(".myform").on("submit",function(e){
        e.preventDefault();
    
        var that = $(this); //ajax scope placeholder
    
        $.ajax({
           type: "post",
           url:"request.php",
           data:new FormData(this),
           contentType:false,
           processData:false,
           cache:false,
           success: function(data){
                that.find(".msg").html(data);
           }
    
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致