我确实想用Ajax发送一个HTTP请求并且当用户点击一个接收响应(图片链接从数据库)链接。我放在Ajax函数调用中的参数是通过PHP中的循环获得的点击链接类的值。我认为这是一个有点混乱。这里是我的代码Ajax请求点击链接,它的类值是由PHP得到的没有得到任何回应
在阿贾克斯的函数调用:的index.php
var current = 0;
function showPics(str) {
if (str=="") {
document.getElementById("displayPic").innerHTML="";
return;
}
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("displayPic").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getpics.php?q="+str,true);
xmlhttp.send();
}
getpics.php
$q = $_GET["q"];
$host = "127.0.0.1";
$db = "pcqsp";
$user = "root";
$pass = "";
try {
$conn = new PDO("mysql:host=$host; dbname=$db", $user, $pass);
} catch(Exception $e) {
die('Erreur : ' . $e->getMessage());
}
$req = $conn->prepare('SELECT * FROM screenshot WHERE id_work = :id_work');
$req->execute(array('id_work' => $q));
while ($data = $req->fetch())
{
echo "";
}
$req->closeCursor();
?>
上Ajax调用的index.php
// Open a database connection
$host = "127.0.0.1";
$db = "pcqsp";
$user = "root";
$pass = "";
try {
$conn = new PDO("mysql:host=$host; dbname=$db", $user, $pass);
} catch(Exception $e) {
die('Erreur : ' . $e->getMessage());
}
$req = $conn -> query('SELECT * FROM work');
$i = 1;
while ($data = $req -> fetch()) { ?>
// Here's the problem
$i ++;
}
?>
$current = 0;
$('a[class*="popup"]').click(function(){
current = $(this).attr('class').charAt(0);
showPics(current); //Ajax function call
});
当我没有做通过PHP循环(我是静态做写作:1-popup,2-popup,等一切工作。
请问你有解决这个问题的办法吗?谢谢。
2013-07-18
Copernic