PHP execution via Ajax jQuery

Asked

Viewed 451 times

4

I am placing some instructions inside the same PHP file that are executed according to the variable value send received, at least that’s what I thought. This code has two of these instructions, one that receives send == 'buUserBloqueia' and another who receives send == 'cCadSend' but once the first receives the command, executes the instruction and does not finish executing also the next ones that has another value of send.

Code

// Função serve para bloquear o usuário do sistema
if((isset($_POST['send']) == "buUserBloqueia") && (isset($_POST['id']) != "")){

// Esta função bloqueia um usuário

exit();
}

// Função serve para cadastrar o usuário no sistema
if ((isset($_POST['send']) == "cCadSend")){

// Esta função cria um usuário

exit();
}

Why is it that by submitting Ajax with a value of send after checking the value of the send in PHP, the script keeps running all the instructions below it?

  • Attention problem in reused code in which within the instruction was repeating an Insert. Served to learn that isset returns true or false. Hugs

3 answers

3

Assuming you want to literally create an HTTP router, this is not the best way to do it. In addition to not being good for the quality of the code, it is not possible to test and end the sanity of the programmers.

I suggest you take a look at components/libraries ready to do this work for you, such as Silex which is a very easy-to-use mini-framework. Oinclusive it already screwed with security issues by you, allowing you to work with Friendly Urls in an easy way.

Here’s an example of what your two actions would look like framework:

 require_once __DIR__.'/../vendor/autoload.php'; 

 $app = new Silex\Application(); 

 $app->post('/usuario/criar', function(){ 
    //aqui cria um usuário e retorna uma resposta 
 }); 

 $app->post('/usuario/bloquear', function(){ 
    //aqui bloqueia um usuário e retorna uma resposta 
 }); 

 $app->run(); 

I have a repository with an application that uses this mini-framework that I did for a college job. Suddenly it can help someone.

2

To check a set of actions I prefer to use the switch, I think it is easier to understand and modify the instructions...

switch ($_POST['send']) {

    case 'buUserBloqueia':
        // Esta função bloqueia um usuário
        exit();
    break;

    case 'cCadSend':
        // Esta função cria um usuário
        exit();
    break;

}

1


The function isset() of PHP returns true or false.

This means that this comparison isset($_POST['send']) == "buUserBloqueia" is always false for isset($_POST['send']) will not appreciate the $_POST['send'] but yes true or false.

Deep down it would be the same as comparing if (true == "buUserBloqueia").
I suggest you re-draw your code a little bit and do something like:

if(isset($_POST['send']) && $_POST['send'] == "buUserBloqueia") && (isset($_POST['id']) != "")){

Or put a if that makes Exit earlier, before the code you have:

if(!isset($_POST['send'])) exit();

Browser other questions tagged

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