Here's a summary of how to "catch" javascript data and send it to php and how to retrieve that data in php
Recupere os dados em PHP oriundos do Javascript
$arrayPost = $_POST; //Se foi serializado ou não
$arrayPost = json_decode($_POST["meuJSON"], true); //Se foi enviado JSON
//Codigo php que vai processar os dados e mandar de volta para o JS
$arrToJSON = array(
"dataPHPtoJs"=>"yourData",
"asYouWant"=>"<div class=\".class1\">soemting</div>"
);
return json_encode(array($arrToJSON));
//Javascript que vai mandar algo para o php
$(document).on("event", "#idElement", function(){
//Vc pode usar
var dt={
ObjEvn:"btn_Login",
dataJsToPHP: $("#txt_EmailLogin").val(),
};
//Ajax
var request =$.ajax({//http://api.jquery.com/jQuery.ajax/
url: "yourServer.php",
type: "POST",
data: dt,
dataType: "json"
});
//Ajax Done catch JSON from PHP
request.done(function(dataset){
for (var index in dataset){
dataPHPtoJsJS=dataset[index].dataPHPtoJs;
asManyasYouWantJS=dataset[index].asYouWant;
}
//JavaScript conditions. Here you can control the behaivior of your html object, based on your PHP response
if(dataPHPtoJsJS){
$( "#idYourHtmlElement" ).removeClass( "class1" )
$( "#idYourHtmlElement" ).addClass( "class2" )
}
});
//Ajax Fail
request.fail(function(jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
}
Note: If serializing use this function:
$.fn.serializeObject = function(){
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
and if you send JSON the whole function would look like this:
var meuJson = JSON.stringfy($(form).serializeObject());