开发者

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 ='user@user.com'; // coming from the session

    $.ajax({
      开发者_C百科  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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜