Catch text from an input text

2

I have the following form

<form method="POST">

<input id="comentario" type="text" name="comentario" id="comentario">
<input type="submit" name="Comentar" value="Comentar" id="Comentar">

</form>

It is necessary to display on the screen what is typed in the input text. I also have this section that checks if the button has been clicked (I plan to do my logic here)

<?php
if(isset($_POST['Comentar'])){
        echo "botão foi clicado";

    }
 ?>
    
asked by anonymous 08.11.2017 / 22:54

2 answers

1

To receive the submission via POST , you use the variable $_POST["name_do_input"]

<form method="POST">

<input id="comentario" type="text" name="comentario" id="comentario">
<input type="submit" name="Comentar" value="Comentar" id="Comentar">

</form>
<?php
if(isset($_POST['Comentar']))
{   echo "botão foi clicado"."<br/>";
    $texto_digitado = $_POST["comentario"];
    echo  "Texto digitado: ".$texto_digitado;
}
?>
    
09.11.2017 / 00:36
1
    <form action="" method="post">
     Escolha um botão
      <button name="Comentar" type="submit" value="escolha1">HTML</button>
      <button name="Comentar" type="submit" value="escolha2">CSS</button>
    </form>

Here, if you retrieve the value of 'Comment' you can manipulate, for example, if it is not empty, it already means that some is clicked, or you can check what was ...  If the submit is done with the enter in an input the value will be empty.

  <?php
if(isset($_POST['Comentar'])){
        echo "um botão foi clicado";
      if($_POST['Comentar] =='escolha1'){
         echo "clicou em HTML";
       }
    }
 ?>
    
08.11.2017 / 23:12