Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6842913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:09:35+00:00 2026-05-27T00:09:35+00:00

I’ve been trying to get this really simple example of using AJAX with JQuery

  • 0

I’ve been trying to get this really simple example of using AJAX with JQuery and PHP to work with no luck (here’s the page for the sample). I’ve had a look at quite a few posts with similar discriptions and none have helped…

I’ve copied the code exactly but my function that should be run on success is never called. As an experiment, in the call to $.post I commented out the data part ({sendValue:str}) and the JSON part and added an alert into the body of the success function to see if it was called and it was. So I’m guessing there’s something wrong with how I’ve created my data? I also tried to display the data returned from the AJAX call in the alert and it came out as ‘undeclared’ (data.returnValue).

This is a copy of my code, you can see the full example via the link above and also a working example from the author of the tutorial here: http://www.devirtuoso.com/Examples/jQuery-Ajax/

JQuery:

        $(document).ready(function(){
            $('#txtValue').keyup(function(){
                sendValue($(this).val());
            });
        });

        function sendValue(str){
            $.post("ajax.php",
                   { sendValue: str },
                   function(data){
                       $('#display').html(data.returnValue);
                   },
                    "json"
            );
        }

PHP:

<?php

//Get Post Variables. The name is the same as
//what was in the object that was sent in the jQuery
if (isset($_POST['sendValue'])){
    $value = $_POST['sendValue'];  
}else{
    $value = "";
}

//Because we want to use json, we have to place things in an array and encode it for json.
//This will give us a nice javascript object on the front side.
echo json_encode(array("returnValue"=>"This is returned from PHP : ".$value));

?>

HTML:

    <body>

    <p>On keyup this text box sends a request to PHP and a value is returned.</p>

    <label for="txtValue">Enter a value : </label><input type="text" name="txtValue" value="" id="txtValue">

    <div id="display"></div>

</body>

Thanks!

EDIT:
I rewrote my $.post into a $.ajax with an error function in it. I’m definitely hitting the error function and the error is a parse error – I’m guessing it’s coming from my PHP script when I call json_encode… here’s a screenshot from firebug – anyone got any more ideas?:

Screenshot 1 – firebug console

Screenshot 2 – firebug watch window

screen grab of firefox console
screen grab of watch on error function

Thanks for all the help so far by the way, really appreciate it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:09:36+00:00Added an answer on May 27, 2026 at 12:09 am

    I noticed that var str = $('#txt').val(); would give you an error because $('#txt') does not exist, it should be $('#txtValue').

    After looking at your code, everything looks as it should, my next step would be trying to debug your code by using some console.debug() in JavaScript and some echo in PHP. I recommend you get Firebug for Chrome/Firefox and if using IE upgrade to IE9 and use their developer tools. Using the mentioned tools will give you a better idea of how your code is executing.

    My first step would be to make sure that the keyup is firing:

     $(document).ready(function(){
            $('#txtValue').keyup(function(){
                alert('keyUp');
                sendValue($(this).val());
            });
     });
    

    Second step would be to make sure sendValue is firing:

    function sendValue() {
        alert('sendValue');
        var str = $('#txt').val();
        tmr = null;
    
    
        $.post(
            'test.php', 
            { sendValue: str }, 
            function(data) { 
                alert('inside post');
                $('#output').html(data.returnValue); 
            }, 
            'json'
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.