0

I am trying to do a simple AJAX function to send data from query string, and the append the DIV. .I have tried a variety of different methods, but none that will updat the div. If it updates, it returns NULL, otherwise it just goes to the request.php page. If I move the prevent default function under the click event, it does nothing. Any help would be appreciated!! TIA

<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(function()
{
    $("#link").click(function(e)
    {

        $.ajax(
                {
                    type    : "GET",
                    url     : "request.php",
                    data    : { id: link.attr('id') },
                    success : function(response)
                    {
                        $("#ajaxresponse    div").fadeOut("fast", function()
                        {
                            $("#ajaxresponse div").remove();
                            $("#ajaxresponse").append($(response).hide().fadeIn());
                        });

                    }
                });

        e.preventDefault();
    });
});

</script>
</head><body>
<h1>
    AJAX with PHP
</h1>
<div class="content">

<a href="request.php?id=1" id="link" data-id="1" >Submit Link</a>

</div>
<div id="ajaxresponse">
    <div>please submit the form</div>
</div>

The request.php is as follows:

<?php
$username = $_GET['id'];


echo getTemplate($username);

function getTemplate($username)
{
return '<div class="box">
    <h1>The ID is</h1>
    <div class="meta">username: '.$username.'</div>
</div>';

}

?>
2
  • What is the data if you access the url directly, and what is in response Commented Jun 18, 2012 at 13:43
  • I am just trying to send the "1" at the end of the url: request.php?id=1 Commented Jun 18, 2012 at 14:13

1 Answer 1

1

I guess you are trying to read the id value of the link being clicked in a wrong way. This should work.

$(function()
{
    $("#link").click(function(e)
    {
       var link=$(this);
       e.preventDefault();
        $.ajax(
                {
                    type    : "GET",
                    url     : "request.php",
                    data    : { id: link.attr('id') },
                    success : function(response)
                    {
                        $("#ajaxresponse div").fadeOut("fast", function()
                        {
                            $("#ajaxresponse div").html(response).fadeIn();
                        });

                    }
                });   

    });
});

You can even use the get method which is a short version of jQuery ajax call with method type as GET.

$(function()
{
    $("#link").click(function(e)
    {
       var link=$(this);
       e.preventDefault();
       $.get("request.php?id="+link.attr('id'),function(response){
               $("#ajaxresponse div").fadeOut("fast", function()
               {
                     $("#ajaxresponse div").html(response).fadeIn();
               });   
       });             
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

I am just trying to send the "1" at the end of the url: request.php?id=1...there is prob a better way, Im just new :)
@Jjames: There is no problem in that.
When I run it, I get back "link", the ID of the anchor tag, instead of the 1 on the query string
I got it...just had to change the link.attr to link.data....probably my crappy question phrasing :)...thanks a bunch!!!

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.