使用AJAX,你必須先在您的文檔的頭部jQuery的,然後把下面的下面的代碼在最後的標籤(</body>
標籤前)
然後,您可以編輯數據變量以將數據作爲$ _POST發送。要訪問這個腳本發送給你的php腳本的數據,你只需調用$ _POST變量。例如:訪問var2,放入你的php腳本$ _POST ['var2']。如果您使用GET而不是post,請記住在您的php腳本中使用$ _GET ['var2']來獲取該變量。
var data = 'var1=value&var2=value2&var3=value3';
$.ajax({
type: "POST", //can be POST or GET
url: "URL_TO_SCRIPT_GOES_HERE.php",
dataType: "html", //or json
data: data, //data to send as $_POST to script
success: function(response) {
//Once data received, do this
alert(response);
},
error: function(response) {
}
});
數據庫連接線應放在php文件中。它與jquery ajax腳本無關。
要執行的Ajax調用,您可以運行它,我怎麼也得在這裏(它會加載在頁面加載),或者你可以把它放在一個JavaScript函數,並調用它根據一個事件:
function my_ajax_call() {
var data = 'var1=value&var2=value2&var3=value3';
$.ajax({
type: "POST", //can be POST or GET
url: "URL_TO_SCRIPT_GOES_HERE.php",
dataType: "html", //or json
data: data, //data to send as $_POST to script
success: function(response) {
//Once data received, do this
alert(response);
},
error: function(response) {
}
});
}
一的onclick按鈕觸發:
<a onclick='my_ajax_call()'>CALL AJAX</a>
該代碼是htaccess的,一無所知的js,但是對於PHP有PHP擴展 – Sam