Help getting data from JS in PHP

0

I'm looking at a JS course, and it goes through the AJAX part, I understand the packaging and everything, the only doubt I have is when validating it in php $usuario = isset($_GET["usuario"]) ? $_GET["usuario"]: $_POST["usuario"];

My doubt there is with the?, I know it's a shorter way to write the if else, but even so I do not understand it, the user variable is going to be the same as isset ("$ _ GET [" user "] ]) if it returns true, that is, it has something, otherwise $ _GET ["user"] = $ _POST ["user"] ??? I do not understand, how do I get the $ _POST ["user"] if I am sending it by getting from the form, I hope you understand me, if you do not explain what you are doing that part ... thanks

    
asked by Shum 18.05.2018 в 06:03
source

2 answers

0

When you have doubts with the abbreviated form of if , I suggest you write the same but in its extended form (the classic one), just to clarify the options, then delete it. In your case:

if (isset($_GET["usuario"]))
{ $usuario = $_GET["usuario"]; }
else
{ $usuario = $_POST["usuario"]; }

    
answered by 18.05.2018 в 06:37
0

Let's see, I'll try to explain it in the best way I can. First,

$usuario = isset($_GET["usuario"]) ? $_GET["usuario"]: $_POST["usuario"];

It can be translated as:

if(isset($_GET['usuario'])){
     $usuario = $_GET['usuario'];
}else{
     $usaurio = $_POST['usuario']:
}

Which means that if the value 'user' exists within the superglobal $_GET[] , $usuario will take that value, and that if it does not exist, $usuario will take the value that the superglobal $_POST[] saves in the 'user' position, where ? equals if as such, and the condition goes before if in this case.

In that statement there is a possible error, and that is that you should contemplate the possibility that the 'user' position does not exist, both in $_POST[] as in $_GET[] . That translated into a ternary operator would be:

$usuario = isset($_GET['usuario']) ? $_GET['usuario'] : (isset($_POST['usuario']) ? $_POST['usuario'] : [VALOR POR DEFECTO/ O DE ERROR]);

This way you would avoid the Undefined Index error that would show your php when executing the code without $_GET['usuario'] or $_POST['usuario'] .

    
answered by 18.05.2018 в 11:21