gpt4 book ai didi

javascript - AJAX PHP 数据库查询不起作用

转载 作者:行者123 更新时间:2023-11-29 07:27:52 24 4
gpt4 key购买 nike

我正在尝试学习 AJAX,但进展不太顺利。在我的网站上,用户可以请求培训。提交请求后,教师应该能够从此页面管理该请求。然而,当尝试“声明”请求时,AJAX 无法正常运行,没有错误。

  • 内部 html 不变
  • 数据库条目未更改
  • 我知道 php 文件正在运行,因为我之前收到了错误

PHP/HTML/Javascript/AJAX

<table class="sidebar" style="width:50%;">
<tr><th>My Open Requests</th></tr>
<div id="myopen">
<?php
if(!empty($myopen)){
foreach($myopen as $request){
$date = date('F d Y - gA', $request['submitted']);
echo '<tr><td>'.$request['vid'].' - '.$request['type'].' - '.$request['comments'].' - '.$date.'</td></tr>';
}
}
else{
echo '<tr><td>NONE</td></tr>';
}
?>
</div>
</table>

<table class="sidebar" style="width:50%;">
<tr><th>My Closed Requests</th></tr>
<?php
if(!empty($myclosed)){
foreach($myclosed as $request){
$date = date('F d Y - gA', $request['submitted']);
echo '<tr><td>'.$request['vid'].' - '.$request['type'].' - '.$request['comments'].' - '.$date.'</td></tr>';
}
}
else{
echo '<tr><td>NONE</td></tr>';
}
?>
</table>

<table class="sidebar" style="width:50%;">
<tr><th>Unclaimed Requests</th></tr>
<div id="unclaimed">
<?php
if(!empty($unclaimed)){
foreach($unclaimed as $request){
$date = date('F d Y - gA', $request['submitted']);
$id = $request['id'];
$name = "'".$this->registry->getData('firstname').' '.$this->registry->getData('lastname')."'";
echo '<tr><td>'.$request['vid'].' - '.$request['type'].' - '.$request['comments'].' - '.$date.' <a href="" onclick="claim('.$id.','.$name.')">Claim</a></td></tr>';

}
}
else{
echo '<tr><td>NONE :)</td></tr>';
}
?>
</div>
</table>
<table class="sidebar" style="width:50%;">
<tr><th>Request Log</th></tr>
<?php
if(!empty($claimed)){
foreach($claimed as $request){
echo '<tr><td>'.print_r($request).'</td></tr>';
}
}
else{
echo '<tr><td>NONE</td></tr>';
}
?>
</table>
<script>
function claim(id, name){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myopen").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("POST", "/lib/AJAX/trainingRequestManage.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("id="+id+"&name="+name);
}
</script>

trainingRequestManage.php

<?php
$id = $_POST["id"];
$id = $_POST["name"];

include('http://www.ivaoxa.org/config/db.php');

$sql = "UPDATE trainingRequests SET trainer='".$name."' WHERE id=".$id;

// Prepare statement
$stmt = $db->prepare($sql);

// execute the query
$stmt->execute();

echo 'test';

db.php

<?php
$db = new PDO('mysql:host=localhost;dbname=xx;charset=utf8', 'xx', 'xx');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

最佳答案

除非http://www.ivaoxa.org/config/db.php作为普通文件提供,否则以下行永远不会工作:

include('http://www.ivaoxa.org/config/db.php');

更多信息:http://php.net/manual/en/function.include.php

关于javascript - AJAX PHP 数据库查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33906952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com