是否可以从jQuery调用返回多个值?

时间:2011-08-26 07:58:14

标签: php jquery

我有这个代码:messages.php

if (count($row) > 0)
{
    foreach ($row as $r)
    {
        //some code setting variables
        if ($opened_once >= 1)
        {   

        }
        else
        {
            echo "<tr>";
            echo '<td><a class="red_link" href="'.ADDRESS.'view_message.php?id='.$r['id'].'" id= "subject_id" ><span id = "subject">'.$r['subject'].'</span></a></td>';
            echo '<td id = "unique_code1">'.$uniqueCode1.'<span class="pink_text" id = "unique_code2">'.$uniqueCode2.'</span><span id = "unique_code3">'.$uniqueCode3.'</span></td>';
            echo "</tr>";
        }
    }

我需要更新$r['id']$r['subject']$uniqueCode1$uniqueCode2$uniqueCode

我的jQuery代码:

<script>
    $(document).ready(function()
    {
        refresh();
    });

    function refresh()
    {      
        $.post('getMessageDetails.php', function (json) {
        $("#subject").html(json.subject);
        $("#subject_id").html(json.subject_id);
        $("#unique_code1").html(json.unique_code1);
        $("#unique_code2").html(json.unique_code2);
        $("#unique_code3").html(json.unique_code3);
        });   

        window.setTimeout(refresh,30000);
    }   
</script>

然后我有newMessageCnt.php

<?php
<?php
header('Content-Type: application/json; charset=utf-8');
include('header_application.php');

$limit = 15;
if(!isset($_GET['page']))
   $page = 1;
else
   $page = $_GET['page'];

$from = (($page * $limit) - $limit);
$row = $obj_clean->getMessages($_SESSION['user_id'], $from, $limit);

if (count($row) > 0)
{
    foreach ($row as $r)
    {
        $codeLength = strlen($r['unique_code']);
        $codeLength = strlen($r['unique_code']);
        $firstPartLength = $codeLength - 5;
        $uniqueCode3 = substr($r['unique_code'], -2);
        $uniqueCode2 = substr($r['unique_code'], -5, 3);
        $uniqueCode1 = substr($r['unique_code'], 0, $firstPartLength);

        $message_id = $r['id'];
        $subject = $obj_clean->getMessageDetails($message_id);
        $opened_once = $obj_clean->getOpenedOnce($message_id);
        if ($opened_once >= 1)
        {   
            $array['subject'] = $r['subject'];
            $array['subject_id'] = $r['id'];
            $array['unique_code1'] = $uniqueCode1;
            $array['unique_code2'] = $uniqueCode2;
            $array['unique_code3'] = $uniqueCode3;
        }
    }    
}
echo json_encode($array);
exit();

&GT?;     ?&GT;

我把这个.php称为其他地方,我只想要echo $obj_clean->getUnopenedMessagesCount($_SESSION['user_id']);的价值

但是从我的messages.php我想要

echo '<td><a class="blue_link" href="'.ADDRESS.'view_message.php?id='.$r['id'].'">'.$r['subject'].'</a></td>';
echo '<td>'.$uniqueCode1.'<span class="pink_text">'.$uniqueCode2.'</span>'.$uniqueCode3.'</td>';

我不确定我是否正确行事,请一些建议吗?

2 个答案:

答案 0 :(得分:3)

$.post('ajax_call.php', function (json) {
        $("#subject").html(json.subject);
        $("#unique_code").html(json.unique_code);
});   

// ajax_call.php

<?php
$array['subject']='bla-bla';
$array['unique_code']='1231312';

header('Content-Type: application/json; charset=utf-8');
echo json_encode($array);
exit();

答案 1 :(得分:0)

最好是让PHP返回一个包含所需值的对象作为属性。这可以使用JSON发送回浏览器。您的客户端代码将此视为对象,可以提取属性并填充HTML。这会将您的演示文稿(VIEW)与您的数据(MODEL)分开。

编辑:正如@TROODON已回答。