2012-01-24 3 views
0

페이지에서 Follow/Unfollow 버튼을 원합니다.2 페이지의 AJAX 기능 - PHP의 Follow/Unfollow 버튼 - 작동하지 않습니다.

사용자가 팔로우를 클릭하면 매개 변수 '아티스트'가 PHP 스크립트로 전달되고 db가 업데이트되고 버튼에 언 폴로우가 표시됩니다.

사용자가 언 폴로우 (Unfollow)를 클릭하면 매개 변수 '아티스트'가 동일한 PHP 스크립트로 전달되고 PHP를 통해 데이터베이스가 업데이트 된 다음 버튼을 클릭 한 다음 팔로우합니다.

Follow를 Unfollow로 변경할 수 있지만 Unfollow를 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

덕분에 스크립트의 라이브 버전을 볼 수 있습니다

!

+0

'this.value'는'a' 태그에서 작동하지 않습니다. 정정하고 싶을 수도 있습니다. – tradyblix

답변

1

아티스트에게 수행 할 갈까요.

  <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> 
+0

Perfect! 그 친구 줘서 고마워. – Franco

1

두 번째

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

나는 오류가 발생합니다 두 번째 XMLHTTP 객체를 변경 한

+0

고마워, 변경! 불행히도이 작업은 변경되지 않았습니다. 어떤 아이디어? 다시 한 번 감사드립니다 – Franco

+0

XMLHTTPlhttp in unfollowArtist (str) 함수는 정의되지 않았습니다. xmlhttp로 대체되어야합니다 – Tomi

관련 문제