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.

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

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗