Error undefined index php

0

I am making a page for a school project and I try to show some communications. This is the code where I request that releases I want to show depending on who is logged in.

<?php 
    if (isset($_SESSION['username']) && $_SESSION['rol']=='administrador'){
     echo '<button type="button" class="btn btn-primary  btn-sm"   onclick="cargar(\'#capa_d\',\'alta_co.php\')">Nuevo Comunicado</button>';
echo '<button type="button" class="btn btn-primary  btn-sm"   onclick="cargar(\'#capa_L\',\'mostrar_comunicados.php?b=administrador\'),cargar(\'#capa_d\',\'mostrar_comunicados.php?c=administrador\')">Ver Comunicados</button>';
    }
if (isset($_SESSION['username']) && $_SESSION['rol']=='Docente'){
 echo '<button type="button" class="btn btn-primary  btn-sm"   onclick="cargar(\'#capa_d\',\'alta_co.php\')">Nuevo Comunicado</button>';
echo '<button type="button" class="btn btn-primary  btn-sm"   onclick="cargar(\'#capa_L\',\'mostrar_comunicados.php?b=Docente\'),cargar(\'#capa_d\',\'mostrar_comunicados.php?c=Docente\')">Ver Comunicados</button>';
}
if (isset($_SESSION['username']) && $_SESSION['rol']=='Padre'){
 echo '<button type="button" class="btn btn-primary  btn-sm"   onclick="cargar(\'#capa_d\',\'alta_co.php\')">Nuevo Comunicado</button>';
echo '<button type="button" class="btn btn-primary  btn-sm"   onclick="cargar(\'#capa_L\',\'mostrar_comunicados.php?b=Padre\'),cargar(\'#capa_d\',\'mostrar_comunicados.php?c=Padre\')">Ver Comunicados</button>';
}
  ?>

And the error Notice: Undefined index: b in show_comunicados.php on line 5 I get in these lines of code of show_comunicados.php that is where I send them above

$str_b = $_GET['b'];
$str_c =  $_GET['c'];

I appreciate any help

    
asked by Santiago Faverio 09.09.2018 в 01:42
source

2 answers

2

That error occurs because you are not passing the "b" parameter in the url.

I recommend you check if you are receiving those parameters and assign a default value before using them:

$str_b = array_key_exists('b', $_GET) ? $_GET['b'] : '';
$str_c = array_key_exists('c', $_GET) ? $_GET['c'] : '';

In this example, the default value is an empty string, but you can assign it the value you want.

    
answered by 09.09.2018 / 02:06
source
2

The problem is that your GET variables are not defined, you should first validate it with a simple if and the isset function, which will return true as long as those variables are defined, you can try the following code:

if( isset($_GET['b']) && isset($_GET['c']) ){
   $str_b = $_GET['b'];
   $str_c =  $_GET['c'];
}
    
answered by 09.09.2018 в 02:08