I have to fill a dropdown select with values from a database, and then i have to check if the value is correct with a javascript function which i have already done.
I have two columns, one with id identifiers and the other one with names. The selection "comune" has to return to javascript function the IDs but the names have to be printed in the selection. This is the code I've written so far.
<?php ?>
<html>
<head>
</head>
<body>
<?php
require "connessione.php";
ini_set('display_errors', 1);
?>
<form name="myForm" action="javascript:funzione_visualizzazione();">
<select name="comune">
<?php
$sql = "SELECT * FROM table_3";
$query = mysqli_query($connessione,$sql) or die("MySQL error: " . mysqli_error($connessione) . "<hr>\nQuery: $query");
while($row=mysqli_fetch_array($query)){
$nome_comuni = $row["nome_comuni"];
$id_comuni = $row["id_comuni"];
?>
<option value="<?php echo $id_comuni?>"><?php echo $nome_comuni?></option>
<?php
}
?>
</select>
<select name="fabbisogno_totale">
<option value="5"> 5t </option>
<option value="15"> 15t </option>
<option value="20"> 20t </option>
<option value="25"> 25t </option>
<option value="30"> 30t </option>
</select>
<input name="fabbisogno_coperto" type="number" value=" " size="40" maxlength="25" />
<script language="javascript">
function funzione_visualizzazione(){
if(document.myForm.comune.value != null && document.myForm.fabbisogno_totale.value != null && document.myForm.fabbisogno_coperto.value != null){
//call the php page for elaborate datas
}
else
alert("inserire dati in tutti i campi");
}
</script>
</form>
</body>
</html>
The code is properly connected to the database because it prints "connected to mySQL".
I'm testing on xampp because the real database doesnt work right now.
UPDATE: i resolved the error i had before but now i'm getting notice errors
Undefined index: nome_comuni in <b>C:\xampp\htdocs\selezioni.php</b> on line <b>24</b><br />
on the line 24 i got $nome_comuni = $row["nome_comuni"];
and the same error occurs at the line 26
The drop down select has as many empty possible selection as there are on the database but it doesn't show anything. for example, if i have 7 rows in the database the dropdown selection shows 7 empty rows, how can i get the selections be showed?
this is my database "id_comuni" "id_province" "nome_comuni
One more question, if I leave the selection blank, does it return null or an empy string?
Sorry for my english but i'm italian, if there are grammatical errors please point them out.