dqbr37828 2016-08-08 16:25
浏览 43

在PHP代码中用Javascript编写HTML代码

I need to write in PHP, inside a Javascript portion code, an hidden input tag with an javascript array that I need to pass to another PHP code ..

This is the sample code ...

    echo '<script type="text/javascript">';

    <other javascript code .... >

    echo 'arr_selections_json = JSON.stringify(arr_selections);';
    echo 'document.write("<input type="hidden" name="arr_selections_json" value="+arr_selections_json+" />")';

This code doesn't work .... Any suggestions? Thank you in advance .. .

  • 写回答

2条回答 默认 最新

  • dongwang6837 2016-08-08 16:27
    关注

    You need to JS-escape the double quotes inside the argument of document.write() e.g.

    echo 'document.write("<input type=\\"hidden\\" name=\\"arr_selections_json" value=\\"" + arr_selections_json + "\\" />")';
    

    Also, I've had recent weird cases, where document.write wouldn't consider the closing slash (/>), whereas it's HTML-5 compliant. I had to escape with a backslash the closing slash.

    评论

报告相同问题?