在这里,我正在使用AJAX和PHP检索数据库的值。我将照片/图像1.jpg等图像的路径存储在数据库中。从数据库中检索该数据并将其发回时,该数据不会打印回来,因为它中有反斜杠或前斜杠。那么,在使用 AJAX 时,将数据发送回包含特殊字符的主页的过程是什么?
我的 JavaScript 代码:
function getdata(eve)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("grayscreen").innerHTML=xmlhttp.responseText;
} }
xmlhttp.open("GET","picdetails.php?q="+eve,true);
xmlhttp.send();
我的 PHP 代码
<?php
include 'config.php';
$query2="select * from Sampletable where sublabel= '".$_GET['q']."'";
$query=mysql_query($query2);
if(mysql_num_rows($query)>0)
{
$count=0;
while($res= mysql_fetch_array($query))
{
if($count==0)
{
$ans= "<img src=". $res['image']."'>";
echo $ans;
}
else {
echo "<img src=".$res['image']." style='display:none'/>";
}
$count=$count+1;
}
}
?>
这里$res['image']是图像的路径,类似于images/image1.jpg它将从数据库中重新派生。
屏幕中的输出是
"<img src="''">"<img src="style='display:none'/">
但输出应该是
<img src="images/images1.jpg"/> <img src="images/images2.jpg" style='display:none'/>
问题是从中检索的数据是图像/图像 1.jpg 和图像/图像 2.jpg它没有被回显,因为它有前斜杠。
如何打印这些数据?
更改:
$ans= "<img src=". $res['image']."'>";
自:
$ans= '<img src="'. $res['image'] .'"/>';
这应该有效。
希望对您有所帮助!