I have a form, where I have a single input, and a button.
<form role="form" method="post">
<div class="form-group mb-none">
<div class="input-group">
<input placeholder="E-mail" type="text" name="entrada" class="form-control input-lg">
<span class="input-group-btn">
<button name="operar" class="btn btn-primary btn-lg" >Activar! </button>
</span>
</div>
</div>
</form>
and the php code I have the following:
<?php
if (!empty($_POST['operar'])){
if (strlen($_POST['entrada']) > 9){ //valida que el campo no este vacio y que sea mayor de 9
echo "biennnnn";
}else{
?>
<div class="alert alert-success">
<p class="m-none text-semibold h6">
Listo! Tu cuenta se encuentra activa nuevamente. </p>
</div>
<?php
}
}
?>
It's all in one file. I press the button and nothing happens, everything stays the same. What I want is that, initially, the first echo works for me: echo "biennnnn";
If you do a var_dump of the entire message, you'll notice that it's actually
$_POST['operar']
a string of length 0, so it doesn't meet your first condition.if (!empty($_POST['operar']))
You can check it with
var_dump($_POST)
To evaluate the value of element
<button>
you need to add the attributevalue
. If you do the test as in the line below, your code will work.<button name="operar" value="mi-valor" class="btn btn-primary btn-lg" >Activar! </button>
I don't know if what you want is to confirm if there was a post or not, or if it comes with empty values, since the button's value is predetermined by you, then it would be constant. To evaluate if there was a post or not I would use
if ($_SERVER['REQUEST_METHOD'] == 'POST')
because it
if (!empty($_POST))
can fail in certain cases (example:check boxes
andbutton
withoutname
)Things I see that are wrong and you should change:
In your label
form
you do not have aaction
, even if it goes to the same file you should indicate it like this:EITHER
Your button should be a
<input type="submit"
instead of<button>
and the Activate text to be inside the button must be in the attributevalue
Finally, in your PHP , to recognize the button pressed, use
isset()
instead ofempty()
to know if it is defined.More information about the isset() function