Note that there are some explanatory texts on larger screens.

plurals
  1. PO2 AJAX functions on one page - Follow/Unfollow button with PHP - not working
    text
    copied!<p>I want to have a Follow/Unfollow button on a page. </p> <p>The user clicks Follow and this passes the parameter 'artist' to the PHP script, updates a db and the button then says Unfollow.</p> <p>When the user clicks Unfollow, this passes the parameter 'artist' to the same PHP script and this then updates a db via PHP and then button then says Follow.</p> <p>I can get the Follow to change to Unfollow but cannot get Unfollow to change to Follow.</p> <p>My code is as follows:</p> <blockquote> <p><strong>index.php</strong></p> </blockquote> <pre><code>&lt;script type="text/javascript"&gt; 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 &amp;&amp; xmlhttp.status==200) { document.getElementById("artist").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","/follow.php?artist=&lt;?php echo $artist; ?&gt;"+str+"&amp;follow=y",true); xmlhttp.send(); } &lt;/script&gt; &lt;script type="text/javascript"&gt; 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 &amp;&amp; xmlhttp.status==200) { document.getElementById("artist").innerHTML=XMLHTTPlhttp.responseText; } } xmlhttp.open("GET","/follow.php?artist=&lt;?php echo $artist; ?&gt;"+str+"&amp;follow=n",true); xmlhttp.send(); } &lt;/script&gt; &lt;!--Follow button for user to click--&gt; &lt;div class="follow_text" id="artist"&gt; &lt;h1&gt;&lt;a href="javascript:void(0)" onclick="followArtist(this.value)"&gt;Follow artist&lt;/a&gt;&lt;/h1&gt; &lt;/div&gt; </code></pre> <blockquote> <p><strong>follow.php</strong></p> </blockquote> <pre><code>&lt;?php if(isset($_GET['follow'])) { $follow = $_GET['follow']; if($follow=='y') { $artist = $_GET['artist']; #############do a database action ?&gt; &lt;h1&gt;&lt;a href="javascript:void(0)" onclick="unfollowArtist(this.value)"&gt;Unfollow artist&lt;/a&gt;&lt;/h1&gt; &lt;?php } else { $artist = $_GET['artist']; #############do a database action ?&gt; &lt;h1&gt;&lt;a href="javascript:void(0)" onclick="followArtist(this.value)"&gt;Follow artist&lt;/a&gt;&lt;/h1&gt; &lt;?php } } </code></pre> <p>?></p> <p>Any ideas why this is not working?</p> <p>You can see a live version of the script at <a href="http://soundshelter.net/release.php?id=421928" rel="nofollow">http://soundshelter.net/release.php?id=421928</a></p> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload