Javascript 在按钮上单击从mysql数据库重新加载php,而不重新加载页面
我有一个.php页面,其中我使用基于cookie值的php显示MySQL数据库中的表 在此页面上,无需重新加载页面,我可以通过单击按钮更改cookie数据:Javascript 在按钮上单击从mysql数据库重新加载php,而不重新加载页面,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我有一个.php页面,其中我使用基于cookie值的php显示MySQL数据库中的表 在此页面上,无需重新加载页面,我可以通过单击按钮更改cookie数据: $(document).ready( function(){ $(".button").click(function () { Cookies.remove('cookie'); Cookies.set('cookie', 'value', { expires: 7 }); }); });
$(document).ready(
function(){
$(".button").click(function () {
Cookies.remove('cookie');
Cookies.set('cookie', 'value', { expires: 7 });
});
});
如何在相同的点击功能中刷新mysql SELECT,以便在不重新加载页面的情况下重新加载表中的数据
我已将我的php数据放入:
我已经阅读了所有我必须使用AJAX的地方——但我不能根据我所读的所有帖子来设置它。我非常感谢你的支持 您需要向pull-from.php文件中添加一些代码,或者创建另一个文件,使用它获取数据,这些数据可以接受AJAX调用中的参数并返回响应,最好是JSON。这基本上就是您的web服务 下面是一个php基本需要做的简单示例。我包括了一个html页面,它从web服务获取数据并显示数据
<?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
);
});
});
添加$.get'pull-from.php',functiondata{$'refresh-table'.htmldata;},基本上点击它!非常感谢你!我浪费了3个小时。但是学习。我已经检查了代码,实现了,并且工作得很有魅力。这是我需要经历更多的东西。非常感谢你在这里的帮助。