PDA

View Full Version : [PHP] salvare valore scelto dall'utente da menų a tendina (drop down list) popolato d


postgres
06-05-2012, 18:24
Salve ho trovato questo codice per crearea un menų a tendina per la visualizzazione di una data.


<?php

$html_output = ' <div id="date_select" >'."\n";
$html_output .= ' <label for="date_day"> </label>'."\n";

/*days*/
$html_output .= ' <select name="date_day" id="day_select">'."\n";
for ($day = 1; $day <= 31; $day++) {
$html_output .= ' <option>' . $day . '</option>'."\n";
}
$html_output .= ' </select>'."\n";

/*months*/
$html_output .= ' <select name="date_month" id="month_select" >'."\n";
$months = array("", "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
for ($month = 1; $month <= 12; $month++) {
$html_output .= ' <option value="' . $month . '">' . $months[$month] . '</option>'."\n";
}
$html_output .= ' </select>'."\n";

/*years*/
$html_output .= ' <select name="date_year" id="year_select">'."\n";
for ($year = 2012; $year <= (date("Y") ); $year++) {
$html_output .= ' <option>' . $year . '</option>'."\n";
}
$html_output .= ' </select>'."\n";

$html_output .= ' </div>'."\n";

?>

<form action="" method="post">

<?php
echo "Data:".date_dropdown();
#echo date_dropdown();



?>
<input type="submit" name="submit" value="Ricerca"/>
</form>

Ora vorrei capire, per quanto possa sembrare banale, come fare a memorizzare la scelta dell'utente in una variabile!

se stampo month esce 13
i $_POST non servono a niente.
Come va preso il valore selezionato?

Mettiu_
06-05-2012, 18:55
Ciao, forse stai facendo un pō di confusione... Intanto il codice da te postato andrebbe un pō sistemato. Comunque il dato scelto dall'utente ti č disponibile quando esso preme il tasto Ricerca. Quando lo preme i dati vengono inviati alla pagina che sta nell'action della form: lė avrai disponibile il dato, NON nella pagina dove c'č la form ovviamente. Per fare un esempio col tuo codice, fai una pagina index.php col seguente codice (adattato):
<html>
<head>
<title>Inserisci la data</title>
<head>
<body>
<?php

$html_output = ' <div id="date_select" >'."\n";
$html_output .= ' <label for="date_day"> </label>'."\n";

/*days*/
$html_output .= ' <select name="date_day" id="day_select">'."\n";
for ($day = 1; $day <= 31; $day++) {
$html_output .= ' <option>' . $day . '</option>'."\n";
}
$html_output .= ' </select>'."\n";

/*months*/
$html_output .= ' <select name="date_month" id="month_select" >'."\n";
$months = array("", "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
for ($month = 1; $month <= 12; $month++) {
$html_output .= ' <option value="' . $month . '">' . $months[$month] . '</option>'."\n";
}
$html_output .= ' </select>'."\n";

/*years*/
$html_output .= ' <select name="date_year" id="year_select">'."\n";
for ($year = 2012; $year <= (date("Y") ); $year++) {
$html_output .= ' <option>' . $year . '</option>'."\n";
}
$html_output .= ' </select>'."\n";

$html_output .= ' </div>'."\n";
?>

<form action="ricerca.php" method="post">
<?
echo $html_output;
?>
<input type="submit" value="Ricerca"/>
</form>
</body>
</html>

Poi predisponi una pagina col nome 'ricerca.php' (l'ho chiamata cosė per fare un esempio ma se lo cambi devi cambiare anche l'action) e scrivi questo codice:

<html>
<head>
<title>Inserisci la data</title>
<head>
<body>
<?
if(isset($_POST['date_day']) && isset($_POST['date_month']) && isset($_POST['date_year']))
{
echo "Hai scelto questa data: ".$_POST['date_day']. "-".$_POST['date_month']."-".$_POST['date_year'];
}
else
{
echo "Non hai inserito correttamente la data";
}
?>
</body>
</html>

Alla pressione del tasto Ricerca non fa altro che stampare la scelta dell'utente, memorizzata all'interno dell'array $_POST. Spero di essermi spiegato chiaramente. Ciao :)

postgres
06-05-2012, 19:13
ah ok stupidaggine!
nel POST la variabile da mettere e la select name mentre io mettevo l'id"


<select name="date_day" id="day_select">'."\n";

E poi nella parte di form non mettevo la pagina di destinazione!
<form action="ricerca.php" method="post">

Grande! Grazie!