【发布时间】:2012-01-24 03:22:58
【问题描述】:
我想在页面上有一个关注/取消关注按钮。
用户点击关注,这会将参数“艺术家”传递给 PHP 脚本,更新数据库,然后按钮显示取消关注。
当用户点击取消关注时,这会将参数“艺术家”传递给相同的 PHP 脚本,然后通过 PHP 更新数据库,然后按钮显示关注。
我可以让关注改成取消关注,但不能让取消关注改成关注。
我的代码如下:
index.php
<script type="text/javascript">
function followArtist(str)
{
if (str=="")
{
document.getElementById("artist").innerHTML="";
return;
}
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("artist").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/follow.php?artist=<?php echo $artist; ?>"+str+"&follow=y",true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function unfollowArtist(str)
{
if (str=="")
{
document.getElementById("artist").innerHTML="";
return;
}
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("artist").innerHTML=XMLHTTPlhttp.responseText;
}
}
xmlhttp.open("GET","/follow.php?artist=<?php echo $artist; ?>"+str+"&follow=n",true);
xmlhttp.send();
}
</script>
<!--Follow button for user to click-->
<div class="follow_text" id="artist">
<h1><a href="javascript:void(0)" onclick="followArtist(this.value)">Follow artist</a></h1>
</div>
follow.php
<?php
if(isset($_GET['follow']))
{
$follow = $_GET['follow'];
if($follow=='y')
{
$artist = $_GET['artist'];
#############do a database action
?>
<h1><a href="javascript:void(0)" onclick="unfollowArtist(this.value)">Unfollow artist</a></h1>
<?php
}
else
{
$artist = $_GET['artist'];
#############do a database action
?>
<h1><a href="javascript:void(0)" onclick="followArtist(this.value)">Follow artist</a></h1>
<?php
}
}
?>
任何想法为什么这不起作用?
您可以在http://soundshelter.net/release.php?id=421928查看脚本的实时版本
提前致谢!
【问题讨论】:
-
this.value不适用于a标签..可能需要更正。
标签: php javascript mysql ajax