Variables returning trying to get object of non object

0

All screen transitions, when I need to call a variable data, have this problem. Always return the vector as empty.

As long as the transitions are made with value defined, for example: $_POST ['vaga'] = ['2'] , the vector is returned with all its variables (40) without error in form html .

But the problem is that the value quoted in the example ['2'] must be variable, since it must be assigned from another list. The relationship is:

page 1 - list of vacancies with 5 main variables and 1 button to present the details of the vacancy;

page 2 - details of the job selected in the button on page 1;

The code that works, with the fixed variable is:

include_once('class_vaga.php');

$obj = new class_vaga();
$obj->host = '127.0.0.1';
$obj->username = 'root';
$obj->password = '';
$obj->table = 'vaga';
$obj->connect();


$_POST ['vaga'] = ['2'];

$return = json_decode($obj->ajaxCall('load_detalhes_vagas',array($_POST['vaga'][0])));

var_dump($return);

?>

When I try to make it vary according to the button clicked, the problem comes. I noticed that this happens on all other pages (login, graphics, etc), because whenever I use a fixed number the system accepts, at the time of making it variable the thing hangs.

Code that does not work:

include_once('class_vaga.php');

$obj = new class_vaga();
$obj->host = '127.0.0.1';
$obj->username = 'root';
$obj->password = '';
$obj->table = 'vaga';
$obj->connect();

$id_vaga = ' ';

foreach ($_POST as $key => $value) {

    if($key == "action"){
        $action = $value;
    }else{
            $id_vaga = $value;
    }
}

$return = json_decode($obj->ajaxCall('load_detalhes_vagas',array($id_vaga)));

var_dump($return);

?>

The call function on page 2 is:

function loaddetalhes(idVaga){
       $.ajax({
        type: "POST",
        url: 'http://localhost/workspace/detalhes_vaga.php',
                    data: { action: 'load_detalhes_vagas', vaga: idVaga },
                    dataType: "json",
                    complete: function (data){
                    window.open('detalhes_vaga.php');
                    },
                });
      }
asked by anonymous 17.05.2016 / 03:37

1 answer

0

To find the error you need to verify that the parameter arrives correctly, with:

var_dump($_POST); before foreach , if it comes the wave means that you are passing the variable correctly. Or if you find it better do this in the javascript console.log (idVaga), before the $.ajax({ from the browser console you will see.

To handle openings that do not exist, make a simple change to your code, so you deflect errors.

First delete your foreach and change to this code:

if(isset($_POST['vaga'],isset($_POST['action']) && $_POST['vaga'] && $_POST['action']){
     $json = json_decode($obj->ajaxCall($_POST['action'],$_POST['vagas']));
     var_dump($json);
}

Now let's practice, delete your foreach , treat everything with try catch , and change the $ return variable to another name, return is PHP's reserved word.

At least the errors will disappear, the tyr get non object error means that it is trying to fetch an object in a null variable or not an object.

    
17.05.2016 / 14:39