Demon Demon - 5 months ago 12
PHP Question

Ajax add record with session id and user id issue

I have an add friend system and i want to make an ajax call but it is not working, i want ajax to insert in the table the session id (me) and friend id,but i have no luck, i am a begginer.

JS CODE:

$('#add').click(function(){
var u = "<?php echo $_SESSION["user"];?>";
var f = "<?php echo $_GET["u"];?>";
$.ajax({
type: "POST",

url: "?u="+u+"&add="+f,
success: function(){
alert("success");
}
});
});


<a id='add' class='btn btn-success' href=\"#\">Add as friend</a>


Any help apreciated! Thanks.

Answer

since you are sending a "post" request this will be never handled you had to send it with a "GET" method see this it may help

('#add').click(function(){
   var u = `"<?php echo $u->id?>"` <--- this is friend id
 var id = "<?php echo  $_SESSION['user']";?> <---- this is my id


     $.ajax({
          type: "GET",
          url : "?u="+u+"&add="+id,
          success: function(data){//here the function should have a parameter which is the response of your request
             alert(data);//try to debug it and see what is the response or alert("success")
          }

in you php file you can get those parameters simply by using $_GET["u"] and $_GET["add"] to use them in your request for inserting