In your example, you're simply missing a key bit of information: which element you want updated. It can be the HTML body
, or something like a div
or section
.
Let's assume that you replace the entire body
with the results of the Ajax call. In your Ajax success
callback, you simply place the return value from the Ajax call into the body
:
var datas = {action: 'update', subject: sendSub, criteria: changed[5], creditos: changed[4], g1: changed[0], g2: changed[1], g3: changed[2], g4: changed[3]};
$.post('/edit.php', datas).done(function (data) {
$("body").html(data); // Replace the body's HTML with the contents of data
console.log(data);
});
Make sure that any Javascript code that you're executing or depending on does not exist in the body. Load Javascript in the head in this case, or it will disappear when the body is replaced.
If instead you wanted to update a div
(or section
), and it's used to contain the results of the Ajax call:
<div id="results"></div>
In this case, your Ajax success
callback will replace the div
content with the return value from the Ajax:
var datas = {action: 'update', subject: sendSub, criteria: changed[5], creditos: changed[4], g1: changed[0], g2: changed[1], g3: changed[2], g4: changed[3]};
$.post('/edit.php', datas).done(function (data) {
$("#results").html(data); // Replace the div's HTML with the contents of data
console.log(data);
});
That's really all you need to do.
If you happen to have scripts in the data, they don't need to be protected with $(document).ready()
, because the document is already loaded, you're just replacing the body of a div element.
It's just that simple.