-1

我正在尝试从数据库中打印文本框和下拉列表。我正在根据给定的输入打印文本框和下拉菜单。

例如:假设我将输入设为 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 下拉列表而不是文本框。

4

1 回答 1

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>';
    }
}
于 2013-08-22T10:10:04.467 回答