two selections in a row

0

I have to do this exercise before Friday and it does not finish to me to leave, I will explain myself. I have in a database countries, cities and data of those cities, to which I connect well. I need to make two selections in html in a row, one from countries and one from cities. Until you select something from the first, nothing will appear in the second and when you select from it, a table with data from the third table will appear. Well, the first select works, but the second does not and I have already given it many laps, but it does not work out. This is the code that I have made:

<html>
    <head>
        <title>WORLD</title>
    </head>
    <body>
        <?php
            define("SERVIDOR","localhost");
            define("USUARIO","root");
            define("CLAVE","");
            //define("BD","world");
            $BD="world";
            $db;
            try{
                if($BD!='')
                    $db=new PDO("mysql:host=".SERVIDOR.";dbname=".$BD.";charset=utf8",USUARIO,CLAVE,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
                else
                    $db=new PDO("mysql:host=".SERVIDOR.";charset=utf8",USUARIO,CLAVE,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
                $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
                $db->setAttribute(PDO::NULL_TO_STRING,true);
                if($BD==''){
                    $sql=file_get_contents('world.sql');
                    $this->ejecuta_SQL($sql);
                }
            }catch(PDOException $e){
                die("<p><h3>No se ha podido establecer la conexión.
                    <p>Compruebe si está activado el servidor de bases de
                    datos MySQL.</h3></p>\n<p>Error: ".$e->getMessage()."</p>\n");
            }
            echo "<h1>EJERCICIO CONSULTA DE PAISES POR REGIONES</h1>
                <form name='form1' method='post' action=\"index.php\">";
            $sql1='SELECT Region FROM country';
            $resultado1=$db->query($sql1);
            echo "Selecciona Region: <select name='campo_busqueda1'>
                <option>Selecciona...</option>";
            while(($fila1=$resultado1->fetch(PDO::FETCH_ASSOC))!=NULL){
                echo '<option value="'.$fila1["Region"].'">'.$fila1["Region"].'</option>';
            };
            $region=$_POST["campo_busqueda1"];
            echo "</select>";
            echo "<input type='submit' value='Enviar'>
            <br>";echo $region;
            $sql2='SELECT Name FROM country WHERE Region="$region"';
            $resultado2=$db->query($sql2);
            echo "Selecciona Pais: <select name='campo_busqueda2'>
                <option>Selecciona...</option>";
            while(($fila2=$resultado2->fetch(PDO::FETCH_ASSOC))!=NULL){
                echo '<option value="'.$fila2["Name"].'">'.$fila2["Name"].'</option>';
            };
            echo "</select>
                <input type='submit' value='Enviar'><br>
            </form>";
        ?>
        <table border=1>
            <tr>
                <th>Nombre</th>
                <th>Poblacion</th>
            </tr>
                <?php
                    /*foreach(){
                        <tr><td></td><td></td></tr>
                    }*/
                ?>
        </table>
    </body>
</html>
    
asked by Charly Utrilla 20.12.2018 в 12:55
source

1 answer

2

you can do it in different ways, first you must define the countries as you already have it:

index.php

<html>
    <head>
        <title>WORLD</title>
    </head>
    <body>
        <?php

            define("SERVIDOR","localhost");
            define("USUARIO","root");
            define("CLAVE","toor");
            //define("BD","world");
            $BD="prueba";
            $db;
            try{
                if($BD!='')
                    $db=new PDO("mysql:host=".SERVIDOR.";dbname=".$BD.";charset=utf8",USUARIO,CLAVE,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
                else
                    $db=new PDO("mysql:host=".SERVIDOR.";charset=utf8",USUARIO,CLAVE,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
                $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
                $db->setAttribute(PDO::NULL_TO_STRING,true);
                if($BD==''){
                    $sql=file_get_contents('world.sql');
                    $this->ejecuta_SQL($sql);
                }
            }catch(PDOException $e){
                die("<p><h3>No se ha podido establecer la conexión.
                    <p>Compruebe si está activado el servidor de bases de
                    datos MySQL.</h3></p>\n<p>Error: ".$e->getMessage()."</p>\n");
            }
        $sql1='SELECT id, nombre FROM paises';
            $resultado1=$db->query($sql1);
    ?>
        <h1>EJERCICIO CONSULTA DE PAISES POR REGIONES</h1>
                <form name='form1' method='post' action='ciudades.php'>

           Selecciona Region: <select name='campo_busqueda1' id="campo_busqueda1">
                <option>Selecciona...</option>
    <?php
            while(($fila1=$resultado1->fetch(PDO::FETCH_ASSOC))!=NULL){
           echo '<option value="'.$fila1["id"].'">'.$fila1["nombre"].'</option>';
            };
    ?>
            </select>
            <input type='submit' value='Enviar'>
            </form>

    </body>
</html>

said file only shows a form with a select with the list of countries something like this:

once you press the send button the id of the option selected is sent to another php file where the cities are processed:

<html>
    <head>
        <title>WORLD</title>
    </head>
    <body>
        <?php
            define("SERVIDOR","localhost");
            define("USUARIO","root");
            define("CLAVE","toor");
            //define("BD","world");
            $BD="prueba";
            $db;
            try{
                if($BD!='')
                    $db=new PDO("mysql:host=".SERVIDOR.";dbname=".$BD.";charset=utf8",USUARIO,CLAVE,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
                else
                    $db=new PDO("mysql:host=".SERVIDOR.";charset=utf8",USUARIO,CLAVE,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
                $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
                $db->setAttribute(PDO::NULL_TO_STRING,true);
                if($BD==''){
                    $sql=file_get_contents('world.sql');
                    $this->ejecuta_SQL($sql);
                }
            }catch(PDOException $e){
                die("<p><h3>No se ha podido establecer la conexión.
                    <p>Compruebe si está activado el servidor de bases de
                    datos MySQL.</h3></p>\n<p>Error: ".$e->getMessage()."</p>\n");
            }
        $sql1='SELECT id, nombre FROM paises';
            $resultado1=$db->query($sql1);

        $sql2='SELECT id, nombre FROM ciudades WHERE pais_id ='.$_POST['campo_busqueda1'];
            $resultado2=$db->query($sql2);
    ?>
        <h1>EJERCICIO CONSULTA DE PAISES POR REGIONES</h1>
                <form name='form1' method='post' action=''>

           Selecciona Region: <select name='campo_busqueda1' id="campo_busqueda1">
                <option>Selecciona...</option>
    <?php
            while(($fila1=$resultado1->fetch(PDO::FETCH_ASSOC))!=NULL){
        if($_POST['campo_busqueda1'] == $fila1["id"]){
           echo '<option value="'.$fila1["id"].'" selected="selected" >'.$fila1["nombre"].'</option>';      
        }else{
         echo '<option value="'.$fila1["id"].'" >'.$fila1["nombre"].'</option>';
        }            
       };
    ?>
            </select><br>
    Selecciona Ciudad: <select name='campo_ciudad' id="campo_ciudad">
                <option>Selecciona...</option>
    <?php
            while(($fila1=$resultado2->fetch(PDO::FETCH_ASSOC))!=NULL){
         echo '<option value="'.$fila1["id"].'" >'.$fila1["nombre"].'</option>';
       };
    ?>
            </select>
            <input type='submit' value='Enviar'>
            </form>


    </body>
</html>

the result would be something like this:

To establish the selected of countries what I do is mediate an if determine if the id of the chosen country is equal to the id of the list of existing countries and there I define the selected one, you must validate more things like choosing an option ie a required and but it's a basic example.

I hope I will guide you .. !!

    
answered by 20.12.2018 / 16:35
source