在这里,我正在使用AJAX和PHP检索数据库的值。我将类似photos / image1.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'/>
问题是从图像/images1.jpg和图像/images2.jpg检索的数据没有回显,因为其中包含正斜杠。
如何打印该数据?
更改:
$ans= "<img src=". $res['image']."\>";
至:
$ans= '<img src="'. $res['image'] .'"/>';
这应该工作。
希望能帮助到你!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句