Good morning for all!
I ask because I'm not an expert in front and back programming, I share my way in which I communicate the client with the server. I would like you to tell me if it's well implemented or if it can be improved? The idea is to boost our knowledge/skills every day
Method: Send with Ajax
Client-side:
<script>
jQuery.ajax({
url: 'script.php',
data:{'Var1': $('#ID').val(), 'Var2': $phpvariable},//May be FormData item too.
type: 'POST',
success:function(s){
//Received a encoded JSON from server-side with any data.
var data = JSON.parse(s);
$('#resultselement').html(data[0]);
},
error:function (){
alert('Error message');
}
});
</script>
Server-side:
<?php
$POSTVAR1 = $_POST["Var1"];
$POSTVAR2 = $_POST["Var2"];
{INCLUDES, INTERNAL VARIABLES, PHP SCRIPT, ETC}
array_push($array, $phpvar1, $phpvar2, $phpvar3....,$phpvarn);
//Sending data to client-side
echo json_encode($array);
?>
I appreciate any correction or suggestion, thanks!