#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    [PHP] Insert query when button is clicked - Stay on same page


    I am trying to develop something like the "like" button on facebook, as it will function the same way. I want the user to be able to click a button, which somehow runs a insert on mysql DB to update the value. I need this, BUT I need it to stay on the same page?

    Pretty much, I want to create my own like button for my website. I understand that PHP is server-side, and I cant really use a onclick function.

    Does anyone know how I would go about executing this with PHP (or ajax or whatever else I would need)
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0
    I've been doing further research and it seems like AJAX is how it needs to be done. I've never worked with AJAX, does anyone have or know of an example of something close to this functionality?

    I've tried something like this:

    PHP Code:
    <!--
    To change this templatechoose Tools Templates
    and open the template in the editor.
    -->
    <!
    DOCTYPE html>
    <
    html>
        <
    head>
            <
    meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <
    title></title>
        </
    head>
        <
    body>
            <
    script type="text/javascript" src="jquery.min.js"></script>
            <script type="text/javascript">
                function doSomething() {
                    $.get("DB.php");
                    return false;
                }
            </script>
            <p><input type= "submit" value="Click Me" onclick="doSomething();"</p>
        </body>
    </html> 

    and called a insert PHP file here to do the insert

    PHP Code:
    <?php

    mysql_connect
    ("$hostname""$username""$password");
    mysql_select_db($database);

    $sql "INSERT INTO 'table' (content) VALUES 'This is added'; ";

    mysql_query($sql)
    ?>
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Are you planning to put the like button on different domains, or will it only be used on one domain?
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0
    It will be on many domains throughout the site. I plan to output data into a unordered list, and each list item will have a like/ dislike button.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    A "domain" and a "site" are generally considered to be the same thing. Explain exactly what you're talking about.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0
    Sorry for the vague response.

    I'll try to explain what I want to do with the like button.

    On many of the pages, I will be outputting data from a DB into un ordered list. Every element in this list will have its own like button. Every element in that list will also have its own individual page, where the user can see more information about it. There will be a like button on this page also. So I will have multiple pages that will be using this button.
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    ok, you're staying on the same domain then: one website.

    On various pages of this website will be buttons which increment a database counter.

    Make those buttons be an ajax call to www.yourdomain.com/updateCounter.php?id=123

    On updateCounter.php, update the counter for $_GET['id'].

    Print out the appropriate ID in your lists and on your landing pages.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo