我有一个名为update.php的页面,在我的更新.php中我有一个省的下拉列表,我想要发生的是我想在下拉列表中显示省的值,我也可以选择不同的省份,如果我想要更新..
这里是下拉列表的代码..
<tr>
<td>Province</td>
<td>
<select name="Province" class="form-control" value="<?php echo $province; ?>" id="province" onchange="populate(this.id,'municipality')">
<option id="">Province</option>
<option value="Albay">Albay</option>
<option value="Camarines Norte">Camarines Norte</option>
<option value="Camarines Sur1">Camarines Sur 1</option>
<option value="Camarines Sur2">Camarines Sur 2</option>
<option value="Catanduanes">Catanduanes</option>
<option value="Masbate">Masbate</option>
<option value="Sorsogon">Sorsogon</option>
</select>
</td>
javacript代码
function populate(s1,s2){
var s1 = document.getElementById(s1);
var s2 = document.getElementById(s2);
var optionArray;
s2.innerHTML = "";
if(s1.value == "Albay"){
optionArray = ["|","bacacay|Bacacay","camalig|Camalig","daraga|Daraga","guinobatan|Guinobatan","jovellar|Jovellar","legazpi|Legazpi","libon|Libon","ligao|Ligao","malilipot|Malilipot","malinao|Malinao","manito|Manito","oas|Oas","pioduran|Pioduran","polangui|Polangui","rapu-rapu|Rapu-Rapu","sto. domingo|Sto. Domingo","tabaco|Tabaco","tiwi|Tiwi"];
}
else if(s1.value == "Camarines Norte"){
optionArray = ["|","basud|Basud","capalonga|Capalonga","daet|Daet","imelda|Imelda","jose panganiban|Jose Panganiban","labo mercedes|Labo Mercedes","paracale|Paracale","san vicente|San Vicente","sta. elena|Sta. Elena","talisay|Talisay","vinzons|Vinzons","capalonga|Capalonga"];
}
else if(s1.value == "Camarines Sur1"){
optionArray = ["|","baao|Baao","balatan|Balatan","bato|Bato","buhi|Buhi","bula|Bula","cabusao|Cabusao","del gallego|Del Gallego","gainza|Gainza","iriga city|Iriga City","libmanan|Libmanan","lupi|Lupi","milaor|Milaor","minalabac|Minalabac","nabua|Nabua","pamplona|Pamplona","pasacao|Pasacao","ragay|Ragay","san fernando|San Fernando","sipocot|Sipocot"];
}
else if(s1.value == "Camarines Sur2"){
optionArray = ["|","bombon|Bombon","calabanga|Calabanga","camaligan|Camaligan","canaman|Canaman","caramoan|Caramoan","garchitorena|Garchitorena","goa|Goa","lagonoy|Lagonoy","magarao|Magarao","naga city|Naga City","ocampo|Ocampo","pili|Pili","presentacion|Presentacion","sagnay|Sagnay","san jose|San Jose","siruma|Siruma","tigaon|Tigaon","tinambac|Tinambac"];
}
else if(s1.value == "Catanduanes"){
optionArray = ["|","bagamanoc|Bagamanoc","baras|Baras","bato|Bato","caramoran|Caramoran","gigmoto|Gigmoto","pandan|Pandan","panganiban|Panganiban","san andres|San Andres","san miguel|San Miguel","viga|Viga","virac|Virac"];
}
else if(s1.value == "Masbate"){
optionArray = ["|","aroroy|Aroroy","baleno|Baleno","balud|Balud","batuan|Batuan","cataingan|Cataingan","cawayan|Cawayan","claveria|Claveria","dimasalang|Dimasalang","esperanza|Esperanza","mandaon|Mandaon","masbate|Masbate","milagros east|Milagros East","milagros west|Milagros West","mobo|Mobo","monreal|Monreal","palanas|Palanas","pio v. corpuz|Pio V. Corpuz","placer|Placer","san fernando|San Fernando","san jacinto|San Jacinto","san pascual|San Pascual","sipalay|Sipalay","uson north|Uson North","uson south|Uson South"];
}
else if(s1.value == "Sorsogon"){
optionArray = ["|","bacon|Bacon","barcelona|Barcelona","bulan|Bulan","casiguran|Casiguran","castilla|Castilla","donsol|Donsol","gubat|Gubat","irosin|Irosin","juban|Juban","magallanes|Magallanes","matnog|Matnog","pilar|Pilar","prieto diaz|Prieto Diaz","sorsogon|Sorsogon","sta. magdalena|Sta. Magdalena"];
}
for(var option in optionArray){
var pair = optionArray[option].split("|");
var newOption = document.createElement("option");
newOption.value = pair[0];
newOption.innerHTML = pair[1];
s2.options.add(newOption);
}
}
这是我尝试的内容
<tr>
<td>Land Type</td>
<?php
include_once 'dbconfig.php';
$sql = "SELECT Province FROM survey_section";
$stmt = $DB_con->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($stmt->rowCount() > 0)
{
?>
<td><select name="Province" class="form-control">
<option selected="selected" value="">---</option>
<?php
foreach ($results as $row)
{
?>
<option value="<?php echo $row['Province']; ?>"><?php echo $row['Province']; ?></option>
<?php
}
?>
</select>
<?php
}
?>
</td>
</tr>
答案 0 :(得分:0)
这将完成工作
使用html时请注意“和'的正确数量开始和结束
<select name="Province" class="form-control"
value="<?php echo $province; ?>" id="province" onchange="populate(this.id,'municipality')">
<?php
// start your php here
$select_data = mysqli_query($connect,"SELECT id_column,name_column from your_table_name LIMIT 10");
while($row = mysqli_fetch_assoc($select_data)){
?>
<option value="<?php echo $row['id_column'] ?>"><?php echo $row['name_column'] ?></option>
<?php
}
?>
</select>