How to bring a result from a .php page to another .php page using javascript?

0

The code below is working fine, but it shows the result on his submit.php page.

    <?php

    // submit.php


    echo $CODNOME;

    if($_POST['CODNOME'] == "001"){
      $msg = "mensagem1";
    } 

    else if($_POST['CODNOME'] == "002"){
      $msg = "mensagem2";
    }

    else {
      $msg = "opção fixa";
    }


    echo $msg;

   ?>

What I would like and have it perform the function in submit.php and then come back with the result to print.php in the div print

<!-- imprimir.php !-->

<form method="post" id="CODNOME" action="submit.php">
      <nav class="zz z_meio2 borda ">
  insira o codigo
<br>
      <input type="text" name="CODNOME" id="CODNOME"><br>
      <button type="submit" >Enviar</button>

      </nav>
    </form>

<div class="print" STYLE="width:660px;">
  <?php echo $msg;?>
     <p>*conteúdo html e php</p> 
</div>

Then how to create a javascripit to grab the data entered by the user in print.php send to submit.php and bring with the result in div print?

    
asked by anonymous 23.03.2018 / 18:28

2 answers

1

You can use ajax but in your example it gets easier like this:

Your form:

<form method="post" id="CODNOME" action="imprimir.php">
      <nav class="zz z_meio2 borda ">
  insira o codigo
<br>
      <input type="text" name="CODNOME" id="CODNOME"><br>
      <button type="submit" >Enviar</button>

      </nav>
    </form>

File print.php:

<?

    if($_POST['CODNOME'] == "001"){
      $msg = "mensagem1";
    } 

    else if($_POST['CODNOME'] == "002"){
      $msg = "mensagem2";
    }

    else {
      $msg = "opção fixa";
    }


   ?>    
<div class="print" STYLE="width:660px;">
      <?php echo $msg;?>
         <p>*conteúdo html e php</p> 
    </div>
    
23.03.2018 / 19:30
0

Below is an example using 2 files, one for the form and one for printing.

index.php (Form)

<html>
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script><script>$(function(){$("#btn").click(function(){
                            var aluno = $("input[name=aluno]").val();
                            var data = $("input[name=data]").val();
                            var horario = $("input[name=horario]").val();
                            var valor = $("input[name=valor]").val();
                            $.ajax({
                                    type: "POST",
                                    data: { primeiro:aluno, segundo:data, terceiro:horario, quarto:valor },
                                    url: "imprime.php",
                                    dataType: "html",
                                    success: function(result){
                                            $("#imprime").html('');
                                            $("#imprime").append(result);
                                    },
                                    beforeSend: function(){
                                            $('#loading').css({display:"block"});
                                    },
                                    complete: function(msg){
                                            $('#loading').css({display:"none"});
                                    }
                            });

                    });
            });
    </script>
<body>
    <div id="loading" style="display: none;">Carregando...</div>
    <input type="text" name="aluno" /></br>
    <input type="text" name="data" /></br>
    <input type="text" name="horario" /></br>
    <input type="text" name="valor" /></br>
    <button id="btn">Imprimir</button>
    <div id="imprime"></div>
</body>

imprime.php

echo $_POST["primeiro"].'</br>'.
$_POST["segundo"].'</br>'.
$_POST["terceiro"].'</br>'.
$_POST["quarto"];
    
23.03.2018 / 21:47