Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript I';我正试图将数据从数据库拉入选择框_Javascript_Php - Fatal编程技术网

Javascript I';我正试图将数据从数据库拉入选择框

Javascript I';我正试图将数据从数据库拉入选择框,javascript,php,Javascript,Php,我试图将数据从数据库拉入选择框,但当数据被拉入一个“td”而不是单独的td时。我正在努力实现如下所示的结果 但我一直得到这个结果 这是我的密码 您需要将整个表行放入循环中。您还需要添加一个变量来计算行号 <?php $row =1; foreach($data_array as $rows2): ?> <tr> <td class="count"><?php echo $row; ?></td> <td

我试图将数据从数据库拉入选择框,但当数据被拉入一个“td”而不是单独的td时。我正在努力实现如下所示的结果


但我一直得到这个结果


这是我的密码



您需要将整个表行放入循环中。您还需要添加一个变量来计算行号

<?php 
$row =1;
foreach($data_array as $rows2):
?>
<tr>
    <td class="count"><?php echo $row; ?></td>
    <td>
        <?php    
             $fighterID = $rows2->FireFighterInfo_fighterID; 
            $results = mysql_query("SELECT `fighterID`, `firstName`, `middleName`, `lastName`, `stationlocation`.`exactlocation` 
                                    FROM `firefighterinfo` 
                                    JOIN `stationlocation` ON `firefighterinfo`.`StationLocation_locationID`=`stationlocation`.`locationID` ORDER BY `firstName`");               
            echo '<select name="fireman[]" required><option value=""></option>';
            while($row = mysql_fetch_array($results))
            {
                if($row['fighterID'] == $fighterID)
                    echo '<option selected>'. $row['firstName']." ".$row['middleName']." ".$row['lastName'].", ".$row['exactlocation'].'</option>';
                else
                    echo '<option>'. $row['firstName']." ".$row['middleName']." ".$row['lastName'].", ".$row['exactlocation'].'</option>';

            }// end while
            echo '</select><br>';

        ?> 
    </td>
    <td>
        <input type="button" value="X" class="removeVar"/>
    </td>
</tr>
<?php
    $row++;
    endforeach;
?>    


将PHP代码包含到for loop中。嗯,为什么要在JS中嵌入PHP代码?您知道Javascript在客户端执行,而PHP在服务器上执行吗?在加载页面时,JS代码将被硬编码到php代码的输出中,然后再也不会更改。这在某种程度上是可行的。我两次得到“1”,第一行没有选择框,然后当按下“添加项目”按钮时,编号跳到数字5。你建议我做什么?
<script type="text/javascript">
    $('form').on('click', '.removeVar', function(){
    $(this).closest('tr').remove();
    $('.count').each(function(i){
      $(this).text(i + 1);
    });
});

//add a new node
$('#addVar').on('click', function(){
    var varCount = $('#myTable tr').length - 1;
    $node = ['<tr>',
    '<td class="count">'+varCount+'</td>',
    '<td><select name="fireman[]" class="ctlGroup" required>',
      '<option value=""></option>',
      '<?php require("php/fireman_list.php"); ?>',
    '</select></td>',
    '<td><input type="button" value="X" class="removeVar"/>',
    '</td></tr>'].join('\n');
    $('#myTable > tbody:last').append($node);
});
</script>
<?php 
$row =1;
foreach($data_array as $rows2):
?>
<tr>
    <td class="count"><?php echo $row; ?></td>
    <td>
        <?php    
             $fighterID = $rows2->FireFighterInfo_fighterID; 
            $results = mysql_query("SELECT `fighterID`, `firstName`, `middleName`, `lastName`, `stationlocation`.`exactlocation` 
                                    FROM `firefighterinfo` 
                                    JOIN `stationlocation` ON `firefighterinfo`.`StationLocation_locationID`=`stationlocation`.`locationID` ORDER BY `firstName`");               
            echo '<select name="fireman[]" required><option value=""></option>';
            while($row = mysql_fetch_array($results))
            {
                if($row['fighterID'] == $fighterID)
                    echo '<option selected>'. $row['firstName']." ".$row['middleName']." ".$row['lastName'].", ".$row['exactlocation'].'</option>';
                else
                    echo '<option>'. $row['firstName']." ".$row['middleName']." ".$row['lastName'].", ".$row['exactlocation'].'</option>';

            }// end while
            echo '</select><br>';

        ?> 
    </td>
    <td>
        <input type="button" value="X" class="removeVar"/>
    </td>
</tr>
<?php
    $row++;
    endforeach;
?>