2011-08-26 77 views
1

我有这样的代码:messages.php是否有可能从jQuery调用中返回多个值?

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(); 

?> ?>

我把这个.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>'; 

我米不知道如果我这样做是正确的,请给我一些建议?

回答

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(); 
0

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

编辑:准确的讲,@TROODON已经回答了。

+0

最后一件事:JSON返回的主题和独特的代码,但在我的HTML标签,我有3个部分(有一个标签ID)的UNIQUE_CODE也是对象是具有消息ID,并与一个标签ID的链接??。我将如何处理这件事?这对我来说都是非常新奇的。感谢您迄今为止的帮助:) –

+0

只需从PHP中返回值,使其在JavaScript中的使用尽可能简单。如果您需要3部分数据,然后将其归为3部分。使用JavaScript将返回的值嵌入到浏览器DOM中。 – trojanfoe

+0

我必须为每个零件都有标签ID吗?对不起,我不跟随:( –

相关问题