Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-15-2012, 07:34 PM   PM User | #1
arava
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
arava is an unknown quantity at this point
subscribe/unsubscribe to/from article using php/ajax

I'm using a news script. Users can login and submit articles. Now I like to give subscribe/unsubscribe feature per articles to the logged in users so they'll receive emails if there is any changes to the articles. I've created a table with 2 fields. userID, articleID. now i want to have subscribe/unsubscribe button like submit to facebook/twitter buttons. how can I do this without page refresh. If the used subscribed to the news, unsubscribe button should show there.

How can I do this?
arava is offline   Reply With Quote
Old 12-15-2012, 07:51 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Well, in outline, if you had a checkbox next to the article
Code:
<input type="checkbox" name="subscribe">
you could use its onclick event:
Code:
if (this.checked) {
    // send ajax request with somepage.php?art=idnumber&subs=1
    // and possibly include the userid
} else {
    // send ajax request with somepage.php?art=idnumber&subs=0
}
then somepage.php could:
PHP Code:
<?php
if (isset($_GET['subs']) && !empty($_GET['subs'])) {
    
$subs = (int) $_GET['subs'];
    if (
$subs == 1) {
        
// check and sanitize the userID and articleID
        // execute SQL to insert userID and articleID
    
} else {
        
// check and sanitize the userID and articleID
        // execute SQL to delete userID and articleID
    
}
}
?>
Of course, this assumes that you are using PHP.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 12-15-2012 at 07:53 PM..
AndrewGSW is offline   Reply With Quote
Old 12-19-2012, 06:18 PM   PM User | #3
arava
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
arava is an unknown quantity at this point
Thanks.

I used Button and its working fine. I disable the button and 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?
arava is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:51 PM.


Advertisement
Log in to turn off these ads.