我正在尝试打印文本框和数据库中的下拉列表。我正在根据给定的输入打印文本框和下拉列表。
例如:假设我将输入设为4.为此,它将创建四个文本框和下拉列表。
但我在PHP中有下拉代码,所以现在我想使用JavaScript在循环中打印PHP下拉列表。我怎么能这样做?
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 +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname">';
target.innerHTML +=' '+'Language '+' ';
target.innerHTML += '<input type="text" name="timings">';
target.innerHTML +='</br>';
target.innerHTML +='</br>';
}
}
PHP下拉列表:
<?php
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = "SELECT language FROM languages;";
$sth = $dbh->prepare($sql);
$sth->execute();
echo "<select name='language' id='course'>";
echo "<option>----Select Language----</option>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['language'] ."'>" . $row['language']. "</option>";
}
echo "</select>";
?>
取代
target.innerHTML += '<input type="text" name="timings">';
我应该使用PHP下拉菜单而不是文本框。
答案 0 :(得分:1)
简单的方法,因为你有两个代码......
function create(param) {
'use strict';
var i, target = document.getElementById('screens');
target.innerHTML = '';
<?php
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = 'SELECT language FROM languages;';
$sth = $dbh->prepare($sql);
$sth->execute();
$combo = "<select name='language' id='course'>";
$combo .= "<option>----Select Language----</option>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$combo .= "<option value='" . $row['language'] ."'>" . $row['language']. "</option>";
}
$combo .= "</select>";
?>
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname">';
target.innerHTML +=' '+'Language '+' ';
target.innerHTML += "<?php echo $combo; ?>";
target.innerHTML +='</br>';
target.innerHTML +='</br>';
}
}