dongyuan1984 2014-07-03 02:07
浏览 29
已采纳

如何压缩PHP上的HTML字符串?

How to compress string on PHP? For instance I have PHP function called form_dropdown() which return into:

<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>

Then I call the php function to 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++;
    });

My problem is the browsers returns an error Uncaught SyntaxError: Unexpected token ILLEGAL . I tried it manually with straight format and it works but with block format it give me an error.

Expected output:

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

1条回答 默认 最新

  • dourang6858 2014-07-03 02:20
    关注

    In your JavaScript function you have this function call:

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

    And within that JavaScript function you have this code snipet:

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

    That is completely wrong. JavaScript is a browser-based language. PHP is server based; it’s a hypertext pre-processor. As explained 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.

    Meaning that for PHP to work it must be stored on a web server, and then when the page is requested Apache—via the PHP component—runs the PHP script which does something. Either outputs content to a web browser or does something else on the server side of things.

    When you do what you are doing in your JavaScript all that happens is you place raw, unprocessed PHP code in the client browser window. It does nothing.

    So you need to refactor your code to perhaps use AJAX calls to allow JavaScript to communicate to the server & run the PHP code. But as-is, this code will not work as you describe for any reason. It needs refactoring.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)