2016-01-29 90 views
-1

我有一个.php页面,我从MySQL数据库显示表使用基于cookie值的PHP。按钮点击重新从MySQL数据库重新加载一个php拉没有页面重新加载

在此页面,无需重新加载页面我能够更改cookie数据通过单击按钮:

$(document).ready(
function(){ 
    $(".button").click(function() { 
     Cookies.remove('cookie'); 
     Cookies.set('cookie', 'value', { expires: 7 }); 

    }); 

}); 

如何刷新mysql的选择在相同的点击功能,以便重新加载表里面的数据,而无需重新加载页面?

我有我的PHP数据:

<div id="refresh-table"> <?php include 'pull-from.php'; ?> </div>

我看了遍我必须使用AJAX的地方 - 但我不能根据所有帖子我已经通过其设置。我会真正appriciate你的支持。

+0

add'$ .get('pull-from.php',function(data){$('#refresh-table')。html(data); }',基本上 –

+0

明白了!非常感谢!我浪费了3个小时。但是学习。 –

回答

0

您需要将一些代码添加到您的pull-from.php文件(或创建另一个使用它来获取数据的文件),该文件可以接受来自AJAX调用的参数并返回响应,最好是JSON。这基本上是你的网络服务。

以下是关于php基本需要做什么的准系统示例。我包含一个从网页服务提取数据并显示它的html页面。

<?php 
$criteria = $_POST['criteria']; 

function getData($params) { 
    //Call MySQL queries from here, this example returns static data, rows  simulate tabular records. 

    $row1 = array('foo', 'bar', 'baz'); 
    $row2 = array('foo1', 'bar1', 'baz1'); 
    $row3 = array('foo2', 'bar2', 'baz2'); 

    if ($params) {//simulate criteria actually selecting data 
     $data = array(
      'rows' => array($row1, $row2, $row3) 
     ); 

    } 
    else { 
     $data = array(); 
    } 

    return $data; 
} 

$payload = getData($criteria); 

header('Content-Type: application/json');//Set header to tell the browser what sort of response is coming back, do not output anything before the header is set. 
echo json_encode($payload);//print the response 
?> 

的HTML

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Sample PHP Web Service</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script> 
<script> 
    $(document).ready(function(){ 
     alert("Lets get started, data from the web service should appear below when you close this."); 

    $.ajax({ 
     url: 'web-service.php', 
     data: {criteria: 42},//values your api needs to query data 
     method: 'POST', 
    }).done(function(data){ 
     console.log(data);//display data in done callback 
     $('#content').html(
     JSON.stringify(data.rows)//I just added the raw data 
     ); 
    }); 

}); 

+0

我已经完成了代码,实现并像魅力一样工作。这是我需要通过更多的东西。非常感谢在这里的帮助。 –

相关问题