AJAX to update database via function

2019-09-21 06:03发布

问题:

I am trying to update some data in my database using AJAX. I have already made the php script and tested that it is indeed working. I need to pass two variables to the php script which updates the database, so I made a javascript function that calls the php script with ajax and passes the variables.

I want to run the function when a user click on a upvote image i made. The data in the database should then be updated and the upvote button and a number that shows the current number of upvotes need to update.

This is my javascript:

 <script type="text/javascript">
    function upvote(memeid, userid)
    {
        $.ajax({
            url : 'scripts/upvote.php', // give complete url here
            type : 'post',
            data : 'id='+memeid+'&bruger_id='+userid,
            success : function(msg){
                alert('success');
            }
        });
    });
    </script>

This is the php code where the function is called from.

echo '<img id="upvote" onClick="upvote('.$feed[$loaded]["memid"].','.$feed[$loaded]["userid"].')" src="images/neutral_up.png" width="20px">';

My upvote.php file:

<?php
    //Script til at upvote memet

    include 'db_connect.php';//Connect til databasen

    $id = $_POST['id'];//Memets id som fanges fra linket
    $bruger_id = $_POST['bruger_id'];//Brugerens id som fanges fra linket
    $cookiename = "yems".$id;//Sætter navn på cookie ud fra memets id

    if($_COOKIE[$cookiename] == 2)//Hvis brugeren allerede har downvoted memet
    {
        mysql_query("UPDATE memes SET ned=ned-1 WHERE id='".$id."'");//Fjern en downvote fra memets
        mysql_query("UPDATE memes SET up=up+1 WHERE id='".$id."'");//Tilføj en upvote til memet
        mysql_query("UPDATE brugere SET up=up+1 WHERE id='".$bruger_id."'");//Giv brugeren som har lavet memet en upvote
        mysql_query("UPDATE brugere SET ned=ned-1 WHERE id='".$bruger_id."'");//Fjern en downvote fra brugeren som har uploadet memet
        $expire=time()+60*60*24*365*10;//Sæt en expire tid på 10 år til cookien 
        setcookie($cookiename, "1", $expire, '/');//Sæt cookie som siger at den nuværende bruger har upvoted memet.
    }else if($_COOKIE[$cookiename] != 1){//Hvis den nuværende bruger ikke allerede har upvoted eller downvoted memet.
        mysql_query("UPDATE memes SET up=up+1 WHERE id='".$id."'");//Giv memet en upvote
        mysql_query("UPDATE brugere SET up=up+1 WHERE id='".$bruger_id."'");//Giv brugeren som har lavet memet en upvote.
        $expire=time()+60*60*24*365*10;//Sæt en expire tid på 10 år til cookien 
        setcookie($cookiename, "1", $expire, '/');//Sæt cookie som siger at den nuværende bruger har upvoted memet.
    }

    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../index.php">'; 
?>

回答1:

<script type="text/javascrip">
    function upvote(memeid, userid)
    {
        $.ajax({
            url : 'scripts/upvote.php', // give complete url here
            type : 'post',
            data : 'id='+memeid+'&userid='+userid,
            success : function(msg){
                alert('success');
            }
        });
    });
</script>

You didnt passed the second argument for upvote function.

echo '<img id="upvote" onclick="upvote('.$feed[$loaded]["memid"].','.$feed[$loaded]["userid"].')" src="images/neutral_up.png" width="20px">';


回答2:

You should read jQuery's documentation : http://api.jquery.com/jQuery.ajax/
Your line "var myData = ..." is incorrect, replace it by data : { (key : value object) } (for further details, refer to the documentation)



回答3:

syntax error

$.ajax({
   url : 'scripts/upvote.php?id='+memeid+'&'+userid, // give complete url here
   type : 'post',
   data:{'id' : 'id...'},
   success : function(data){
      alert('success');
   }
});


回答4:

<script type="text/javascrip">
        function upvote(memeid, userid)
        {
            $.ajax({
            url : 'scripts/upvote.php
            type : 'post',
            data = {member_id:memeid, user_id:userid},
            success : function(data){
                alert('success');
            }
        });
    });
</script>

In php You can get the datas using $_POST['member_id'] & $_POST['user_id']