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

Asked

Viewed 277 times

0

You can tell me how to finish this project, with this code below I managed 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 enters an invalid code or does not enter anything he prints the "msg fixed" 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 were user print, on the same page, has some way to do this ?

  • 1

    You can use the action for the same page as the form. You can type the page name or use $_SERVER["PHP_SELF"]. Then just check if the variable post is not empty, if (!empty($_POST)) and puts the conditions that you want to verify.

  • @lazyFox worked well by putting in the same page it generates what I want but now it does not apply the function does not recognize the value inserted in the input neither 001 nor 002 da just as 'Else' as apply the 'if (!Empty($_POST))' na ' <input type="text" name="CODNAME"><br> '

1 answer

1

Put the html page together 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 so 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>
  • Pasting the code on the same page does not change the answer always shows "fixed option" as if the function was not being applied only showing what has Else { $_POST['msg'] = "fixed option"; }

  • @Luccascerqueira this part I copied from your code, always goes to elsse because you should not receive the $_POST['CODNOME'], as it does not exist, it is always Else

Browser other questions tagged

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