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>
相关文章:
- 如何使用phaser使html5游戏在移动设备浏览器上运行
- 使用压缩的JavaScript文件(不是运行时压缩)
- Javascript运行php文件,然后下载文件
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- 我已经创建了一个jquery转盘,并使用if条件来运行和停止转盘
- Angularjs代码未在匿名函数中运行
- jquery设置为使用参数运行
- 如何根据时间运行不同的脚本
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 将文本框链接到由按钮运行的javascript公式
- Javascript键码在Firefox中不起作用,但在chrome中运行良好
- 在Phonegap(Java Android)中运行本机代码
- Chrome扩展本应在所有Facebook页面上运行,但只有当我点击刷新时才会运行
- array.forEach比本机迭代运行得更快?怎样
- 扩展本机函数以在函数调用时运行代码
- 检测ES2015代码是由babel编译的还是本机运行的
- 运行命令出错:构建脚本返回非零退出码;将Gatsby JS站点部署到netflix时
- 从浏览器运行本机java应用程序
- Angularjs应用程序运行在本地主机上,而不是在heroku上
- Google Web 工具包运行时错误:方法中缺少非本机或抽象方法中的“代码”属性