我在 html 檔案中有一個帶有“orderdetail”類的按鈕。我想通過 jquery 獲取它的 id 并將其發送到 php 檔案,然后重定向到該 php 檔案,然后我通過 POST 處理該 id,但我總是收到“注意:未定義索引:id in C:\laragon\www\portal \orderDetail.php 第 9 行”錯誤。這是我的代碼:
在 html 檔案中:
$(document).on('click', '.orderdetail', function () {
var _id = $(this).attr("id");
console.log(_id); //I got the true result here
$.post("orderdetail.php", { id: _id }, function (data, status, xhr) {
window.location.href = "orderdetail.php";
})
.... more code not shown
在 php 檔案中: echo $_POST['id'];
有什么問題?
uj5u.com熱心網友回復:
您可以使用jquery.redirect庫同時發送帶有重定向的 POST 資料。
匯入jQuery后,添加這個檔案:https : //github.com/mgalante/jquery.redirect/blob/master/jquery.redirect.js
然后在 JS 中使用它,如下所示:
$.redirect('file.php', {'First': 'Hello', 'Second': 'World'});
在你的情況下:
$.redirect('orderdetail.php', {'id': _id});
這與重定向一起發布資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/334377.html
上一篇:如何在Nuxt.js中獲得StackOverflowSEO友好的URL結構?
下一篇:getInitialProps在我的實時站點上導致ERR_TOO_MANY_REDIRECTS錯誤,但在我的本地版本上沒有
