I have a html code, I need to use AJAX, by pressing the button: Save, that all the code is saved in the database in a field type TEXT
I have a html code, I need to use AJAX, by pressing the button: Save, that all the code is saved in the database in a field type TEXT
Hi, you could put your html code inside a div, and with the ajax get its content to send it to the BD.
Something like this:
<div id="divConHTML">
.......codigo html
</div>
and the ajax:
$.ajax({
type: "POST",
url: "mandaraBD.php",
data: {
'html' : $('#divConHTML').html(),
},
dataType: "html",
success: function (response) {
}
});
You could pass the data through ajax to PHP and this will be printed in a txt.
Try the following:
-AJAX:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#save').live('click', function() {
var dataString = 'data='+$('textarea#data').val();
$.ajax({
type: "POST",
url: "writetxt.php",
data: dataString,
success: function(data) {
//alert(data);
$('form').remove();
$('.content').append('<p>Tu texto se ha guardado correctamente!</p><a href="data.txt" target="_blank">Ver</a>');
}
});
return false;
});
});
</script>
-PHP:
<?php
require_once("classes/class.inputfilter.php");
$ifilter = new InputFilter();
$data = $ifilter->process($_POST['data']);
$file = 'data.txt';
if (isset($data)) {
$fp = fopen($file, 'w');
fwrite($fp, utf8_decode($data));
fclose($fp);
chmod($file, 0777);
echo 'Se han guardado correctamente la información en el txt!';
}
else {
echo 'No hay datos que guardar!';
}
?>