我基本上似乎不明白將變數發送到另一個頁面。我嘗試過 PHP 會話、javascript cookie 和 ajax POST 和 GET。我正在嘗試發送一個 div 的 innerHTML,其中包含由 jQuery 呼叫創建的資料,一個名為savedartists. 它在發送頁面的 console.log 中正確顯示,但$_POST['savedArtists']
在接收頁面中未定義。我花了幾個小時查看這個網站上的不同帖子,但我無法讓它作業。任何幫助表示贊賞。
<input class="et_pb_button et_pb_button_0 et_pb_bg_layout_light" onClick="savequote();" type="button" id="savedchoices" value="Commander la prestation" >
<script>
function savequote() {
var savedartists = document.getElementById('selectedList').innerHTML;
console.log(savedartists);
$.ajax({
type: "POST",
url: 'example.com/artiste/mise-en-relation/',
data: { savedArtists : savedartists },
success: function(data) {
console.log("success!");
location.href = "example.com/artiste/mise-en-relation/";
}
});
}
</script>
在接收頁面 (example.com/artiste/mise-en-relation/)
<?php
if(isset($_POST['savedArtists']))
{
$uid = $_POST['savedArtists'];
echo $uid;
} else {
echo 'zit!';
}
?>
謝謝你的時間
uj5u.com熱心網友回復:
捕捉作為未來讀者的答案......
從根本上說,這里發生的是對目標頁面的兩個請求。第一個是 AJAX 請求:
$.ajax({
type: "POST",
url: 'example.com/artiste/mise-en-relation/',
data: { savedArtists : savedartists },
success: function(data) {
//...
}
});
這是一個 POST 請求,其中包含您期望的資料,并且作業正常。 但是,此請求的結果將被忽略。該結果在success回呼中可用,但代碼對它不做任何事情:
console.log("success!");
location.href = "example.com/artiste/mise-en-relation/";
相反,代碼正在執行重定向。這會創建對同一頁面的第二個請求(盡管它本質上是同一頁面無關)。這是一個 GET 請求,不包含要發送到服務器的資料。
在最簡單的情況下,您應該使用AJAX或重定向用戶。目前你正在混合兩者。
我想重定向到另一個頁面。
在這種情況下,AJAX 是不適合這項作業的工具。您甚至可能根本不需要 JavaScript,除非您想在提交表單之前修改表單的元素/值。但是,如果您只想在將用戶引導到該頁面的同時將資料發布到另一個頁面,那么一個普通的舊 HTML 表單就可以做到這一點。例如:
<form method="POST" action="example.com/artiste/mise-en-relation/">
<input type="text" name="savedArtists">
<button type="submit">Submit</button>
</form>
在這種情況下,用戶輸入的任何值<input>都將包含在example.com/artiste/mise-en-relation/用戶提交表單時的 POST 請求中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/472803.html
標籤:javascript php jQuery 阿贾克斯 邮政
上一篇:將輸入值傳遞給asp-route
下一篇:如何在我的網站網址中包含字串?
