AJAX PHP数据库查询不起作用

时间:2015-11-25 01:22:43

标签: javascript php mysql ajax database

我正在尝试学习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);

1 个答案:

答案 0 :(得分:2)

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

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

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