1

HTML :

<a href="/profile.php?user={$username}&action_request="friend_request">
Friend request
</a>

PHP :

if(isset($_GET['user']) && isset($_GET['action_request'])) {
     if(strcmp($_GET['action_request'],"friend_request") == 0) {
        insert_friendship($id_user1,$id_user2);
   }
}

and the function for insert is :

  //sql string
   if(sql_insert()) {
      return "Friendship request sent!";
      } else {
       return "Friendship request failed!"; }

P.S I am using smarty engine for templating.

How do i take the url from <a> tag with ajax and send it to the php page to call the function then ajax to wait for the respond and send it to the html.

2 Answers 2

2

include jquery in your page.

<script type="text/javascript">
    document.getElementById("ajax_a").addEventListner("click", function(e){
        e.preventDefault();
        var uri = document.getElementById("ajax_a").getAttribute("href");
        $.ajax({
            url: uri,
            type: "GET",
            data: {},
            dataType: "html",
            success: function(data){
                // data is the value returned by server.
            },
            error : function(){
                alert("some error occured");
                // data is the value returned by server.
            }
        });
    });
</script>

<a id="ajax_a" href="/profile.php?user={$username}&action_request="friend_request">
Friend request
</a>
Sign up to request clarification or add additional context in comments.

7 Comments

i have included this script,but it wont call the php script. on succes or error it doesn`t return nothing. I have also included jqeury library last version.
in data: {} should i write the function name or i let it that way?
Well, as you can see the success function does nothing here.. so pust some thing like alert("data") in success function.
Again you need to return an HTTPResponse from your php function. check HTTPResponse php manual.
something like this, -> HttpResponse::status(200); HttpResponse::setContentType('text/xml'); HttpResponse::setData('Friendship request sent!'); HttpResponse::send();
|
1

For using ajax you can use jQuery.ajax() http://api.jquery.com/jQuery.ajax/

And using ajax u will have to call the ajax function on click of the <a> tag and will have to specify the url on the ajax function.

OR

If you want to take the url from <a> tag with ajax and send it to the php page you will have to prevent the default click of the <a> tag using e.preventDefault(); and call ajax from using the url

Try something like this and work around

$('selector').click(function(){
e.preventDefault();
var url = $(this).attr('href');
$.ajax({
  url: url,
  success: function(data) {
    //do something
  }
});
})

Call this function inside $(document).ready().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.