2016-01-29 177 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

我已經完成了代碼,實現並像魅力一樣工作。這是我需要通過更多的東西。非常感謝在這裏的幫助。 –