2 回答

TA貢獻1803條經(jīng)驗 獲得超6個贊
在開發(fā)工具中查看由 PHP 生成的代碼,它給出了這個(假設 $usernames='John' 在 PHP 中,并且只是一個名字,而不是一個數(shù)組)
<br><div style='width:100%; height: 30px;'><div class='userpic' style='background-image: url(); margin-left: 10px; float: left;'></div><p class='incomingfriendrequests' style='float: left; margin-top: 7px; margin-left: 8px;'>John</p><input type='submit' onclick='acceptfriendrequest(John)' style= 'margin-left: 10px; margin-top: 6.5px; float: left;' name='accept' value='Accept'><input type='submit' style= 'margin-left: 10px; margin-top: 6.5px; float: left;' name='deny'onclick='declinefriendrequest(John)' value='Deny'></div>)
查看接受好友請求和拒絕好友請求的調用。他們那里有 John,而不是字符串,而且它需要是一個字符串,正如一些評論員以某種方式指出的那樣。
需要更改的是 PHP,onclick='acceptfriendrequest($usernames)' 需要添加引號。
這是修改后的 PHP 回顯——我將元素拆分為換行符以使事情更清楚,并以基本方式添加必要的引號以顯示正在發(fā)生的事情:
echo "<br><div style='width:100%; height: 30px;'>
<div class='userpic' style='background-image: url($avatar); margin-left: 10px; float: left;'></div>
<p class='incomingfriendrequests' style='float: left; margin-top: 7px; margin-left: 8px;'>$usernames</p>
<input type='submit' onclick='acceptfriendrequest(".'"'.$usernames.'"'.")' style= 'margin-left: 10px; margin-top: 6.5px; float: left;' name='accept' value='Accept'>
<input type='submit' style= 'margin-left: 10px; margin-top: 6.5px; float: left;' name='deny'onclick='declinefriendrequest(".'"'.$usernames.'"'.")' value='Deny'>
</div>";

TA貢獻1856條經(jīng)驗 獲得超11個贊
我希望這對你來說很清楚:)
在 HTML onclick 中,您需要放置不帶任何參數(shù)或 () 的函數(shù):
// function without () or arguments
onclick=acceptfriendrequest
在 JavaScript 中使變量中的參數(shù)超出函數(shù)范圍,然后在函數(shù)中使用此變量作為參數(shù):
// variables as argument
let username = "Name";
// function without arguments
function acceptfriendrequest() {
// use variables in function :)
var fd = new FormData();
fd.append("username", username);
fd.append("accept", true);
var xhr = new XMLHttpRequest();
var fullurl = '../backend/friends.php';
xhr.open('POST', fullurl, true);
xhr.onload = function() {
if (this.status == 200) {
loadfriends();
};
};
添加回答
舉報