2013-04-15 19 views
0

我有一個腳本,可以從數據庫中的相關類別加載頁面時從數據庫加載樣本圖像。從數據庫結果創建JSON對象使用AJAX post方法回發到jQuery UI對話框

<?php 
$category = 'granite'; 
$samples = 'SELECT * FROM material WHERE material_type = :cat'; 
$res = $db->prepare($samples); 
$res->execute(array(':cat' => $category)); 
$count = $res->rowCount(); 
if($count > 0) 
while ($row = $res -> fetch()){ 
    $postimggranite = $row[mat_image]; 
    $postidgranite = $row[id]; 
?> 
<?php 
    echo "<div class='sample'>"; 
    echo "<h3 class='sample_h'>$row[name]</h3>"; 
    echo "<a href='/images/granite-worktops/samples/$postimggranite'><img src='/images/granite-worktops/thumbs/$postimggranite' alt='$row[name]' width='100' height='100' class='aligncenter size-full' title='$row[name]'></a>"; 
    echo "<br /><a class=\"button\" href=\" \" rel=\"nofollow\" onClick=\"user_notice(this,''); return false;\">More Details</a>"; 
    echo "</div>"; 
} 
?> 

你可以從腳本中的最後一個回聲見上關閉一個div我有一個「更多詳細信息按鈕之前,通過點擊出現在我需要顯示的附加信息在屏幕上的對話框同樣DB。方法我打算用的是

function user_dialog(a,b){ 
    "undefined"!=typeof jQuery.ui?($("#dialog").attr("title","Detailed Information").html(a), 
    $("#dialog").dialog({ 
     modal:true, 
     width:400, 
     buttons: { 
      Cancel: function() { 
       $(this).dialog("close"); 
       }, 
       Download: function(){ 
        $(this).dialog("close"); 
        window.location=b 
        } 
        } 
        } 
        )) 
        :window.location=b} 

function user_notice(a){ 
    download_link=$(a).attr("href"); 
    $.ajax({ 
     type:"POST", 
     url:"/includes/json.php", 
     data:"action=reminder&thepath="+download_link, 
     dataType:"json", 
     error:function(){ 
      window.location=download_link 
      }, 
     success:function(a){ 
      1==a.status&&user_dialog(a.html,download_link); 
      } 
      }) 
      }; 

這裏也是我的json.php文件

<?php 
header('X-Robots-Tag: noindex, noarchive'); 
$a = session_id(); 
if(empty($a)) session_start(); 
if($_SERVER['HTTP_REFERER']){ 

    $resp_dialog = array(
    'status' => 1, 
    'html' => '<p>Here is you sample and rest of staff</p>' 
); 
    echo json_encode($resp_dialog); 
    }else{ 
     header('HTTP/1.1 403 Forbidden'); 
     exit; 
     } 
?> 

清楚的腳本所有的工作和行<p>Here is you sample and rest of staff</p>出現在對話框中,但我真正需要的是創建一個json對象,該對象帶來了來自數據庫的其他信息,並且不知道在json.php文件的哪個位置,或者創建某種附加文件並將其放置在與urlid="something to get fromdb"$_GET['urlid']json.php發送它做DB。基本上不知道在什麼地方以及在哪裏做。

對我來說請放輕鬆,因爲我仍然在學習這一切,而且大部分對我來說還是很新的,所以請原諒我,如果我沒有正確解釋。

回答

1

您需要在$通過阿賈克斯要詳細類別的ID。

$.ajax({ 
    type:"POST", 
    url:"/includes/json.php", 
    data:['cat_id': cat_id], // passing var cat_id from the link... 

然後你json.php文件應該是這樣的:

<?php 
header('X-Robots-Tag: noindex, noarchive'); 
$a = session_id(); 
if(empty($a)) session_start(); 
if($_SERVER['HTTP_REFERER']){ 

    $cat_id = $_POST['cat_id']; // Grab the Id of the category you want to detail... 

    // Here you have to fetch the info from the DB... 
    $samples = '... WHERE cat_id = :cat'; // Create the sql here! 
    $res = $db->prepare($samples); 
    $res->execute(array(':cat' => $cat_id)); 
    $count = $res->rowCount(); 
    $htmlToDisplay = ''; 
    if($count > 0) 
    while ($row = $res -> fetch()){ 
    $htmlToDisplay += $row['...']; // format yout output... 
    } 

    $resp_dialog = array(
    'status' => 1, 
    'html' => $htmlToDisplay, 
); 
    echo json_encode($resp_dialog); 
    }else{ 
     header('HTTP/1.1 403 Forbidden'); 
     exit; 
     } 
?> 

編輯:

要創建 「更多」 鏈接,嘗試這樣做:

echo "<div class='sample'>"; 
/* ... */ 
// Passing $row[id] to user_notice() function! 
echo "<br /><a class=\"button\" onClick=\"user_notice(this,". $row[id] ."); return false;\">More Details</a>"; 
echo "</div>"; 

然後,將user_notice()函數更改爲:

function user_notice(a,cat_id){ // New parameter cat_id! 
    download_link=$(a).attr("href"); 
    $.ajax({ 
     type:"POST", 
     url:"/includes/json.php", 
     data:{ cat_id: cat_id }, // Passing cat_id to json.php... don't forget to pass your other variables 
     /* ... */ 

看看如何通過阿賈克斯$ here

+0

所以,基本上我必須把數據庫查詢腳本放在我的json.php,現在我明白了。讓我試試看,如果出現問題,我可能不得不回來。感謝您的建議 – AlexB

+0

我試圖讓它工作,但看起來我無法正確構建更多信息按鈕的href。 – AlexB

+0

我編輯了我的答案,添加了如何將id傳遞給json.php文件。然後,在該文件中,獲取id並創建你的sql。 – vinigarcia87

0

在$阿賈克斯()函數

data:"action=reminder&thepath="+download_link, 

改變你將數據傳遞到

data: "{'action':'reminder','thepath':'"+download_link+"'}" 
+0

OK,都做到了變數,但不知道這是否會有助於真正解決我的問題 – AlexB

+0

你能告訴我你的錯誤? – ravisolanki07

+0

此刻我沒有錯誤,但我沒有一個腳本,將發送另一個查詢數據庫填充json.php,應該輸出結果時,顯示對話框 – AlexB