dongse3348 2013-12-27 23:06
浏览 40
已采纳

无法将php变量作为参数传递给javascript函数[关闭]

The case is that I have a .php file and inside it, I have a function inside script tags. After it, I have php code, that reads from a file. I want to sent the file data to the js function. I had done this before, but now it will produce parsing errors.

SOLUTION

THE file format must not have line breaks!!!!
echo '<script>updateMatch1("'.$filetext.'") </script>';

Here is the code

<script>
    function updateMatch1(names) {
    alert(names);
};
</script>
<?php
    /* Read file */
    ...
    $filetext = fread( $file, $filesize ); //checked the output, it's ok

    // numerous ways I tried. Some produce an error and the page isn't loaded at all,
        // while others produce an error in the console
    echo "<script>updateMatch1('" . $filetext . "');</script>";
    //echo '<script> updateMatch1($filetext);</script>';
    //echo '<script>updateMatch1();</script>';
    //echo "<script>updateMatch1($filetext" . ")</script>";
    //echo "<script>updateMatch1($filetext)</script>";
        //echo '<script>updateMatch1(' . $filetext  . ');</script>';
?>
  • 写回答

4条回答 默认 最新

  • doutuo7815 2013-12-27 23:23
    关注

    Check your file.txt. Maybe it contains some illegal character or has some incompatible file encoding that produce the illegal character. If you print the value of $filetext with php, there is no visible error, but it can produce some in JS. E.g. it can be the zero-width space. See if you have spaces or other characters on the end of the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?