PHP 文件输出 javascript 代码本身而不是运行


PHP file ouputs the javascript code itself and not running

我正在尝试创建一个编辑模式。如果我有这个的html代码,我写了这个javascript/jquery代码:

   <script type='text/javascript'>
 $(function() {
    <?php
    $q  =  $db->query("select * from tblUnit where unitStatus <> '2'");
      while($r = $q->fetch(PDO::FETCH_ASSOC)){ 
                                echo " <script type'text/javascript'> alert('1');</script>";
                                $unitID         = $r['unitID'];
                                $unitStatus     = $r['unitStatus'];
                                $unitNumber     = $r['unitNumber'];
                                $floorNumber    = $r['floorCode'];
                                $unitType       = $r['unitType'];   
    $t = $db->query("select floorLevel, floor_buildingID from tblFloors where floorLevel = '$floorNumber'");
            while( $u = $t->fetch(PDO::FETCH_ASSOC)){
                                $floorLevel     = $u['floorLevel'];
                                $floor_buildingID = $u['floor_buildingID'];
                              $w = $db->query("select unitTypeName from tblUnitType where unitTypeID = $unitType");     
                              while($x = $w->fetch(PDO::FETCH_ASSOC)){
                                  $unitTypeName   = $x['unitTypeName'];
    ?>
            $("#editModal<?php echo $unitID; ?>").click(function(){
              $("#editUnitNumber").val("<?php echo $unitNumber;?>");              
              $("#editUnitType").val("<?php   echo $unitType; ?>").material_select('update');              
              $("#editFloorNumber").val("<?php  echo $floorNumber; ?>");

            });
<?php }}}?>   
});

上面的代码用于从模态写入数据,但它输出以下内容:

$("#editModal5").click(function(){ $("#editUnitNumber").val("12002"); $("#editUnitType").val("4").material_select('update'); $("#editFloorNumber").val("12"); }); });

我该如何解决这个问题?这是什么原因造成的?

使用 json 将数据从 php 传递到 javascript,而不是将所有内容都回显到一个地方。这似乎有点矫枉过正,但它是可读的,从长远来看更有益。

下面的代码没有经过测试,但它应该让你对如何处理这些事情有一个大致的了解。我没有在第一个 while 循环中包含第二个和第三个查询。您可以将这些查询的结果嵌套在 $unit 数组中,并通过 javascript 中的其他循环访问相关数据。

此外,理想情况下,您不会只是在 php 之后立即回显解码的数组,更好的解决方案是在页脚中调用一个函数,该函数将生成一个包含 javascript 使用的所有数据的脚本标签。另一种方法是使用 AJAX 并仅在需要时获取 json 响应,然后将相同的 json 馈送到循环中。

<?php
    $q  =  $db->query("select * from tblUnit where unitStatus <> '2'");
    $units = [];
    while($r = $q->fetch(PDO::FETCH_ASSOC)){ 
        $unit = [
            'unitID' => $r['unitID'],
            'unitStatus' => $r['unitStatus'],
            'unitNumber'     => $r['unitNumber'],
            'floorNumber'    => $r['floorCode'],
            'unitType'       => $r['unitType'] 
        ];
        $units[] = $unit;
    }
    $units_json = json_encode($units);
?>
<script type='text/javascript'>
$(function() {
    var units = '<?php echo $units_json ?>';
    var units_array = JSON.parse(units);
    // do some validation here
    for (var i = 0; i < units_array.length; i++) {
        // do some validation here
        $("#editModal" + units_array[i].unitID).click(function(){
            $("#editUnitNumber").val(units_array[i].unitNumber);
            $("#editUnitType").val(units_array[i].unitType).material_select('update');              
            $("#editFloorNumber").val(units_array[i].floorNumber);
        });
    };
});
</script>