How to print the result of a php function from another page?

0

You can tell me how to finish this project, with this code I have been able to define the message that will appear from the number entered by the user given by the administrator

        <form action="submit.php" method="post" >
          <nav class="zz z_meio2 borda ">
insira o código especifico ou deixe em branco para resposta padrão 
            <br>
         <input type="text" name="CODNOME"><br>
          <button type="submit" >Enviar</button>
          </nav>
        </form>

The code I'm using in submit.php

 <?php 

//função para gerar respostas 

echo $CODNOME;

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

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

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

echo $msg;
?>

The above code redirects to another page.

But I would like that if the user types an invalid code or does not type anything it prints the "fixed msg" by printing it without changing page by clicking on the code below.

<input type="submit" value="Imprimir" class="btn no-print" onClick="window.print()"> 

<div class="print" STYLE="width:660px;">

<p>msg fixa</p>

</div>

This would be as if the result of submit.php was the user's impression, on the same page, do you have any way to do that?

    
asked by anonymous 22.03.2018 / 15:59

1 answer

1

Place the html page along with the php file and do this:

<?php 

//função para gerar respostas 

echo $CODNOME;

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

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

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

echo "<input type='submit' value='Imprimir' class='btn no-print' onClick='window.print()''> 

<div class='print' STYLE='width:660px;''>

<p>$msg</p>

</div>";
?>

Or you can do this too:

<?php 

//função para gerar respostas 

echo $CODNOME;

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

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

else {
  $_POST['msg'] = "opção fixa";
}

?>

<html...>
//Title, body etc
<input type='submit' value='Imprimir' class='btn no-print' onClick='window.print()''> 

<div class='print' STYLE='width:660px;''>

<p><?php echo $_POST['msg']; ?></p>

</div>
    
22.03.2018 / 16:14