Jump to content

Subscribe/unsubscribe To/from Article Using Php/ajax


dave1234

Recommended Posts

I am using button to subscribe/unsubscribe from articles. When an user click the the button I change the text to subscribed/unsubscribed. The problem is that after the user refresh the page, the button enabled and show the text Subscribe/Unsubscribe accordingly. when the user click the button no action takes place and change the text with text from previous action.

 

ex.

user login.

click Subscribe button from the article page.

- database action done

- button text change to Subscribed, disabled

User refresh the page.

click Unsubscribe button

- no db action takes place

- button text change to Subscribed, disabled

 

If the browser closed and reopen then the user can unsubscribe. To subscribe again, close and reopen the browser.

 

how to fix this?

Link to comment
Share on other sites

I'm using the following code in three places.

 

<? if(user_check_status()) {
$que = "select * from ar_usersub where userID=".$_SESSION['UserPkID']." and articleID=".$myArt['articleID'];
$result =mysql_query($que);
if(mysql_num_rows($result)){
$val = "Unsubscribe";
}
else
{
$val ="Subscribe";
}
?>
<div width="10%">
<div id="subscribeDiv"><input type="submit" name="subscribe" value="<?echo $val;?>" onclick="checkStatus()"/></div></div> <? } ?>

javascript/Ajax

<script language="Javascript" type="text/Javascript">

function getXMLHTTP() { //fuction to return the xml http object
var xmlhttp=false;
try{
 xmlhttp=new XMLHttpRequest();
}
catch(e) {
 try{	
 xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
 }
 catch(e){
 try{
 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 }
 catch(e1){
	 xmlhttp=false;
 }
 }
}
return xmlhttp;
}

function checkStatus() {
var strURL="/subscribe.php?eId="+<?echo $myArt['articleID'];?>+"&uId="+<?echo $_SESSION['UserPkID'];?>;
var req = getXMLHTTP();
if (req) {	
 req.onreadystatechange = function() {
 if (req.readyState == 4) {
	 // only if "OK"
	 if (req.status == 200) {		
	 document.getElementById('subscribeDiv').innerHTML=req.responseText;		
	 } else {
	 alert("There was a problem while using XMLHTTP:\n" + req.statusText);
	 }
 }	
 }	
 req.open("GET", strURL, true);
 req.send(null);
}
}
</script>

 

subscribe.php

<?
$val=($_GET['eId'] + $_GET['uId']);
$us = $_GET['uId'];
$ev = $_GET['eId'];
$link = mysql_connect('localhost', 'xxxxx', 'xxxx');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('xxxx');

$que = "select * from ar_usersub where userID=".$us." and articleID=".$ev;

$result =mysql_query($que);
$que2="";
if(mysql_num_rows($result)){
$que2 = "delete from ar_usersub where userID=".$_GET['uId']." and articleID=".$_GET['eId'];
$val = "Unubscribed";
}
else
{
$que2 = "insert into ar_usersub values(".$us.", ".$ev.")";
$val ="Subscribed";
}
mysql_query($que2);

?>
<div id="subscribeDiv2"><input id=<?echo $val;?> type="submit" name=<?echo $val;?> value=<?echo $val;?> disabled="disabled" /></div>

 

Front End actions:

user login.

click Subscribe button from the article page.

- database action done

- button text changed to Subscribed & the button disabled

User refresh the page.

click Unsubscribe button (since the user already subscribed the Unsubscribe button displayed)

- no db action takes place

- button text change to Subscribed & the button disabled

 

If the browser closed and reopen then the user can unsubscribe. To subscribe again, close and reopen the browser.

 

how to fix this?

Edited by dave1234
Link to comment
Share on other sites

user login.

click Subscribe button from the article page.

- database action done - correct

- button text changed to Subscribed - correct & the button disabled - correct

User refresh the page.

click Unsubscribe button (since the user already subscribed the Unsubscribe button displayed)

- no db action takes place - wrong

- button text change to Subscribed - wrong & the button disabled - correct

 

I want to fix the wrong part

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.