AJAX将PHP数据返回到页面

时间:2014-03-13 01:58:34

标签: php ajax

修改

我根据一些研究改变了我的方法,我得到了部分结果。

数据库:

+--------------------------------------------------+
|  id  |  date      |  objet  |  contenu     | lu  |
+--------------------------------------------------+
|  1   | 2013-01-20 | msg1    | msg1_content | 0   |
|  2   | 2013-01-20 | msg2    | msg2_content | 0   |
|  3   | 2013-01-20 | msg3    | msg3_content | 0   |
+--------------------------------------------------+

链接:

<a href="javascript:void(0);" onclick="markAsRead('.$message['id'].');">View'</a>

JS / AJAX:

function markAsRead(id)
{
    $.ajax(
    {
        type: "GET",
        url: 'php/messagerie.php',
        data: "id=" + id, // appears as $_GET['id'] @ ur backend side
        success: function(data)
        {
            // data is ur summary
            $('#contenu').html(data);
        }
    });
}

PHP(php / messagerie.php)

$q = intval($_GET['id']);

// connect to db (of course not shown here)
if (!$con)
{
    die('Could not connect: ' . mysqli_error($con));
}

$sql="SELECT * FROM coq_messagerie WHERE id = '".$q."'";

$lu = mysqli_query($mysqli, "UPDATE ".DB_PREFIX."messagerie SET lu='1' WHERE id='$q' LIMIT 1");
$result = mysqli_query($con,$sql);

    echo "<table border='1' width='100%'>
        <tr>
            <th>id</th>
            <th>date</th>
            <th>objet</th>
            <th>contenu</th>
            <th>lu</th>
        </tr>";

        while($row = mysqli_fetch_array($result))
        {
            echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['date'] . "</td>";
                echo "<td>" . $row['objet'] . "</td>";
                echo "<td>" . $row['contenu'] . "</td>";
                echo "<td>" . $row['lu'] . "</td>";
            echo "</tr>";
        }

    echo "</table>";

mysqli_close($con);

我目前只检索<th>的信息,而不是在查找id 2或3时的信息,但我正在获取id的信息。这可能只是部分工作吗?


简介

为了在我的页面上显示动态信息,我正在混合使用一些代码,这些代码应从基本HTML链接中收集数据,该链接解析并返回当前页面上的数据。当然,这看起来非常基本,但作为我的第一个AJAX脚本,我非常感谢一些帮助。

代码

HTML链接:

$output .= '<a onclick="markAsRead('.$message['id'].'); return false;">'.View.'</a>';

注意:该链接会按预期收集id字段。

JS / AJAX代码:

function markAsRead(str) {
    console.log("starting");
    if (str=="")
    {
        console.log("string empty");
        document.getElementById("contenu").innerHTML="";
        return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        console.log("on ready state change");
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("contenu").innerHTML=xmlhttp.responseText;
        }
        else
        {
            console.log("seems to returning information");
            var abc = "<h1>Huh.</h1>";
            document.getElementById("contenu").innerHTML=abc;
        }
    }
    xmlhttp.open("GET","php/messagerie.php?q="+str);
    xmlhttp.send();
}

注意:console.log('###_'+str)用于查看代码崩溃的位置。全部记录正确(105,107,109)。

PHP( php / read.php

// connect to db (of course not shown here)
$message = $GET["messageid"];

$lu = mysqli_query($mysqli, "UPDATE messages SET lu='1' WHERE id='$message' LIMIT 1");
if(!$lu)
{
    echo "Update error"; 
    exit;
}
else
{    
    $data = mysqli_query($mysqli, "SELECT * FROM messages WHERE id='$message' LIMIT 1");
    while($row = mysqli_fetch_array($data))
    {
        echo $row["contenu"];
    }
}

注意:$lu已更新,这让我相信问题出在while/echo区域或我的AJAX回复中。

当前控制台日志

starting
on ready state change
seems to returning information
-> GET ***/php/messagerie.php?q=3
   ** params -> q 3 **
   ** empty response **
   ** empty HTML **
on ready state change
seems to returning information
on ready state change

我哪里错了?

2 个答案:

答案 0 :(得分:1)

$message = $GET["messageid"];

应该是

$message = $_GET["messageid"];

答案 1 :(得分:0)

找到解决方案!这会更新字段值以将消息标记为已读并在正确的div中显示消息。

解决方案

在AJAX请求中, true 的缺失参数已随查询一起添加。

完整代码

AJAX

function markAsRead(str)
{    
    if (str=="")
    {
        document.getElementById("contenu").innerHTML="";
        return;
    }
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("contenu").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","php/messagerie.php?q="+str,true);  // added ",true"
    xmlhttp.send();
}

PHP

$q = intval($_GET['q']);

$con = mysqli_connect('','','',''); // values hidden here for obvious reasons
mysql_set_charset('utf8',$con);

if (!$con)
{
    die('Could not connect: ' . mysqli_error($con));
}

$sql="SELECT * FROM messages WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

$update = mysqli_query($con, "UPDATE messages SET lu='1' WHERE id='$q'");

if(mysqli_num_rows($result) > 0)
{
    echo "<table border='1'>
            <tr>
                <th>id</th>
                <th>date</th>
                <th>objet</th>
                <th>contenu</th>
                <th>lu</th>
            </tr>";

    while($row = mysqli_fetch_array($result))
    {
        echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['date'] . "</td>";
            echo "<td>" . $row['objet'] . "</td>";
            echo "<td>" . $row['contenu'] . "</td>";
            echo "<td>" . $row['lu'] . "</td>";
        echo "</tr>";
    }

    echo "</table>";
}

mysqli_close($con);