views:

91

answers:

2

Sorry folks but I asked a question earlier and perhaps did not explain myself very well at all. I answered the question myself to prevent further replies and hopefully to reset the question in a more basic format, as none of the answers worked/explained to me, stress to me, how to do it. There were 4 totally different answers to one basic question.

I had hoped the question was quite simple - take a variable from an href then pass that variable through JQuery/ajax to another page and return the results into a div on the original page. In php it is simple i.e.

if(isset($_GET['nameofvariable']): // do this // endif;

I tried all the answers in my previous question with a simple echo $_GET['nameofvariable']; on the receiving page but none of them give me the right result - sorry. Perhpas a simpler version will help me (and hopefully others)

Starting page call it index.php contains just 1 link

<a href="?abc=1" id="#idoflink">Try again</a>

and one div

<div id="testit"></div>

I want to pass "abc" to another page say testit.php which contains 1 line of code

<?php echo $_GET['abc']; ?>

Then print $_GET['abc'] into the div id="testit"

OK, before you all shout, I want to use ajax (or similar) as when I understand this basic function - i.e. sending the variable and getting it to print out into the div id="testit" I then want to run a query on the page testit.php based on the variable abc and to return the results of that query into the div id="testit" without having to refresh the page.

I am trying to laern this Jquery/Ajax thingy, have a reasonable grasp of php - can do this in php but needs a page refresh for my purpose I cannot have a page refresh (even a small one in terms of the time taken to run the eventual query).

Beyond this by being able to understand this simple functionality - ie. send a GET to another page and perform a query and return I can then "develop it out" from my other understandings of JQuery/Ajax and my PHP knowledge.

Sorry if blunt. Thanks for any answers.

A: 

Insert in the head section of your index.php the following

<script type="text/javascript">
    $('#idoflink').click( function(){
       $.get(this.href, function(data){$('#testit').html(data);});
    });
</script>

and also change the link to have this href

<a href="testit.php?abc=1" id="#idoflink">Try again</a>
Gaby
I had to edit some things after re-reading the original question ..
Gaby
Shouldn't it also be this.href instead of this.src?
Richard Poole
absolutely right ... corrected..
Gaby
A: 

If I understand what you want to do correctly, you can get by with jQuery's .load function, its simplest AJAX call. The following two files give a simple example of how it might work (index.php makes an AJAX call to test.php and displays its results in the div):

index.php:

<head>
<script src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
<script>google.load("jquery", "1.3.2");</script>

<script>
  $(document).ready(function() {
    $("#idoflink").click(function() {
      $("#test").load("test.php?abc=1");
    });
  });
</script>
</head>

<body>
<a href="#" id="idoflink">Try again</a>
<div id="test"></div>
</body>

test.php:

<?
  print "abc = " . $_GET["abc"];
?>

EDIT:

In response to your comment, if you want to read the href, you can replace the onclick function with this

$("#idoflink").click(function(){
  $("#test").load(this.href);
  this.href = "#";
});

and the link with:

<a href="test.php?abc=1" id="idoflink">Try again</a>
Randy
Randy, very near, but how do I get <a href"?abc=1"> into $("#test").load("test.php?abc=1"); it cannot be hard coded as the variable value of abc will change?
russell