doumi1944 2015-07-09 06:47
浏览 176
已采纳

使用JQuery Ajax保存数据以避免页面重新加载

At the moment I save data when changes are made on the page using ordinary PHP but of course it makes the page reload every time which resets everything. I really need to do it without page reload. I've heard its possible to do with Ajax and JQuery. I would much prefer Jquery answers rather than Javascript.

  • 写回答

2条回答 默认 最新

  • douwudie8060 2015-07-09 07:29
    关注

    for example you have this form

    <input type="text" name="name" class="abc" id="test" placeholder="Name">
    <a href="#" class="PopUpSendBtn" id="sendinfo">Send</a>
    

    then write Script

    <script>
        $(document).ready(function() {
       // invoke event on clicking send button
       $("#sendinfo").on("click", function(evt) {
       // get value of input field
    var userName = $('#test').val();
    
                             jQuery.ajax({
                    url: filename.php,
                    type:'POST',
                    async: false,
                    data:{'userName':userName},
                    success: function(output_string){
                        //    alert(output_string);
                        if(output_string == 1){
                            // do someting
                        } else { 
                            // do someting
                        }
                       }
                       , error: function(object, status, response) {
                        alert(response); 
                    }
                    });
    
        });
    });
    </script>
    

    your php file(filename.php) get data using post request

     $data = $_POST['userName'];
    // query to store data in db
    // after saving data successfully in db your can send back a success message //in json or any other format (in my case i am using json)
             $data['success'] = TRUE;
            echo json_encode($data);
    

    Don't forget to include jquery script.

    This is the basic idea how to send data using ajax. You can modify it according to your requirements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行