Send message after clicking Submit button

Asked

Viewed 2,104 times

2

How do I identify with PHP if a Submit button was clicked? and after the click I want to send a click confirmation message. Detail all in PHP.!!!

So it’s not working not

<form  method="POST">

    <input type="text" name="salario"><br/><br/>
    <input type="text" name="bonificacao"><br/><br/>

    <input type="submit" name="btn">
</form>

<?php

$btn = $_POST['btn'];

if(!$btn){

   echo " O botão foi clicado ";

}else{

}
  • Doesn’t it even work when you press the button? Once you load the page the first time is expected to go wrong, as you will be making a GET request and retrieving POST values that do not exist. In fact, why the condition in the if is !$btn?

  • There’s a time when a message appears without being clicked

  • 1

    I believe the problem is at first access a Warning of undefined index is generated because the $_POST there is still no isset()/empty() should solve, for more details see How to know if the form is sent and When isset is required?

3 answers

2

TEST

if (isset($_POST['btn'])){
   echo " O botão foi clicado ";
}else{
   echo "esperando botão ser clicado";
}

1

You should use a means that relates to the POST to check if the button was clicked, see the example:

<form action="" method="post">
<input type="text" name="salario"><br/>
<input type="text" name="bonificacao"><br/>
<input type="hidden" name="validar" value="989">
<input type="submit" name="go" value="Vai" />
</form>

<?php
if(isset($_POST['validar'])){
$valida = $_POST['validar'];
if($valida == 989){
echo 'Clicado';
}
}
?>

-1

Try to take the exclamation off your condition, you’re denying the condition.

If it doesn’t work try it like this. Put the value in submit:

<form  method="POST">
  <input type="text" name="salario"><br/><br/>
  <input type="text" name="bonificacao"><br/><br/>
  <input type="submit" name="btn" value="Botão">
</form>

Whenever you submit it will send the Submit, then you check if it is passing the value:

<?php
  if (isset($_POST['btn']) and ($_POST['btn'] <> "")) {
    echo " O botão foi clicado ";
  } else { }
  • It didn’t work no! so the message is already printed on the screen

  • I want to show the message only after click!!! but I want to do this with PHP

  • 1

    Dude, the codes are in the same file?

  • I modified the code, check if it works. I did some tests here and it worked.

Browser other questions tagged

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