我有一个图像,当点击时更新我的mysql数据库中的一行:
<a href='javascript:setActive(".$jS.")'>
<img src='/update.png'></a>
以下是链接显示的页面:
<?
mysql_connect("xxx", "xxx", "xxx");
mysql_select_db("xxx");
$res = mysql_query("select * from artikel");
$fcount = mysql_fetch_assoc(mysql_query("select count(*) as count from artikel"));
while ($hello = mysql_fetch_assoc($res)) {
$jS = '"'.$hello[id].'"';
echo "<center><a href='javascript:setActive(".$jS.")'>";
echo "<img src='/application/assets/images/featured.png'></a></center>";
echo "<center><span style='color: black;' id='".$hello[id]."'><font face='Century Gothic'>".$hello[status]."</font></span></center>";
}
?>
<script type="text/javascript">
function setActive(ObjID) {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//Return Value. Handle as you wish. Display or ignore.
var x = xmlhttp.responseText;
if (document.getElementById(ObjID).style.color == 'black') document.getElementById(ObjID).style.color='red';
else document.getElementById(ObjID).style.color='black';
document.getElementById(ObjID).innerHTML = '<center><font face="Century Gothic">' + x + '</font></center>';
}
}
var p_str = "a="+ObjID;
xmlhttp.open("POST","setActive.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(p_str);
}
</script>
以下是更新页面:
<?php
$id = $_REQUEST[a];
$res = mysql_query("select status from artikel where id = ".$id);
$row = mysql_fetch_assoc($res);
$active = $row[status];
$activeID = ($active == 0) ? 1 : 0;
$res = mysql_query("update artikel set status = $activeID where id = ".$id);
if ($res) echo $activeID;
else echo "Failure, unable to update ActiveID to $activeID";
?>
一切正常。单击图像时,数据库中的值会更改,如果再次单击,则会更改。
但是,我希望这样做,以及在单击图像时将用户定向到页面。 因此,当我单击图像时,它会转到page.html并更新数据库。
这可能吗?我怎样才能实现它?