Variables returning trying to get Object of non Object

Asked

Viewed 73 times

0

All screen transitions, when I need to call a given variable, are with this problem. Always returns the vector as empty.

When transitions are made with defined value, for example: $_POST ['vaga'] = ['2'], the vector is returned with all its variables (40) error-free on the form html.

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

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

page 2 - details of the selected vacancy on page 1 button;

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 vary it according to the clicked button, the problem comes. I noticed that this occurs in all other pages (login, graphics, etc), because whenever I use a fixed number the system accepts, when it comes to making it variable the thing hangs.

Code that doesn’t 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);

?>

page 2 call function 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');
                    },
                });
      }

  • look in the field of hml if you put the name attribute. Because the problem is in the parameter passage. add here your html code of form 1 and I’ll show you where the problem is

  • I cannot include the code here in the comment...lack space

  • <img src="imgs/plus.png" width="30" height="30" alt="More information§Ãµes" style="cursor:Pointer" id="btnloaddetails" name="detalhesvaga" onclick="loaddetalhes('<?= $v->id_vaga; >');"></td>

  • edit your question and include html

1 answer

0

To find out the error you need to check if the parameter arrives correctly, with:

var_dump($_POST); before the foreach, if the vacancy comes it means that you are passing the variable correctly. Or if you think it best to do so in javascript console.log(idVaga), before the $.ajax({ by the browser console you will see.

To handle vacancies that do not exist make a simple change in your code, so you avoid errors.

First eliminate your foreach and switch 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 go to good practice, eliminate your foreach, treat everything with Try catch and change the variable $Return for another name, Return is reserved PHP word.

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

Browser other questions tagged

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