2011-08-26 73 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嗎?對不起,我不跟隨:( –

相關問題