douzuo0002 2017-11-06 07:29
浏览 77
已采纳

如何在同一页面上显示表单发布数据异步(没有刷新页面)?

i am working on small project.. In one module, i need to do some entries using multiple forms. and i need that added form data(respective table data) to be displayed in same page after adding each value. So, that user will get to know what data he inserted.

Pls look at the code.

<div class="container">
<div class="row">
<br><br>
<h4> School Name:   <?php echo $sn; ?>
</h4>
</div>
</div>
 <div class="container col-md-offset-1 col-md-7">
  <h3>Budget/Students Count:</h3>
  <br>
  <ul class="nav nav-tabs">
    <li class="active"><a href="#home">K6</a></li>
    <li><a href="#menu1">K7</a></li>
    <li><a href="#menu2">K8</a></li>
    <li><a href="#menu3">K9</a></li>
    <li><a href="#menu4">K10</a></li>
    <li><a href="#menu5">K11 SC</a></li>
    <li><a href="#menu6">K12 SC</a></li>
    <li><a href="#menu7">K11 NSC</a></li>
    <li><a href="#menu8">K12 NSC</a></li>
  </ul>
 
  <div class="tab-content">
    <div id="home" class="tab-pane fade in active">
      <form name="add_name" id="add_name" class="col-md-5">
        <table class="table" id="dynamic_field">
        <tr>
        <td><label style="font-size:10px;"></label></td>
        <td><label style="font-size:10px;"></label></td>
        <td><label style="font-size:12px;">Section</label></td>
        <td><label style="font-size:12px;">#of students</label> </td>
        <td><label style="font-size:10px;"></label></td>
        </tr>
        <tr>
        <td> <input type="text" name="idref" id="idref" class="form-control input-xs" value="<?php echo $idk; ?>" style=" width:35px; height:20px; visibility: hidden;"> </td>
        <td> K6: <input type="text" name="clas" id="clas" class="form-control input-xs" value="6" style=" width:35px; height:20px; visibility: hidden;"> </td>
        <td> <input type="text" name="k[]" id="k" class="form-control k_list " style="width:45px; height:28px"> </td>
        <td> <input type="text" name="name[]" id="name" class="form-control name_list" style="width:80px; height:28px"> </td>
        <td> <button type="button" name="add" id="add" class="btn btn-success btn-xs">add</button> </td>
        </tr>
        </table>
        <input type="submit" name="submit" id="submit" value="submit" class="btn-success btn-xs" style="margin-left:170px;">
        <input type="reset" name="Reset" class="btn-success btn-xs">
        </form>
    </div>
    <div id="menu1" class="tab-pane fade">
      <form name="add_name1" id="add_name1" class="col-md-5">
        <table class="table" id="dynamic_field1">
        <tr>
        <td><label style="font-size:10px;"></label></td>
        <td><label style="font-size:10px;"></label></td>
        <td><label style="font-size:12px;">Section</label></td>
        <td><label style="font-size:12px;">#of students</label> </td>
        </tr>
        <tr>
        <td> <input type="text" name="idref" id="idref" class="form-control input-xs" value="<?php echo $idk; ?>" style=" width:35px; height:20px; visibility: hidden;"> </td>
        <td> K7: <input type="text" name="clas" id="clas" class="form-control input-xs" value="7" style=" width:35px; height:20px; visibility: hidden;"> </td>
        <td> <input type="text" name="k[]" id="k" class="form-control k_list" style="width:45px; height:28px"> </td>
        <td> <input type="text" name="name[]" id="name" class="form-control name_list" style="width:80px; height:28px"> </td>
        <td> <button type="button" name="add" id="add1" class="btn btn-success btn-xs">add</button> </td>
        </tr>
        </table>
        <input type="submit" name="submit" id="submit1" value="submit" class="btn-success btn-xs" style="margin-left:170px;">
        <input type="reset" name="Reset" class="btn-success btn-xs">
        </form>
    </div>
    <div id="menu2" class="tab-pane fade">
      <form name="add_name2" id="add_name2" class="col-md-5">
        <table class="table" id="dynamic_field2">
        <tr>
        <td><label style="font-size:10px;"></label></td>
        <td><label style="font-size:10px;"></label></td>
        <td><label style="font-size:12px;">Section</label></td>
        <td><label style="font-size:12px;">#of students</label> </td>
        </tr>
        <tr>
        <td> <input type="text" name="idref" id="idref" class="form-control input-xs" value="<?php echo $idk; ?>" style=" width:35px; height:20px; visibility: hidden;"> </td>
        <td> K8: <input type="text" name="clas" id="clas" class="form-control input-xs" value="8" style="width:35px; height:20px; visibility: hidden;"> </td>
        <td> <input type="text" name="k[]" id="k" class="form-control k_list" style="width:45px; height:28px"> </td>
        <td> <input type="text" name="name[]" id="name" class="form-control name_list" style="width:80px; height:28px"> </td>
        <td> <button type="button" name="add" id="add2" class="btn btn-success btn-xs">add</button> </td>
        </tr>
        </table>
        <input type="submit" name="submit" id="submit2" value="submit" class="btn-success btn-xs" style="margin-left:170px;">
        <input type="reset" name="Reset" class="btn-success btn-xs">
        </form>
    </div>

Script:

$(document).ready(function() {
    $(".nav-tabs a").click(function(){
        $(this).tab('show');
    });
});

$(function() {
    var i = 1;
    $("#add").click(function() {
    i++;
    $('#dynamic_field').append('<tr id="row'+i+'"><td></td><td></td><td> <input type="text" name="k[]" id="k'+i+'" class="form-control name_list" style="width:35px; height:20px" > </td><td> <input type="text" name="name[]" id="name" class="form-control name_list" style="width:55px; height:20px"> </td><td> <button name="remove" id="'+i+'" class="btn btn-danger btn_remove btn-xs">X</button> </td></tr>');
});
$(document).on('click','.btn_remove', function(){
    var button_id = $(this).attr("id");
    $('#row'+button_id+'').remove();
});
$("#add1").click(function() {
    i++;
    $('#dynamic_field1').append('<tr id="row'+i+'"><td></td><td></td><td> <input type="text" name="k[]" id="k'+i+'" class="form-control name_list" style="width:35px; height:20px" > </td><td> <input type="text" name="name[]" id="name" class="form-control name_list" style="width:55px; height:20px"> </td><td> <button name="remove" id="'+i+'" class="btn btn-danger btn_remove btn-xs">X</button> </td></tr>');
});
$(document).on('click','.btn_remove', function() {
    var button_id = $(this).attr("id");
    $('#row'+button_id+'').remove();
});
$('#submit').click(function() {
    $.ajax({
        url: "section.php",
        data: $('#add_name').serialize(),
        success: function(data) {
            alert(data);
            $('#add_name')[0].reset();
        }
    });
});
    
$('#submit1').click(function() {
    $.ajax({
        url: "section.php",
        data: $('#add_name1').serialize(),
        success: function(data) {
            alert(data);
            $('#add_name1')[0].reset();
        }
    });
});

In section.php i have written sql query insertion. Now what i need is, i need this entered data to be displayed in same page. and table should be updated every time when i add new value.

</div>
  • 写回答

1条回答 默认 最新

  • donglou8371 2017-11-06 07:49
    关注
    $.ajax({
    
        type:"GET",
    
        url:"path/to/file",
    
        data: 'your data',
    
        success:function(html){
    
            $('.response').html(html); //this return response to your page
        }
     })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)