How do I print the html content of a div when I store the information in the database?

Asked

Viewed 93 times

0

Good night,

I have a system to control incoming and outgoing computer repairs what I want and when I store the information in the database soon after I gave it to print the html content of a div where the stored data will be.

In this case I want to be saved successfully open the option to print the contents of the div.

<script> 
$(document).on("click", "#novo", function(e) { 
    tinyMCE.triggerSave();
    var postData = $("#form_novo").serialize();
    var cliente = $("#cliente").val();
    var avaria = $("#avaria").val();
    if(cliente === '' || avaria === ''){
        toastr.error('Preencha os campos obrigatórios!', 'Campos');
    }else{
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: "modulos/reparacoes/guardar.php",
            data: postData,
            cache: true
        }).done(function(msg) {
            toastr.success('Dados guardados com sucesso!', 'Reparações');
        }).fail(function(data) {
            toastr.error('Ocorreu um erro!', 'Erro');
        });
    }
});    
</script>
  • Hello, you already have the option created to print content?

  • That’s not what I’d like to do

  • What do you mean by printing? Popular fields? Send to printer?

  • Barely serious if saved successfully print the fields I will put in an html

  • Already have this html done? if yes insert in the question with the information you want to popular these fields. You will also need to get a response from the JSON server (at least that’s how I would do it).

  • I don’t have the html yet so I need to know how to print after the javascript done open the option to print this html that will be in a div

  • OK. I tried to simplify the process. If you continue to have questions ask me to change the answer.

Show 2 more comments

1 answer

0

If the save is successful you should return from the server all the data you want in JSON.

EXAMPLE.:
{NAME:'test', CONTACT:'123'}

In php to return this JSON
print_r(json_encode (array('NAME'=>'test','PHONE'=>'123')));

In the function you already have in AJAX the done should display the print option. (You can make it visible if it already exists or you can create the button/image) And must make a code similar to.:

  msg=JSON.parse(msg); //a variável msg é a variável que recebe no done
  //Vamos supor que essa opção tem o ID igual a ImprimirDados.
  $('#ImprimirDados').on('click',function(){ 
     for (var dados in msg){
        //dados tem o índex JSON exemplo NOME
        //msg[dados] tem o valor JSON exemplo teste
        //Se os índex tiverem os mesmo nomes dos campos
        $('#'+dados).val(msg[dados]);
     }
 });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.