tags:

views:

113

answers:

6

Playing with jquery for the first time, and I'm trying to get a simple AJAX set up working so I can better understand how things work. Unfortunately, I don't know a whole lot. Here's the HTML with the script:

<html>
<head>
 <title>AJAX attempt with jQuery</title>
 <script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript">
   function ajax(str){
   $("document").ready(function(){
     $.post("ajaxjquerytest.php",str,function(){
       $("p").html(response);
       });
     });
 </script> 
</head>  
<body>
 <input type="text" onchange="ajax(this.value)"></input> 
 <p>Age?</p>
</body>
</html>

And here is the PHP it's talking to:

<?php
$age = $_POST['age'];

if ($age < 30)
  {
  echo "Young";
  }
else if ($age > 30)
  {
  echo "Old";
  }
else
  {
  echo "you're 30";
  }
?>
+5  A: 

Not sure if the $.post() function has access to the str parameter. Try this instead:

<html>
<head>
 <title>AJAX attempt with jQuery</title>
 <script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript">
    $("document").ready(function(){
        $('input').change(function() {
             $.post("ajaxjquerytest.php",{'age': $(this).val()},function(response){
                 $("p").html(response);
             });
        });
    });
 </script> 
</head>  
<body>
 <input type="text" />
 <p>Age?</p>
</body>
</html>

This attaches an onChange handler to the input element after the DOM is completely loaded.

Your approach should also work if you omit $(document).ready(). But attaching the JS function to the element is more the way it is done with jQuery.

Besides that, you only need to wrap code into $(document).ready() that should be executed after the whole DOM tree is built. Your case, your are defining just a function. There is no need to use document ready as the function cannot be called until the DOM is loaded anyway.

Read the documentation of .post()!

Felix Kling
made you're suggested changes, and it got rid of two of the errors, but now firebug tells me: 'Uncaught ReferenceError: response is not defined'
Cortopasta
@Cortopasta: A I see, you have to do `function(response){$("p").html(response)}`. Updated my code. Also you have to change how to send the data to the server, it has to be {'age': $(this).val()}. The best is, you read the documentation: http://api.jquery.com/jQuery.post/
Felix Kling
+1  A: 

The easiest way to debug problems like this is to use a tool which allows you to view HTTP requests. Try the net panel in Firebug. That will let you see what url the request was made to, what postdata was sent, and what the server response was. Then you'll know whether the PHP or the JS code is the problem.

Annie
or you can also use fiddler2 from http://www.fiddler2.com/fiddler2/
Jayrox
+1  A: 

Change your function from this:

function ajax(str){
    $("document").ready(function(){
        $.post("ajaxjquerytest.php",str,function(){
            $("p").html(response);
        });
    });
}

to:

function ajax(str){
    $.post("ajaxjquerytest.php",str,function(){
        $("p").html(response);
    });
}

First, the $(document).ready() construct executes when the page is first loaded. Second, "document" should not be in quotes. Finally, you might have better success with:

<form>
    <input type="text" />
    <p>Age?</p>
    <input type="submit" />
</form>

and change:

$('input').change(function() { ... }

to:

$('form').submit(function() { ... }
Neil T.
A: 

Your ajax() function is attaching a document#ready event handler. document#ready would have fired when your page is initially loaded and it never fires again. So $.post never execute. Just change your function to this.

function ajax(str){
     $.post("ajaxjquerytest.php",str,function(){
       $("p").html(response);
     });
}
Chetan Sastry
+2  A: 

There are a couple of problems in the code you posted.

  1. It doesn't look like you're closing the brace on the "ajax" function so you should be getting a JS error when you load the page.
  2. You should either write

    function ajax(str){ $.post("ajaxjquerytest.php",str,function(){ $("p").html(response); }); } or use the $(document).ready as Felix suggested

  3. Looks like your PHP code expects a parameter named AGE but you're but you're not explicitly giving in a name in your code, you need to create a JS class with an appropriately named property like so { AGE: str }. There are some examples of doing that in the jQuery documentation.

  4. Also, be careful with using this keyword in JavaScript, it can have drastically different meanings based on what context it's used in. (Looks like the way you're using it is described at the very bottom of the page).

It may just be easier to start with Felix's example and work from there though.

R0MANARMY
The reason Felix's answer isn't working for you is because of the callback method signature `function(){...}` doesn't define a response parameter, it should really be `function(response){...}`
R0MANARMY
How do I send the value to the PHP script like you mention in #3
Cortopasta
`$.post( "ajaxjquerytest.php", { age: str }, function(){ ... });Also if you follow the jQuery documentation link I provided, they have a couple examples of how to send arguments to the script.
R0MANARMY
+4  A: 

I think the problem here is that response isn't being defined. It should be passed as a parameter to the function. Try this:

function ajax(str){
    $.post("ajaxjquerytest.php",str,function(response) {
        $("p").html(response);
    });
}
jkody21