我想通過javascript添加動態下拉列表。如何動態添加下拉列表
我有一個下拉菜單,其中有數字,選中時會創建下拉菜單。 但我想通過javascript添加動態下拉菜單。 我該怎麼做?
這裏是PHP代碼
代碼:
<?php
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = "SELECT theater_name FROM theater;";
$sth = $dbh->prepare($sql);
$sth->execute();
echo "<select name='theater_name' id='course' onchange='showUser(this.value);'>";
echo "<option>----Select Theater----</option>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['theater_name'] ."'>" . $row['theater_name']. "</option>";
}
echo "</select>";
?>
這個PHP代碼獲取下拉值從MySQL數據庫。 但這個下拉將被動態地從JavaScript
JavaScript代碼
function create(param) {
'use strict';
var i, target = document.getElementById('screens');
target.innerHTML = '';
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='Movie in hall '+i+' ';
target.innerHTML += '<?php
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = "SELECT theater_name FROM theater;";
$sth = $dbh->prepare($sql);
$sth->execute();
echo "<select name='theater_name' id='course' onchange='showUser(this.value);'>";
echo "<option>----Select Theater----</option>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['theater_name'] ."'>" . $row['theater_name']. "</option>";
}
echo "</select>";
?>';
target.innerHTML +=' '+'Timings '+' ';
target.innerHTML += '<input type="text" name="timings">';
target.innerHTML +='</br>';
target.innerHTML +='</br>';
}
}
所以現在我已經加入此PHP代碼在JavaScript,但它也沒有創造下拉創建..
爲什麼?我該怎麼辦
是它加入「電影的殿堂」和「計時」在你的HTML? – Thundar
@ F.Haymard'Ettory在添加之前,但在添加php代碼之後,它並未添加這些代碼 –
JavaScript在瀏覽器上運行,PHP在服務器上運行,因此瀏覽器無法解釋PHP腳本。 –