2 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
你使用 XMLHttpRequest 的方式不對(duì)。您應(yīng)該使用 2 個(gè)不同的頁(yè)面:調(diào)用者 (index.php) 和異步腳本 (tempo.php) 更正您當(dāng)前的調(diào)用者頁(yè)面: index.php : ? 使用不帶任何參數(shù)的 url:
url="tempo.php"
? 一起發(fā)送您的兩個(gè)參數(shù):
req.send("a="+dayName+"&b="+dayName);
要調(diào)試異步頁(yè)面:tempo.php,只需在 tempo.php 頂部添加一個(gè)偽造的 get_parameter:
a = a_possible_value_for_a
然后直接在瀏覽器中調(diào)用 tempo.php(沒(méi)有你的 ajax 頁(yè)面)

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
從 HTML 文件發(fā)送的請(qǐng)求。
發(fā)送過(guò)程一:
$(document).on("click","#btn1",function(){
var data = $(this).val();
/* ajax request sent start */
$.ajax({
method:"post",
url:"phpFileName.php",
data:{backendPostName:data},
dataType:"json",
success:function(response){
/* Logic implemented here */
}
});
/* ajax request sent end*/
});
根據(jù)你的html結(jié)構(gòu)發(fā)送過(guò)程二:
function clickMe(data){
var data = $(this).val();
/* ajax request sent start */
$.ajax({
method:"post",
url:"phpFileName.php",
data:{backendPostName:data},
dataType:"json",
success:function(response) {
/* Logic Implementation here */
}
});
/* ajax request sent end*/
}
當(dāng)您想在 php 文件中接收此發(fā)送數(shù)據(jù)時(shí)。
首先通過(guò)php“isset()”函數(shù)檢查是否找到這個(gè)名字
下面的例子:
php 文件:
<?php
if(isset($_POST['backendPostName'])){
$customName = $_POST['backendPostName'];
/*
store or other logic implement here .
if you wants to echo html then echo "success"; or your choice
if you wants to return json data then return json_encode(["result"=>1]);
*/
/* For HTML Return */
echo "<h1>Success</h1";
/*For Json return */
echo json_encode(["result"=>1]);
}
?>
- 2 回答
- 0 關(guān)注
- 205 瀏覽
添加回答
舉報(bào)