I have a jQuery php script which accesses a database and echoes back html to fill a div on my page. It uses the following code.
function myCall() {
var request = $.ajax({
url: "ajax.php",
type: "GET",
dataType: "html"
});
request.done(function (msg) {
$("#divholder").html(msg);
});
request.fail(function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
}
What I need to do is also send a variable through to the script so it knows what to look for in the database. I have been searching but I can't seem to find quite the right answer. Any help greatly appreciated.
Regards.
You can send the parameter like this :
function myCall() {
var request = $.ajax({
url: "ajax.php?param1=" + 'parameter',
type: "GET",
dataType: "html"
});
request.done(function(msg) {
$("#divholder").html(msg);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
And get the parameter with $_GET
Or you can send the parameter with POST (look at data : Ajax jQuery)
With a get request just append it to the url.
$.ajax({
url : 'ajax.php?someVar=20',
type : 'GET',
dataType : 'html'
)}
You can also add it to the data property and jQuery will url encode your data and append it to the url string.
$.ajax({
url : 'ajax.php',
type : 'GET',
dataType : 'html',
data : {
someVar : 20
}
)}