user712027
user712027

Reputation: 572

Updating a mysql table when clicking a link

I need to update a MySQL table when the user clicks a link.

Here's the link:

<a href='moreinfo.html' onclick="moreInfo();">More info</a>

If the user clicks it, I need to update table user with a 'yes' in field MoreInfo.

And here's the function:

function moreInfo(){

   info = 'yes';
   username ='[email protected]'; // coming from the session

    $.ajax({
        type: "POST",
        url: "more.php",
            data: "info="+info+"&username="+username+"",


        success: function(msg){ 
                alert(msg);

        }
    });
}

more.php

<?php 
//not updating the database yet, just echoing for testing purposes

    $username = $_POST['username'];
    $info= $_POST['info'];
    echo $username;
    echo "<br>".$info;
?>

What I get in the alert of the function is: <br>

Is my aproach correct? What am I doing wrong?

Thanks a ton!

Upvotes: 0

Views: 1440

Answers (1)

jon_darkstar
jon_darkstar

Reputation: 16768

The database access needs to occur in PHP. Soo...

  1. So write a PHP file that will take a parameter for the current user and update the DB as necessary. Have it echo something. Can be as small as success/failure flag, but the ajax that calls should get some response data.

  2. In javascript write some ajax to call upon this PHP file to get teh database access done. The javascript should probably also report some type of confirmation on the page.

These are the most common options for accessing MySQL in PHP. The PHP standard manual is fantastic. If you aren't using it yet I highly recommend!

http://php.net/manual/en/book.pdo.php
http://php.net/manual/en/book.mysql.php
http://php.net/manual/en/book.mysqli.php

Upvotes: 1

Related Questions