我有一個html頁面,其中有一個搜索欄位,可以輸入一個名字,回傳一個名字和ID的表格(特別是來自Steam)。最左邊的一列,也就是名字,是我想要的超鏈接,當點擊時,會將用戶帶到該玩家的個人資料(profile.php),當點擊名字鏈接時,我想要將 "名字 "和 "steamid "發送到profile.php,所以基本上是將兩個JS變數從一個頁面發送到另一個頁面的PHP后端。
我是ajax的新手,而這似乎是使用它的唯一方法,所以在研究了一段時間后,我得出了這樣的結論:
我是ajax的新手,而這似乎是使用它的唯一方法。
$(document).ready(function(>/span>) {
$('#playerList td').click(function(e) {
if ($(this).text() == $(this) 。 closest('tr').children('td:first').text() } {
console.log($(this).text()) 。
var name = $(this).text() 。
var steamid = $(this).closest('tr')。 children('td:nth-child(2)'/span>).text()。
$.ajax({
url: 'profile.php'。
method: 'POST',
data: {
playersSteamID : steamid,
playersName : 名字
},
success: function() {
console.log('success')。
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest)。
console.log(textStatus)。
console.log(errorThrown)。
}
})
}
});
});
在ajax定義之前的所有內容都按我的要求進行,我想將 "name "和 "steamid "變數發送到profile.php,但我認為我沒有理解ajax的作業原理。我的理解是,ajax可以 "發布 "資訊(通常是我所看到的json物件)到一個url,但也可以從一個頁面回傳資訊?這就是我有點困惑的地方,我想知道我是否用錯了它。
作為一個簡短的說明:playerList是我的表的ID。
當我點擊超鏈接時,它把我帶到了profile.php,這是我想要的,但是php的$_POST[]陣列似乎是空的/空的,因為當我試圖訪問'playerSteamID'和'playerName'時得到了 "未定義陣列鍵 "的錯誤。var_dump()也回傳空,所以我在想是否在ajax中發送資料{}域的方式有問題。我還是個新手,如果有任何幫助,我將非常感激。
Update: How I'm accessing the variables in profile.php
<?php
echo var_dump($_POST['playerName'])。
echo var_dump($_POST['playerSteamID'])。
if (isset($_POST['playerName']>) {
console_log("PLAYER_NAME => ".$_POST['playerName'])。
}
if (isset($_POST[' playersSteamID'])) {
console_log("PLAYER_STEAMID => ".$_POST['playerSteamID'])。
}
? >
profile.php的其余部分是利用這些變數,運行幾個sql查詢,并建立一個表,這在給定適當的變數時是有效的,但由于$_POST[]是空的,我不能繼續超過上述點,因為前兩行回傳空,而且條件永遠不會是真的,因為isset()是假的。
uj5u.com熱心網友回復:
ajax用于,post或get引數/資訊從/到URL,而無需重定向/重繪 /導航到一個特定的頁面。
請注意沒有重定向/重繪 /導航
在你的案例中,你想向profile.php發送2個引數,你還想導航到它,是嗎?但為此你使用了Ajax,這不是一個正確的選擇。
解決方案:-
->使用正常的表單提交方式,并將引數發布到profile.php,在這種情況下,你將獲得重定向到profile.php,并可以期待適當的功能。
你可以使用一個帶有提交按鈕的正常表單{非常正常},或者使用一個自定義函式來提交表單,如果需要做一些進一步的作業{表單驗證}。
你的函式應該是這樣的......
你的函式應該是這樣的。
$(document).ready(function(>/span>) {
$('#playerList td').click(function(e) {
e.preventDefault()。
if ($(this).text() == $(this) 。 closest('tr').children('td:first').text() } {
console.log($(this).text();
var name = $(this).text() 。
var steamid = $(this).closest('tr')。 children('td:nth-child(2)')。text()。
//做一些其他的事情。
document.forms['someform'].submit(); //submitting the form here>
}
});
});
在profile.php中的其余部分保持不變。
->如果你真的想使用ajax,請執行以下操作。
Ajax是用于動態網頁設計的,在不重繪 頁面的情況下從服務器上獲取資料是非常有用的。
你應該改變你在profile.php檔案中的回應方式。
例如:-
<?php
if(isset($_POST['playerSteamID']){
$name = $_POST['playerName'] 。
$id = $_POST['playerSteamID'] 。
//Do your stuff here db query whatever.
//Here echo out the needed html/json response.
echo "<h3>"/span>.$name."</h3>"。
echo "<h4>"/span>.$playersSteamID."</h4>"/span>。
}
?>.
回應{from: echo}將在ajax成功的function(data)的data中提供,你可以在任何你想要的地方使用這個data。
例如:-
success: function(data){
console.log(data);//for debugging。
$('#mydiv').html(data);//appending the response.
}
->使用php會話,并將steamID存盤在sesssion變數中,如果你的網站有一些登錄功能,則非常有用。
$_SESSION['steamID'] = //the steamID here;
這個變數可以在網站使用的任何地方通過呼叫session_start()來使用,你要使用session。
例如:-
<a href="file. php" target="_self"> 點擊這里查看您的個人資料 </a>
profile.php
<?php
session_start()。
$id = $_SESSION['steamID'/span>]。
//echo var_dump($_POST['playerName']) 。
//echo var_dump($_POST['playerSteamID'])。
/*if (isset($_POST['playerName']) {
console_log("PLAYER_NAME => ".$_POST['playerName'])。
}
if (isset($_POST['playerSteamID']) {
console_log("PLAYER_STEAMID => ".$_POST['playerSteamID'])。
}*/
echo $id;
//在這里做你的作業......
?">。
如果有任何疑問,請留言。
uj5u.com熱心網友回復:
一個超鏈接可能會導致頁面導航,這是你不想要的。
頁面導航會發出一個獲取請求,但你的端點期待的是一個發布請求。
你可以通過將href設定為#或者完全洗掉<a>來停止導航。
你也可以嘗試在事件物件上呼叫preventDefault。
$(document).ready(function(>/span>) {
$('#playerList td').click(function(e) {
e.preventDefault()。
if ($(this).text() == $(this) 。 closest('tr').children('td:first').text() } {
console.log($(this).text();
var name = $(this).text() 。
var steamid = $(this).closest('tr')。 children('td:nth-child(2)'/span>).text()。
$.ajax({
url: 'profile.php'。
method: 'POST',
data: {
playersSteamID : steamid,
playersName : 名字
},
success: function(data) {
console.log('success', data) 。
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest)。
console.log(textStatus)。
console.log(errorThrown)。
}
})
}
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/323828.html
標籤:
上一篇:用Ruby和R創建Docker
