【问题标题】:2 AJAX functions on one page - Follow/Unfollow button with PHP - not working一页上有 2 个 AJAX 函数 - 使用 PHP 的关注/取消关注按钮 - 不起作用
【发布时间】: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


【解决方案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>

【讨论】:

  • 完美!谢谢这位朋友
【解决方案2】:

你的第二个

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

应该是关注艺术家

【讨论】:

  • 感谢您,已更改!不幸的是,它并没有改变任何工作方式。有任何想法吗?再次感谢
  • unfollowArtist(str) 函数中的 XMLHTTPlhttp 未定义,应将其替换为 xmlhttp
猜你喜欢
  • 1970-01-01
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
相关资源
最近更新 更多