I need to do this exercise in a single file and only with php. I have two combobox with two buttons, one behind each combo. The first combo loads well from a mysql table. When the first button is clicked, the second combo is loaded with a query that is created with the choice of the first one and when the second button is clicked, a table with data from the database appears. The problem is that it does not save the variables that I select in the combos when I hit the buttons. This is the code that I have done so far.
<html>
<head>
<title>WORLD</title>
</head>
<body>
<?php
define("SERVIDOR","localhost");
define("USUARIO","root");
define("CLAVE","");
$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");
}
$region="";
$name="";
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'];
//$region=isset($_POST["campo_busqueda1"]) ? $_POST["campo_busqueda1"] : "";
echo "</select>";
echo "<input type='submit' value='Enviar'><br>";
$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>';
};
$name=$_POST['campo_busqueda2'];
//$name=isset($_POST["campo_busqueda2"]) ? $_POST["campo_busqueda2"] : "";
echo "</select>";
echo "<input type='submit' value='Enviar'><br>
</form>";
?>
<table border=1>
<tr>
<th>Nombre</th>
<th>Poblacion</th>
</tr>
<?php
$sql3='SELECT city.Name,city.Population FROM city as city INNER JOIN country AS country ON city.CountryCode=country.Code
WHERE (country.Region="'.$region.'") AND (country.Name="'.$name.'")';
$resultado3=$db->query($sql3);
echo $region;
echo $name;
echo $resultado3;
while($row=$resultado3->fetch(PDO::FETCH_ASSOC)){
echo $row;
print_r($row);
echo '<tr><td>'.$row[0].'</td><td>'.$row[1].'</td></tr>';
}
?>
</table>
</body>
</html>