0

I've seen lots of similar questions but I can't find an answer! I am trying to open a PHP file, passing some Javascript variables into the URL, using $.ajax. But, when I define the variable in Javascript then try to use it inside $.ajax, it returns null and the variable is not defined. How can I pass this variable?

Thanks in advance! -C

var searchTerm = "startups";
function Initialize() {
  PopulateTable(searchTerm);
 }
function PopulateTable(searchTerm) {
    $.ajax({  
            type: "POST",
            dataType: "text",
            data:   "tableName=Events&searchTerm=" + searchTerm, 
            // It's not recognizing my JS variables inside Ajax. Has it always been this way?           
            url: "/php/postData.php",  
            success: function(data, textStatus, jqXHR){ 
                alert(data);
            }
        });
    window.location.reload();
}
6
  • 2
    Doing AJAX and immediately reloading the page seems like you might be shooting yourself in the foot. I would speculate that the reload is causing "funny" behaviour with that ajax request. Better to avoid that reload altogether. Maybe put it into your "success" function if the alternative means massive rewriting. Fix that and see if the problem persists. Commented Jul 5, 2011 at 19:27
  • I don't see anything wrong with this, even if your use of globals and parameters is a bit confusing. Is it possible var searchTerm is not defined in a global scope? Assuming you are calling "initialize" AFTER the global variable "searchTerm" is defined, it should work. Commented Jul 5, 2011 at 19:29
  • make sure searchTerm contains something... Commented Jul 5, 2011 at 19:32
  • 1
    .. although i missed this crucial bit before, calling window.location.reload() immediately after executing an ajax call is going to result in nothing at all happening (except for the page reloading). Commented Jul 5, 2011 at 19:32
  • Is searchTerm even set when PopulateTable is called? try doing alert(searchTerm) right before the $.ajax() to see what's been passed in to the function. Commented Jul 5, 2011 at 19:35

1 Answer 1

7

$.ajax({ ... will spawn an asynchronous request to your php resource. window.location.reload(); will reload your page before that request is received and anything can be done with it. If you need to reload the page, do it inside here:

        success: function(data, textStatus, jqXHR){ 
            alert(data);
            window.location.reload();
        }
2
  • Doh! The problem was staring me right in the face and I wasn't looking. This worked perfectly after moving window.location.reload(). Thank you! Commented Jul 5, 2011 at 23:22
  • Hey Charles, if this solved your problem you should mark it as accepted. That way I get points and it helps others :) Commented Dec 10, 2012 at 19:31

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.