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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛