2011-05-02 72 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