此问题已在此处有答案:
What is the difference between client-side and server-side programming?(3个答案)
5天前关闭。
你好,我在我的“Functions.php”页面上有一个函数:
function displaySearchedGame($game) {
if (empty($game)) {
echo '<p>No games found.</p>';
return;
}
echo '<ul class="searchedGamesList">';
foreach ($game as $game) {
echo '<li class="searchedGame">';
// Display the cover image if URL is defined
if ($game['cover_url']) {
echo '<img src="' . $game['cover_url'] . '" alt="Game Cover">';
}
echo '<div class="game-details">';
echo '<h3>' . $game['game_name'] . '</h3>';
echo '<p>Platform: ' . ($game['platform_name'] ?? 'Unknown Platform') . '</p>';
echo '</div>';
echo '</li>';
}
echo '</ul>';
}
我试着在dashboard.php上的JavaScript的click函数中调用它:
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('searchButton').addEventListener('click', function () {
var searchTerm =document.getElementById('search').value;
var xhr = new XMLHttpRequest();
xhr.open('POST', 'Functions.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange =function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
displaySearchedGame(response);
}
};
xhr.send('action=searchGame&searchTerm=' + encodeURIComponent(searchTerm));
});
});
我的Functions.php没有识别出我在JavaScript中调用了DisplaySearchedGame(),并且我一直得到错误“DisplaySearchedGame”not defined。
我尝试了许多不同的方法来调用函数。我甚至试着在dashboard.php上从我的php调用它,但它只返回“null”。我想让它做的是使用searchGame()来搜索游戏:
function searchGame($searchTerm) {
// Prepare the URL for the AJAX request
$url = "search_game.php?searchTerm=" . urlencode($searchTerm);
// Initialize cURL session
$ch = curl_init();
// Set the cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
// Execute the cURL request and get the response
$response = curl_exec($ch);
// Check for errors and close the cURL session
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
return "Error: " . $error;
}
// Close the cURL session
curl_close($ch);
// Return the response
return $response;
}
并归还了搜索的游戏。我希望函数在functions.php上,这样我就可以从另一个页面使用/调用它们。
1条答案
按热度按时间inkz8wg91#
您的代码应类似于以下内容
服务器端Functions.php
在客户端