1

I am coding a dynamic chained select and it's working, however when I try to get the selected values I get this error:

Notice: Undefined index: ciudad in C:\Users\Syscom\Documents\PEDRO\Dropbox\xampp\htdocs\Syscom\_\logueado.php on line 11

Notice: Undefined index: departamento in C:\Users\Syscom\Documents\PEDRO\Dropbox\xampp\htdocs\Syscom\_\logueado.php on line 13
Pais escogido .Departamento escogido .Ciudad escogido .

Here is my code:

    <?php
require_once ('funciones.php');
include 'includes/iniciosinbanner.php';

session_start();
if ($_SESSION['logged'] == 'yes')//
{

    if (isset($_POST['bciudad'])){

        $ciudad=$_POST['ciudad'];
        $pais=$_POST['pais'];
        $departamento=$_POST['departamento'];
        echo 'Pais escogido '.$pais.'.'; 
        echo 'Departamento escogido '.$departamento.'.'; 
        echo 'Ciudad escogido '.$ciudad.'.'; 
    }


    else{

        echo 'Bienvenido '.$_SESSION['nombreusuario'].'.'; 

    $con=fullconectar();
    $res=mysql_query("select * from paises",$con);
    ?>

    <br><br>

    <table border="1" width=800 align="center">
    <tr >
        <td  width="30%">
        <form name "buscar" action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post">
    <font color="#27A7D6">Buscar por País:<br></font>
    <select id="pais" onchange="this.form.submit();" name="pais" style="width:150px">    
       <option value="">Seleccione</option>
       <?php
       while($fila=mysql_fetch_array($res)){
        ?>
        <option value="<?php echo $fila['nombrepaises']; ?>"><?php echo $fila['nombrepaises']; ?></option>
        <?php } ?>
   </select>

    <input type="submit" name="bpais" value ="Buscar">
    <br><br>

<!-- INICIO DEL SELECT DEPARTAMENTO-->
    <?php
if(isset($_POST['pais'])){ // REVISAR SI HAY VALOR ESCOGIDO EN PAIS
$pais=$_POST['pais']; 
$con=fullconectar();
$res=mysql_query("select distinct departamento from sitios where pais='".$pais."'",$con);
?>
<font color="#27A7D6">Buscar por Departamento/Región:<br></font>
<select id="departamento" name "departamento" style="width:150px">
<?php while($fila=mysql_fetch_array($res)){ ?>
<option><?php echo $fila['departamento']; ?></option>
<?php } ?>
</select>
<input type="submit" name="bdepartamento" value ="Buscar">
<?php } ?>
<br><br>

 <!-- INICIO DEL SELECT CIUDAD-->
<?php
if(isset($_POST['pais'])){ // REVISAR SI HAY VALOR ESCOGIDO EN PAIS
$pais=$_POST['pais']; 
$con=fullconectar();
$res2=mysql_query("select distinct ciudad from sitios where pais='".$pais."'",$con);
?>
<font color="#27A7D6">Buscar por Ciudad/Municipio:<br></font>
<select id="ciudad" name "ciudad" style="width:150px">
     <option value="">Seleccione</option>
<?php while($fila=mysql_fetch_array($res2)){ ?>
<option value="<?php echo $fila['ciudad']; ?>"><?php echo $fila['ciudad']; ?></option>
<?php } ?>
</select>
<input type="submit" name="bciudad" value ="Buscar">
<?php } ?>
<br><br>





       <div id="myDiv"></div><!--div donde aparecen los departamentos-->    



    <font color="#27A7D6">Buscar por Longitud y Latitud:<br></font>
    Longitud:<input type="text" name="long" size="21"><br>
    Latitud:&nbsp&nbsp&nbsp<input type="text" name="lat" size="21"><br>
    Radio:&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="radio" size="4">Metros. 
    <input type="submit" name="latitud" value ="Buscar"><br><br>

    <font color="#27A7D6">Buscar por Palabra clave:<br></font>
    <input type="text" name="pclave" size="19">
    <input type="submit" name="palabra" value ="Buscar"><br><br>
    </td>





        <td  width="70%" align="center" valign="top">Resultados:</td>
    </tr>
    </table>


<?php
    }
}
else
{
    echo 'No haz iniciado sesión, por favor inicia sesión aquí.';
}

include 'includes/final.php';
?>
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
Altrazar
  • 105
  • 2
  • 10

1 Answers1

1

Undefined index error means that the element you're trying to access, of an array, doesn't exist. The errors are on lines 11 and 13, so it is most likely the $_POST array from the code you've given.

They don't exist because your HTML is wrong so you don't actually have form elements with those names.

<select name "ciudad"> and <select name "departamento">
            ^                          ^ 

They are each missing an equals sign. The syntax should be

<select id="ciudad" name="ciudad" style="width:150px">
                        ^

and

<select id="departamento" name="departamento" style="width:150px">
                              ^
Popnoodles
  • 28,090
  • 2
  • 45
  • 53