2011-05-02 71 views
1

我有一個ajax腳本,我有點了解,但仍需要一些額外的幫助。需要幫助學習ajax,從mysql抓取數據

$('.images').click(function(){ 
    var imageId = $(this).attr('id'); 
    alert(imageName); 
    $.ajax({ 
      type: "get", 
      url: "imageData.php", 
      dataType: "json", 
      data: {getImageId: imageId}, 
      error: function() { 
       alert("error"); 
      }, 
      success: function(data){ 
       alert(imageId); 
       $("#images_"+imageId).html(data); 
      } 
     }); 
    //$('#images_'+imageId).toggle(); 

}); 

我有一個代碼,它進入這個imageData.php文件

<?php 
if(isset($_GET)){ 
    $images = ""; 
    $path = 'img/'; 
    $imageId = $_GET['getImageId']; 
    $sql = mysql_query("SELECT * FROM images WHERE iID = '".$imageId."'"); 
    while($row = mysql_fetch_array($sql)){ 
     $images .= $path.$row['images']; 
} 
    $json = json_encode($images); 
?> 
<img src='<?php echo $json;?>'/> 

    <?php 
} 
    ?> 

爲什麼,當我嘗試從$圖像呼應的字符串,但它正確輸出,當我做輸出誤差echo $imageId;?我試圖從MySQL輸出的東西,但沒有試圖輸出只是ID。

需要幫助,請,謝謝

+0

當jQuery ajax返回值的時候,你會得到這個錯誤嗎?做一個'echo json_encode($ images);' – arma 2011-05-02 22:22:01

+0

它不返回一個值,我只是得到錯誤。我試過做json_encode($ images);我不再有錯誤,但是,我也沒有得到一個成功的警報。 – hellomello 2011-05-02 23:03:46

+0

我不確定是否可以提醒json嘗試使用console.log。螢火蟲控制檯響應是否爲空? – arma 2011-05-02 23:07:25

回答

1

這裏你不需要使用json_encode,沒有數據需要是JSON格式。如果查詢只返回一個圖像,那麼也沒有理由循環結果集。

試試這個:

<?php 
if(isset($_GET['getImageId'])) { 
    $path = ''; 
    $imageId = mysql_real_escape_string($_GET['getImageId']); // SQL injection! 
    $result = mysql_query("SELECT images FROM images WHERE iID = '".$imageId."'"); 
    $row = mysql_fetch_array($result); 
    if($row) { 
     $path = 'img/' . $row['images']; 
    } 
} 
?> 
<?php if($path): ?> 
    <img src='<?php echo $path;?>'/> 
<?php endif; ?> 

如果iID實際上是一個整數,你需要省略單引號中查詢。

您還可以到dataTypejson改變html,爲你是返回一個圖像標記(HTML),而不是JSON

$.ajax({ 
    type: "get", 
    url: "imageData.php", 
    dataType: "html", 
    data: {getImageId: imageId}, 
    error: function() { 
     alert("error"); 
    }, 
    success: function(data){ 
     $("#images_"+imageId).html(data); 
    } 
}); 

另一種選擇是隻返回文本(鏈接)並在客戶端創建圖像:

<?php 
if(isset($_GET['getImageId'])) { 
    $path = ''; 
    $imageId = mysql_real_escape_string($_GET['getImageId']); // SQL injection! 
    $result = mysql_query("SELECT images FROM images WHERE iID = '".$imageId."'"); 
    $row = mysql_fetch_array($result); 
    if($row) { 
     echo 'img/' . $row['images']; 
    } 
} 
?> 

而在JavaScript中:

$.ajax({ 
    type: "get", 
    url: "imageData.php", 
    dataType: "text", 
    data: {getImageId: imageId}, 
    error: function() { 
     alert("error"); 
    }, 
    success: function(data){ 
     $("#images_"+imageId).html('<img src="' + data + '" />'); 
    } 
}); 
+0

thank you, changing '$("#images_"+imageId).html('');'幫助。此外,我試圖添加切換,我做了這個'$(「#images _」+ imageId).toggle()。html('');'但它會在我第一次雙擊它時切換,而不是單擊它。任何想法爲什麼這樣做?謝謝 – hellomello 2011-05-02 23:45:04

+0

也,如果你能幫助我。如果我回聲超過一件事,說我想echo'echo $行['userid']'我怎麼分開這兩個「圖像和userid」從數據在Ajax? – hellomello 2011-05-04 03:16:44

1

正如你可能會得到許多圖像,因爲您使用while循環,你可能要做到這一點,像這樣:

在PHP中:

$x = 0; 
$another = array(); 
while($row = mysql_fetch_array($sql)){ 
    $another[$x] = $path.$row['images']; 
    $x++; 
} 

echo json_encode($another); 

,並在jquery(在你的成功回調):

$.each(data, function(i, v){ 
    // Do the image inserting to the DOM here v is the path to image 
    $('#somelement').append('<img src="'+v+'"'); 
}); 
+0

when you say many images do you mean because append() keeps adding multiple same objects? – hellomello 2011-05-02 23:41:20

+0

Well as you did while loop in php i assumed you retrieve multiple images sometimes. And append() will keep adding images in loop till it has added all of them. – arma 2011-05-02 23:42:44

+0

thank you for your help your answers cleared up a lot – hellomello 2011-05-02 23:47:21