I have 2 questions :
If i send the ajax request to self page (from abc.php to abc.php) like this :
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
What will msg variable contain if I have a full webpage (with div’s , forms, imgs) ? Will it contain the whole html code source ? How to tell ajax to return only specific details (like for example a php $variable after querying a database for a record based on Name and Location) . Remember some.php is the same file that contains the ajax script.
I want to make a .php script that contains all querys posible to manage a database like this:
if(isset($_GET['option']) && $_GET['option'] == 'insert') { code here .. and echo div`s .. etc) }
if(isset($_GET['option']) && $_GET['option'] == 'del') { code here .. and echo div`s .. etc) }
if(isset($_GET['option']) && $_GET['option'] == 'update') { code here .. and echo div`s .. etc) }
if(isset($_GET['option']) && $_GET['option'] == 'find') { code here .. and echo div`s .. etc) }
if(isset($_GET['option']) && $_GET['option'] == 'abc') { code here .. and echo div`s .. etc) }
and i want to run ajax request based on option and retrive specific results (like php $variables … and so on)
How do i do that ?
Anyway the most important question is how to i get ajax.result that contain just a php $variable or an $array if the page that receive ajax request already contains <html><body><divs><tables><h4><h3>.... etc ?
Because jquery.ajax() has a documentation quite complex/complicated on it’s options like accepts, async, complete, contentType, context .. and so on.
?
Thanks a lot.
You can use the dataType option to specify the type of data that you are expecting back from the server. In your case JSON might be a good option since the php page can return a String in the form:
that that is easy to process as a JSON object on the client.
e.g. Found on this page
Your callback function will then process the JSON and retrieve the value. As you can see from the link, you can use the getJSON() method as a shortcut.