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();
}
var searchTerm
is not defined in a global scope? Assuming you are calling "initialize" AFTER the global variable "searchTerm" is defined, it should work.searchTerm
contains something...window.location.reload()
immediately after executing an ajax call is going to result in nothing at all happening (except for the page reloading).alert(searchTerm)
right before the$.ajax()
to see what's been passed in to the function.