我想在页面上有一个Follow / Unfollow按钮。
用户点击Follow,这会将参数'artist'传递给PHP脚本,更新数据库,按钮然后显示取消关注。
当用户单击取消关注时,会将参数“artist”传递给相同的PHP脚本,然后通过PHP更新数据库,然后按钮然后显示“关注”。
我可以将Follow更改为取消关注但无法取消关注以更改为关注。
我的代码如下:
的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
}
}
&GT;
为什么这不起作用?
您可以在http://soundshelter.net/release.php?id=421928
查看该脚本的实时版本提前致谢!
答案 0 :(得分:1)
你的第二个
<h1><a href="javascript:void(0)" onclick="followArtist(this.value)">Unfollow artist</a></h1>
请跟随艺术家
答案 1 :(得分:1)
我更改了第二个抛出错误的xmlhttp对象。
<script type="text/javascript">
function unfollowArtist(str)
{
if (str=="")
{
document.getElementById("artist").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp2=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp2.onreadystatechange=function()
{
if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
{
document.getElementById("artist").innerHTML=xmlhttp2.responseText;
}
}
xmlhttp2.open("GET","/follow.php?artist=<?php echo $artist; ?>"+str+"&follow=n",true);
xmlhttp2.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>