lmcm2008 Posted May 3, 2011 Share Posted May 3, 2011 Hi. I´m learning PHP and trying to develope a Form using arrays. In this case I want to show 2 lists from an arrays for themselves, but I´m having problems, and don´t know how to solve the error that I receive. The error is something related with undefined variables or something like that. Please, can you help me??? Thanks, My code is the following: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>inicio</title> </head> <?php // Provincias de España $provincias = array("A Coruña", "Madrid","A Coruña", "Álava", "Albacete", "Alicante", "Almeria", "Asturias", "Ávila", "Badajoz", "Baleares", "Barcelona", "Burgos", "Cáceres", "Cádiz", "Cantabria", "Castellón", "Ceuta", "Ciudad Real", "Córdoba", "Cuenca", "Girona", "Granada", "Guadalajara", "Guipúzcoa", "Gran Canaria", "Huelva", "Huesca", "Jaén", "La Rioja", "Las Palmas", "León", "Lleida", "Lugo", "Madrid", "Málaga", "Melilla", "Murcia", "Navarra", "Ourense", "Palencia", "Pontevedra", "Salamanca", "Tenerife", "Segovia", "Sevilla", "Soria", "Tarragona", "Teruel", "Toledo", "Valencia", "Valladolid", "Vizcaya", "Zamora", "Zaragoza" ); // localidades de Madrid $madrid = array ( "Alcalá de Henares", "Alcobendas", "Alcorcón", "Aldea del Fresno", "Alpedrete", "Aranjuez", "Arganda del Rey", "Arroyomolinos", "Alcalá de Henares", "Alcobendas", "Alcorcón", "Aldea del Fresno", "Alpedrete", "Aranjuez", "Arganda del Rey", "Arroyomolinos", "Batres", "El Berrueco", "Boadilla del Monte", "El Boalo", "Buitrago del Lozoya", "La Cabrera", "Cadalso de los Vidrios", "Cenicientos", "Chinchón", "Collado Mediano", "Collado Villalba", "Colmenar de Oreja", "Colmenar Viejo", "Coslada", "El Escorial", "Fresnedillas de la Oliva", "Fuenlabrada", "Fuente el Saz de Jarama", "Fuentidueña de Tajo", "Galapagar", "Getafe", "Guadarrama", "Humanes de Madrid", "Leganés", "Loeches", "Madrid", "Majadahonda", "Manzanares el Real", "Mejorada del Campo", "Móstoles", "Navacerrada", "Navalagamella", "Navalcarnero", "Nuevo Baztán", "Parla", "Patones", "Pinto", "Pozuelo de Alarcón", "Puebla de la Sierra", "Puentes Viejas", "Rascafría", "Robledo de Chavela", "Las Rozas de Madrid", "San Fernando de Henares", "San Lorenzo de El Escorial", "San Martín de la Vega", "San Martín de Valdeiglesias", "San Sebastián de los Reyes", "Santa María de la Alameda", "Santorcaz", "Talamanca de Jarama", "Torrejón de Ardoz", "Torrejón de Velasco", "Torrelaguna", "Torrelodones", "Valdemaqueda", "Valdemorillo", "Valdemoro", "Villaconejos", "Villanueva de la Cañada", "Villar del Olmo", "Villarejo de Salvanés" ); ?> <body bgcolor="#FFFFFF"> <center> <form METHOD="POST" action=""> <table border="1"> <tr> <td>PROVINCIA: <?php $cuenta = array_count_values ($provincias); $i = 0; reset($provincias); while($i <= $cuenta){ echo "<option value='".$provincias[$i]."'>".$provincias[$i]."</option>"; $i++; } ?></td> <td>LOCALIDAD: <?php $cuenta2 = array_count_values ($madrid); $ix = 0; reset($madrid); while($ix <= $cuenta2){ echo "<option value='".$madrid[$ix]."'>".$madrid[$ix]."</option>"; $ix++; } ?></td> </tr> </table> <br> <br> <input type="submit" value="enviar"> </center> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/235409-help-arrays-don%C2%B4t-know-how-to-solve/ Share on other sites More sharing options...
spiderwell Posted May 3, 2011 Share Posted May 3, 2011 it is because you have not defined your keys 0,1,2,3,4 and the $i represents that key. use this too loop through an array instead. foreach ($provincias as $value) { echo $value; } Quote Link to comment https://forums.phpfreaks.com/topic/235409-help-arrays-don%C2%B4t-know-how-to-solve/#findComment-1209813 Share on other sites More sharing options...
sasa Posted May 3, 2011 Share Posted May 3, 2011 change while($i <= $cuenta){ to while($i < $cuenta){ and some in 2n while loop Quote Link to comment https://forums.phpfreaks.com/topic/235409-help-arrays-don%C2%B4t-know-how-to-solve/#findComment-1209818 Share on other sites More sharing options...
lmcm2008 Posted May 3, 2011 Author Share Posted May 3, 2011 Hi. Now the code is looking good and work fine... <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>inicio</title> </head> <?php // Provincias de España $provincias = array("A Coruña", "Madrid","A Coruña", "Álava", "Albacete", "Alicante", "Almeria", "Asturias", "Ávila", "Badajoz", "Baleares", "Barcelona", "Burgos", "Cáceres", "Cádiz", "Cantabria", "Castellón", "Ceuta", "Ciudad Real", "Córdoba", "Cuenca", "Girona", "Granada", "Guadalajara", "Guipúzcoa", "Gran Canaria", "Huelva", "Huesca", "Jaén", "La Rioja", "Las Palmas", "León", "Lleida", "Lugo", "Madrid", "Málaga", "Melilla", "Murcia", "Navarra", "Ourense", "Palencia", "Pontevedra", "Salamanca", "Tenerife", "Segovia", "Sevilla", "Soria", "Tarragona", "Teruel", "Toledo", "Valencia", "Valladolid", "Vizcaya", "Zamora", "Zaragoza" ); // localidades de Madrid $madrid = array ( "Alcalá de Henares", "Alcobendas", "Alcorcón", "Aldea del Fresno", "Alpedrete", "Aranjuez", "Arganda del Rey", "Arroyomolinos", "Alcalá de Henares", "Alcobendas", "Alcorcón", "Aldea del Fresno", "Alpedrete", "Aranjuez", "Arganda del Rey", "Arroyomolinos", "Batres", "El Berrueco", "Boadilla del Monte", "El Boalo", "Buitrago del Lozoya", "La Cabrera", "Cadalso de los Vidrios", "Cenicientos", "Chinchón", "Collado Mediano", "Collado Villalba", "Colmenar de Oreja", "Colmenar Viejo", "Coslada", "El Escorial", "Fresnedillas de la Oliva", "Fuenlabrada", "Fuente el Saz de Jarama", "Fuentidueña de Tajo", "Galapagar", "Getafe", "Guadarrama", "Humanes de Madrid", "Leganés", "Loeches", "Madrid", "Majadahonda", "Manzanares el Real", "Mejorada del Campo", "Móstoles", "Navacerrada", "Navalagamella", "Navalcarnero", "Nuevo Baztán", "Parla", "Patones", "Pinto", "Pozuelo de Alarcón", "Puebla de la Sierra", "Puentes Viejas", "Rascafría", "Robledo de Chavela", "Las Rozas de Madrid", "San Fernando de Henares", "San Lorenzo de El Escorial", "San Martín de la Vega", "San Martín de Valdeiglesias", "San Sebastián de los Reyes", "Santa María de la Alameda", "Santorcaz", "Talamanca de Jarama", "Torrejón de Ardoz", "Torrejón de Velasco", "Torrelaguna", "Torrelodones", "Valdemaqueda", "Valdemorillo", "Valdemoro", "Villaconejos", "Villanueva de la Cañada", "Villar del Olmo", "Villarejo de Salvanés" ); ?> <body bgcolor="#FFFFFF"> <center> <form METHOD="POST" action="busca.php" name=frminsertar> <table border="1"> <tr> <td>PROVINCIA:<br> <select name=provincia> <?php $cuenta = array_count_values ($provincias); $i = 0; reset($provincias); foreach ($provincias as $value=>$prov) { echo "<option value='".$provincias[$i]."'>".$provincias[$i]."</option>"; $i++; } ?> </td> <td>LOCALIDAD: <br> <select name=localidad> <?php $cuenta2 = array_count_values ($madrid); $ix = 0; reset($madrid); foreach ($madrid as $value=>$loc ) { echo "<option value='".$madrid[$ix]."'>".$madrid[$ix]."</option>"; $ix++; } ?> </select> </td> </tr> </table> <br> <br> <input name="submit" type="submit" value="enviar"> </center> </form> </body> </html> And Now, Im trying to receive the results of the form in another php file, BUT, I don´t receive anything. I don´t know how to post the results to use them in another file... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Documento sin título</title> </head> <body> <?php $laprov=$_POST["$provincias"]; echo "valores recibidos: $laprov <br>"; ?> </body> </html> Please, can you help me to see what I´m receiving from the first form????? Thanks a lot for your help. Quote Link to comment https://forums.phpfreaks.com/topic/235409-help-arrays-don%C2%B4t-know-how-to-solve/#findComment-1209886 Share on other sites More sharing options...
sasa Posted May 3, 2011 Share Posted May 3, 2011 change $laprov=$_POST["$provincias"]; to $laprov=$_POST["provincias"]; Quote Link to comment https://forums.phpfreaks.com/topic/235409-help-arrays-don%C2%B4t-know-how-to-solve/#findComment-1209922 Share on other sites More sharing options...
lmcm2008 Posted May 4, 2011 Author Share Posted May 4, 2011 Hi, not, it doesn´t work. I receive: Notice: Use of undefined constant provincias - assumed 'provincias' in C:\wamp\www\busca.php on line 13, and Notice: Undefined index: provincias in C:\wamp\www\busca.php on line 13. the file busca.php is the following: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Documento sin título</title> </head> <body> <?php echo $_POST[provincias]; ?> </body> </html> thanks. regards, and thankyou for your help. I really apreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/235409-help-arrays-don%C2%B4t-know-how-to-solve/#findComment-1210293 Share on other sites More sharing options...
spiderwell Posted May 4, 2011 Share Posted May 4, 2011 try echo $_POST['provincias']; notice the extra ' ' in there Quote Link to comment https://forums.phpfreaks.com/topic/235409-help-arrays-don%C2%B4t-know-how-to-solve/#findComment-1210298 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.