How To Save File After It Had Been Edited With Jquery Using Php?
Solution 1:
It is currently not possible for Javascript (in a browser) to edit/save files like you want. However, you can use .ajax() to send the data back to a PHP file so that the PHP file can save it using file_put_contents() (though it would usually save it in a database instead).
Solution 2:
Read up on JQuery Ajax function and also read about JSON objects.
With AJAX and JQuery you can easily send a JSON object to your server like so:
function saveElements(myJsonObj) {
$.ajax({
method: "POST",
dataType: "json",
url: "path/to/savemyobject.php",
data: {json:myJsonObj}
});
}
A JSON file may look something like this:
{ "myData":
{
"some_text":"this could be the CSS code",
"name":"some name",
"number":"some number"
}
}
You could generate a JSON string representing a JSON object and send it to a PHP file with the Ajax function (as above).
In PHP you would do this to obtain the object:
<?php
$json_string = $_POST["json"]; //the same variable key we sent it in
$json_obj = json_decode($json_string, true);
// get data
$myDataCode = $json_obj["myData"]["some_text"];
// do some processing
...
?>
You could also convert an array in PHP to a JSON object using the PHP json_encode function.
Read more:
http://www.w3schools.com/json/default.asp
http://php.net/manual/en/function.json-decode.php
http://php.net/manual/en/function.json-encode.php
http://api.jquery.com/jQuery.ajax/
Post a Comment for "How To Save File After It Had Been Edited With Jquery Using Php?"