Bootstrap4 DataTables行与按钮跳转冲突及PHP语法错误解决
解决方案
1. 阻止按钮点击触发行跳转
点击按钮时事件会向上冒泡到<tr>元素,导致同时执行行跳转和表单提交。只需在按钮的点击事件中阻止事件冒泡,即可只触发表单提交逻辑:
修改后的按钮代码:
<td> <form method="get" action="findLocation.php"> <input type="hidden" name="LongitudeValue" value="<?php echo $event->origin && $event->origin->longitude ? $event->origin->longitude->value : null; ?>" /> <input type="submit" value="FIND LOCATION" style="margin: auto; width: 100%; border: 3px solid green; padding: 1px;" onclick="event.stopPropagation()" /> </form> </td>
注意:原代码中<form>未包裹在<td>内属于无效HTML结构,修正后将表单完整放入对应的单元格中,避免布局异常。
2. 修正PHP三元运算符输出HTML的语法错误
PHP三元运算符中,要输出的HTML必须作为字符串处理,不能直接嵌入标签。可以用单引号包裹HTML内容(内部双引号无需转义),同时给条件表达式加括号避免优先级问题:
修正后的三元运算符代码:
<td> <?php echo ($event->origin && $event->origin->quality) ? '<input onclick="event.stopPropagation()" type="submit" value="FIND LOCATION" style="margin: auto; width: 100%; border: 3px solid green; padding: 1px;" />' : null; ?> </td>
完整的表格行示例(结合两种修正):
<tr onclick="xdata(this)"> <td><?php echo $event->description ? $event->description->type : null; ?></td> <td><?php echo $event->description ? $event->description->text : null; ?></td> <td><?php echo $event->origin && $event->origin->time ? $event->origin->time->value : null; ?></td> <td> <?php echo ($event->origin && $event->origin->quality) ? '<form method="get" action="findLocation.php"> <input type="hidden" name="LongitudeValue" value="' . ($event->origin->longitude->value ?? null) . '" /> <input type="submit" value="FIND LOCATION" style="margin: auto; width: 100%; border: 3px solid green; padding: 1px;" onclick="event.stopPropagation()" /> </form>' : null; ?> </td> </tr>
注:使用??空合并运算符可简化PHP空值判断逻辑。
内容的提问来源于stack exchange,提问作者karna
相关产品推荐
相关产品推荐

