Conveter PHP function for Javascript

Asked

Viewed 58 times

-1

I don’t have much knowledge of Javascript. I need to convert this check below that is in PHP to Javascript.

How can I do that?

$verificao = $_POST['verificacao_name']; 
$pesquisa1 = $_POST['p1']; 
$pesquisa2 = $_POST['p2'];

if (($verificacao == 2) and
   (($pesquisa1 <= 8 and $pesquisa1 != 0) or  
    ($pesquisa2 <= 8 and $pesquisa2 != 0) or  
    ($pesquisa3 <= 8 and $pesquisa3 != 0))) {
       echo 'Erro'; 
else{
       echo 'Ok'; 
}
  • And the $_POST['verificacao_name']; is what in Javascript?

  • It is a select for yes or no. If he chooses yes he has to perform the function, if he chooses No, he does not have to perform. Sorry not to comment on this :/

  • And what do you want to check on that select? what is 'p1'?

  • I think it would be better to say what you want to do in Javascript.

  • P1 is a value from 1 to 10. If it chooses verification = 1 (yes), it has to execute the if I quoted in the question. If <=8 does not let go, it must return false.

  • I need to do that if in Javascript, got it?

  • And where does that come from P1 select also? can display the object that would be the $_POST?

  • With the data coming via post

  • 2

    The if is exactly the same... the question is what do you want to do instead of echo.

  • 1

    How so "data coming via post"?

Show 5 more comments

1 answer

2


Given the incomplete information, I assume you would like to verify inputs, below follows one and is almost identical to the PHP:

function verificar() {
  var verificacao = parseInt(document.getElementsByName("verificacao_name")[0].value);
  var pesquisa1 = parseInt(document.getElementsByName("p1")[0].value);
  var pesquisa2 = parseInt(document.getElementsByName("p2")[0].value);
  var pesquisa3 = parseInt(document.getElementsByName("p3")[0].value);

  if (
    (verificacao == 2) && (
      (pesquisa1 <= 8 && pesquisa1 != 0) ||
      (pesquisa2 <= 8 && pesquisa2 != 0) ||
      (pesquisa3 <= 8 && pesquisa3 != 0)
    )
  ) {
    console.log("Ok");
  } else {
    console.log("Erro");
  }
}
<input type="text" name="verificacao_name" />
<input type="text" name="p1" />
<input type="text" name="p2" />
<input type="text" name="p3" />

<input type="button" value="Verificar" onClick="javascript:verificar();">

  • Thanks Marcelo, exactly that. Just convert what is in PHP to Javascript.

  • Remember that if this information is sent to the server, it is essential to validate the data also on its side and not only on the client.

  • Yeah, I used to do that.

Browser other questions tagged

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