阿贾克斯将允许您通过JavaScript使用PHP脚本,而无需重新加载页面
让说,你有3个文件:
的application.js
$(document).ready(function(){
// add the click event to the checkbox
$('#TheCheckbox').click(function() {
// Looking if your checkox is checked
var Am_I_Checked = !$(this).is(':checked')
// Use javascrip to send an ajax call to your php file
var remoteFile = 'www.yourdomain.com/db_update.php';
// Data to send to your php file, The php file will see
// those vas in the $_GET array ...
// This is json format use all the time in javascript if you need more info
// google it :) .
// http://www.w3schools.com/json/json_intro.asp
var dataToSend = {
'Am_I_Checked' : Am_I_Checked,
'variable1' : 'the value',
'variable2' : 'the value'
// etc...
}
// This is the fonction that will be executed once the php call is done
// I know its look like a variable, but its a lamba style function, that
// also exist in php is you dont know
//
// If you notice, the data parameter is what will be return by php (echoed by ...)
// In this example that will be recognize as json
var oneAjaxIsDone = function(data) {
//example
alert(data.whatever); // in this example, that will alert the string : "you want to return"
}
$.ajax({
dataType: "json",
url: remoteFile, /* the remote script to call */
data: dataToSend, /* the json data of element that you want to send to your script */
success: oneAjaxIsDone /* there you pass your lamba */
});
})
})
的index.html
<html>
<header>
<script src="jquery.js">
<script src="application.js">
</header>
<body>
<input id="TheCheckbox" type="checkbox">
</body>
</html>
db_update.php
<?php
$ajaxVars = $_GET[];
// Do your database stuff .....
// Now we will build the return, remeber the ajaxIsDone function above...
$return = array('whatever' => 'you want to return');
// When you send data from javascript to php via ajax that was in json format
// we have to do the sameting here
$jsonString = json_encode($return);
echo $jsonString;
?>
重申不要问你,如果你使用AJAX或PHP的,因为他们没有做同样的工作
PHP:更新数据库,回报回答
html:containe you html and layout
javascrip PT:冰事件的复选框对象,并触发AJAX
AJAX:对于知道刚才把它看作你的颈部和你的服务器脚本
以及究竟是你的问题的出租车司机? – 2014-09-28 11:35:31
看起来你要求我们给你写一篇关于Ajax *教程的简介。已经有很多了。试试Google。 – Quentin 2014-09-28 11:36:28
我使用ajax,但更新数据库通过重新加载页面 – 2014-09-28 11:38:03