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

Asked

Viewed 60 times

0

The code below is working very well, but it shows the result in his page Submit.php.

    <?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 is for him to run the function in Submit.php and then come back with the result to print.php in 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 get the data entered by the user in the print.php send to Submit.php and bring with the result in the div print ?

2 answers

1


You can use ajax but in your example it is easier so:

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>

Print file.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>
  • did not work because I tried exactly is to do the processing of this php code in javascript.

  • 1

    @Luccascerwhether my answer is valid. You can use ajax or the mentioned code. It will send directly to the print page and place the $msg variable in the div. Edit your answer and explain better what you need if the answer hasn’t served (y).

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>

prints.php

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

Browser other questions tagged

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