as a full html code as TEXT in database through AJAX?

0

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

    
asked by Jra 30.11.2018 в 12:29
source

2 answers

0

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) {
                    }
});
    
answered by 30.11.2018 / 12:37
source
0

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!';
}
?>
    
answered by 30.11.2018 в 12:37