我有一個關于ajax和PHP的問題。當我嘗試從ajax向PHP發送資料時,ajax似乎起作用了,因為一個警報器正確地顯示了資料(盡管在很短的時間內,接下來就消失了),但在我的php檔案中沒有顯示任何資料(我用開發者工具檢查,沒有結果)。 這是我的Ajax腳本
。$(document).ready(function(>/span>) {
$('#tabla_eventos') 。 on('click', '#guardar_bt', function(e){
e.preventDefault()。
var filaactual = $(this).closest("tr")。
var id_evento = filaactual.find("td:eq(0)").html() 。
var parametros = {
id_evento : id_evento
};
$.ajax( {
method: 'POST'。
data: parametros,
url: 'detalle_evento.php'。
success: function (response) {
location.href='detalle_evento.php'。
alert('bien' ' id_evento)。
}
});
});
});
而這是我的php檔案:
session_start()。
<html lang="en">
<head>/span>
<meta charset="UTF-8">/span>
< meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=devicewidth, initial-scale=1. 0">
<title>Document</title>
</head>/span>
<body>
<h1>
echo $_POST['id_evento']。
</h1>
</body>
</html>
uj5u.com熱心網友回復:
問題出在你的代碼中的這一行:
location.href='detalle_evento.php';
在腳本收到來自ajax的資料后,頁面在POST中以空資料重新加載(因為重新加載是一個GET請求)。
如果你想以這種方式使用這些資料,那么請在你的HTML中添加這段代碼(在body部分)。
<formaction="detalle_evento. php" method="post" id="someform">
< input type="hidden" name="id_evento" id="id_evento" value=">< ? = $_POST['id_evento'] ? >">>
</form>/span>
并使用這個JS代碼
document. getElementById("id_evento").value = id_evento;
document.getElementById("someform"/span>).submit()。
代替這部分代碼
$.ajax( {
method: 'POST'。
data: parametros,
url: 'detalle_evento.php'。
success: function (response) {
location.href='detalle_evento.php'。
alert('bien' ' id_evento)。
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/320477.html
標籤:
