gpt4 book ai didi

javascript - 如何在php上压缩html字符串?

转载 作者:行者123 更新时间:2023-12-02 17:14:41 25 4
gpt4 key购买 nike

如何在 PHP 上压缩字符串?例如,我有一个名为 form_dropdown() 的 PHP 函数,它返回:

<select name="selecOP" id="selecOP">
<option value=" ">Select selecOP</option>
<option value="1">test1</option>
<option value="2">test2</option>
<option value="2">test2</option>
</select>

然后我将 php 函数调用到 JavaScript:

$("#addrow").on("click", function () {

counter = $('#myTable tr').length - 2;

var newRow = $("<tr>");
var cols = "";

cols += '<td><?php echo form_dropdown("selecOP' + counter + '"); ?></td>';
cols += '<td><textarea name="price' + counter + '" rows="1px" cols="50px"></textarea></td>';

cols += '<td><button class="ibtnDel btn btn-xs btn-danger"></i> Delete</button></td>';
cols += ' </td>';
newRow.append(cols);
if (counter == 4) $('#addrow').attr('disabled', true).prop('value', "You've reached the limit");
$("table.order-list").append(newRow);
counter++;
});

我的问题是浏览器返回错误 Uncaught SyntaxError: Unexpected token ILLEGAL 。我用直接格式手动尝试过,它可以工作,但是用 block 格式它给我一个错误。

预期输出:

<select name="selecOP' + counter + '" id="selecOP"><option value="1">test1</option><option value="2">test2</option></select>

最佳答案

在你的 JavaScript 函数中,你有这个函数调用:

$("#addrow").on("click", function () {

在该 JavaScript 函数中,您有以下代码片段:

cols += '<td><?php echo form_dropdown("selecOP' + counter + '"); ?></td>';

这是完全错误的。 JavaScript 是一种基于浏览器的语言。 PHP 是基于服务器的;它是一个超文本预处理器。正如所解释的in the official documentation :

Server-side scripting. This is the most traditional and main target field for PHP. You need three things to make this work. The PHP parser (CGI or server module), a web server and a web browser. You need to run the web server, with a connected PHP installation. You can access the PHP program output with a web browser, viewing the PHP page through the server.

这意味着 PHP 必须存储在 Web 服务器上才能工作,然后当请求该页面时,Apache 通过 PHP 组件运行 PHP 脚本来执行某些操作。要么将内容输出到网络浏览器,要么在服务器端执行其他操作。

当您在 JavaScript 中执行操作时,所发生的只是将原始的、未处理的 PHP 代码放入客户端浏览器窗口中。它什么也不做。

因此,您需要重构代码,或许可以使用 AJAX 调用来允许 JavaScript 与服务器通信并运行 PHP 代码。但按原样,该代码由于任何原因都无法按照您所描述的方式工作。它需要重构。

关于javascript - 如何在php上压缩html字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24543872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com