我正在尝试在窗口加载事件发生时使用AJAX从数据库中获取值。当我访问PHP脚本时,我在屏幕上获得了正确的值,但是当我尝试通过AJAX将其传递到它应该是的另一个页面时,我只得到HTML而不是PHP值。请帮我。从昨天开始,我很沮丧地试图解决这个问题!
这是我的ajax脚本:
<script type="text/javascript">
function getReplies() {
var commentDiv = document.getElementById("reply_comment");
var xhr = new XMLHttpRequest();
xhr.open('POST', "widgets/board_reply_fetch.php?comment_id=<?php echo $board_comment_id_number;?>", true);
// when we make POST request, we must add this first line
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
var result = xhr.responseText;
//result2 = result.split('/');
console.log('Result: ' + result);
commentDiv.innerHTML = result;
}
};
xhr.send("comment_id=<?php echo $board_comment_id_number;?>");
}
window.addEventListener("load", getReplies);
</script>
这是我的php脚本:
<?php
require_once '../includes/session.php';
require_once '../includes/functions.php';
require_once '../includes/validation_functions.php';
$comment_id = isset($_GET['comment_id']) ? (int)$_GET['comment_id'] : '';
$reply_data = find_board_replies_by_comment_id($comment_id);
$reply_assoc = mysqli_fetch_assoc($reply_data);
$reply_comment_id = $reply_assoc['comment_id'];
$reply_board_id = $reply_assoc['board_id'];
$reply_user_id = $reply_assoc['user_id'];
$reply_text = $reply_assoc['reply'];
$reply_timestamp = $reply_assoc['reply_timestamp'];
$reply_user_data = find_user_data_by_id($reply_user_id);
$profile_image = $reply_user_data['profile_picture'];
$profile_image_thumb = "uploaded_pictures/profile/$reply_user_id/" . $reply_user_id . "small.png";
if ($profile_image == "") {
if ($reply_user_data['gender'] == "Male"){
$user_profile_picture = "images/ProfilePicMale.png";
} else {
$user_profile_picture = "images/ProfilePicFemale.png";
}
} else {
$user_profile_picture = $profile_image_thumb;
}
$full_name = ucfirst(htmlentities($reply_user_data['first_name'])) . " " . ucfirst(htmlentities($reply_user_data['last_name']));
$time_of_post = time_of_post($reply_timestamp);
$the_reply_text = nl2br($reply_text);
?>
<div class="reply_comment_div">
<a href="profile.php?user_id=<?php echo $reply_user_id;?>" class="board_comments_div_picture">
<img src="<?php echo $user_profile_picture;?>" width="50px" height="50px" /></a>
<a href="profile.php?user_id=<?php echo $reply_user_id;?>" class="board_comments_reply_link"><?php echo $full_name;?></a>
<a href="edit_comment_board.php?comment_id=<?php echo $reply_comment_id;?>" class="edit_comment_button_board">Edit</a>
<a href="widgets/delete_board_comment.php?board_id=<?php echo $reply_board_id;?>&comment_id=<?php echo$reply_comment_id;?>" onclick="return confirm('Are you sure you want to delete this admin?')" class="delete_button_status">Delete</a>
<div class="board_comment_submited_on">submitted <?php echo $time_of_post;?></div>
<span class="comment_content_span"><?php echo $the_reply_text;?></span>
</div>
答案 0 :(得分:0)
我可以在这里看到几个问题:
xhr.open
$_GET
访问有效负载,该{1}}用于在GET
请求中获取查询字符串参数。您应该使用$_POST
代替。修改这两项应该可以使事情发挥作用。
话虽如此,我建议您使用GET请求。由于数据库状态未被更改(不执行创建或更新操作),因此GET请求更有意义。