Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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.
    Last edited by AndrewGSW; 12-15-2012 at 07:53 PM.
    "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

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •