繁体   English   中英

一页上有2个AJAX函数 - 使用PHP关注/取消关注按钮 - 无法正常工作

[英]2 AJAX functions on one page - Follow/Unfollow button with PHP - not working

我想在页面上有一个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
            }

    }

?>

任何想法为什么这不起作用?

您可以在http://soundshelter.net/release.php?id=421928上查看该脚本的实时版本

提前致谢!

你的第二个

<h1><a href="javascript:void(0)" onclick="followArtist(this.value)">Unfollow artist</a></h1>

请跟随艺术家

我已经更改了第二个抛出错误的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM