php 接口与前端数据交互实现示例代码(3)

<!--新增弹框--> <div tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"> <div role="document"> <div> <div> <button type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4>用户新增</h4> </div> <div> <form method="post"> <div> <label for="userName">用户名称</label> <input type="text"> </div> <div> <label for="userAge">用户年龄</label> <input type="text"> </div> <div> <label for="user-sex">用户性别</label> <div> <select > <option value='-1'>请选择</option> <option value='0'>男</option> <option value='1'>女</option> </select> </div> </div> </form> </div> <div> <button type="button" data-dismiss="modal">关闭</button> <button type="button">保存</button> </div> </div> </div> </div>

var $table = $('#table'), $remove = $('#remove'); $(function() { searchData(); delData(); $('#save').click(function(){ addData(); }); }); function addData(){ var userName = $('#userName').val(); var userAge = $("#userAge").val(); var userSex = $('#user-sex').val() == '0' ? '男' : '女'; var addUrl = "./php/data.php?action=add_row&user_name=" + userName + "&user_age=" + userAge + "&user_sex=" + userSex; $.ajax({ type:"post", url:addUrl, dataType:'json', contentType:'application/json;charset=utf-8', success:function(data){ console.log("success"); }, error:function(data){ console.log("data"); //添加成功后隐蒧modal框 setTimeout(function(){ $('#exampleModal').modal('hide'); },500);               //隐藏modal框后重新加载当前页 setTimeout(function(){ searchData(); },700); } }); }

至此,还没有解决如下问题:

1.表单验证;

2.添加多条数据后,php如何接收参数;

3.新增成功后,在$.ajax的方法中,为什么,新增成功后的其它操作要在 error 这个对象中实现?而不是在 sucess 中实现?

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/08f915f97b5b24d0950a1ff5a9f9785f.html