0
Good morning,
I have these fields in an html form and I’m trying to validate it with php. In my validation with php, I need only one of the fields (any one) to be required. In this case, the user needs to fill in at least one of the fields in order to send the form.
<input type="email" name="email" id="oemail" placeholder="Digite seu E-mail">
<input type="tel" name="whats" id="whats" placeholder="Digite seu whatsapp" maxlength="15">
<input type="tel" name="telefone" id="telefone" placeholder="Digite seu telefone" maxlength="14">
I am doing the validation with the following code. (working)
if (empty($whats) OR strstr($whats, ' ')==false) {
$erro = 1;
}
if ($erro != 0) {
echo '<script type="text/javascript">window.location = "erro.php";</script>';
exit;
}
I tried to use the following code, but as the understanding You can see, it doesn’t work:
if (empty($whats) OR strstr($whats, ' ')==false) and (empty($telefone) OR strstr($telefone, ' ')==false) and (empty($email) OR strstr($email, ' ')==false) {
$erro = 1;
}
if ($erro != 0) {
echo '<script type="text/javascript">window.location = "erro.php";</script>';
exit;
}
The questions are as follows::
- For my problem, this is the right way to validate the form? If not, which would be the best?
- How I could assemble the logic of the second attempt, correctly within php?
I just don’t understand the purpose of
strstr
, what does it validate? Related: Qual a diferença entre “&&” e “||” e “and” e “or” em PHP? Qual usar?– rray
I took this code from the internet, I don’t know exactly what it does. I’m just front programmer.
– Leo Amaral
@Leoamaral
strstr()
is being used to check if there are no spaces in the received string. If you do not need this validation you can remove: http://php.net/manual/en/function.strstr.php– Ricardo Moraleida