当用户单击图像时,我有一个叠加层出现在屏幕上。单击图像时,将执行一个 post ajax 请求,它应该返回结果moduleID,moduleName并返回pageID到<div id="result">,它也应该返回content到<div id="content">但是我什么都没有得到?
模块.php:
<div class="grid-2 dashboardIcons module">
<h3 class="fontAmaticH1">Critical Writing</h3>
<a class="cursor module" onclick="toggleWindow(); getModuleData(4)"><img value="4" src="images/CriticalWriting.png">
</a>
</div>
<div id="result"> <!-- Should be returning data from db here -->
</div>
<div id="content"> <!-- and here -->
</div>
模块测试AJAX.php:
<?php
require 'scripts/db.php';
$moduleID = $_POST['moduleID'];
$pageID = 1;
if(isset($_POST['moduleID']))
{
//$stmt = $conn->prepare ("SELECT * FROM `module` WHERE moduleID = ?");
$stmt = $conn->prepare ("SELECT `module`.`moduleID`, `module`.`moduleName`,`moduleContent`.`pageID`, `moduleContent`.`content` FROM `moduleContent` INNER JOIN `module` ON `module`.`moduleID` = `moduleContent`.`moduleID` WHERE `moduleContent`.`pageID` = ? AND `moduleContent`.`moduleID` = ? ");
$stmt->bind_param("ii", $pageID, $moduleID);
$stmt->execute();
$result = $stmt->get_result();
$output = [];
while($row = $result -> fetch_assoc()) {
$output["result"] = $row['moduleID'].' '.$row['moduleName'].' '.$row['pageID'];
$output["content"] = $row['content'];
}
echo json_encode($output);
}
?>
</div>
脚本.js:
// When user clicks open a module, pass the moduleID through in an ajax request to get data from the db
function getModuleData(moduleID){
console.log(moduleID);
$.ajax({
url: "moduleTestingAJAX.php",
method: "post",
data: {moduleID:moduleID},
success: function(data){
console.log(data);
$('#result').html(data.result);
$('#content').html(data.content);
}
});
console.log("test");
}
桌子上放着什么moduleContent
console.log(data);
在ajax 调用中运行后,我得到了这个返回:
汪汪一只猫