简体   繁体   English

传递PHP变量时,JavaScript抛出TypeError,document.getElementById

[英]Javascript throwing TypeError, document.getElementById when passed PHP variable

I'm trying to write a function that will dynamically display a form (inside a div, of which there will be several instances, thus the weird "id" name) when you click a button. 我正在尝试编写一个函数,当您单击按钮时,该函数将动态显示表单(在div中,其中会有多个实例,因此会有奇怪的“ id”名称)。 Then, it should POST to a separate PHP file. 然后,它应该发布到单独的PHP文件。 Here's the code I have so far: 这是我到目前为止的代码:

function add_comment_url($table, $id) {
    $html = '<div id="comment' . $id . '" name="comment_box" style="display: none">
        <form action="cgi-bin/add_comment.php" method="post">
            <textarea id="comment" name="comment"></textarea>
            <input type="hidden" name="id" value="' . $id . '">
            <input type="hidden" name="table" value="' . $table . '">
            <input type="submit" name="submit" value="Submit Comment">
        </form></div>
        <input type="button" value="Add Comment" onclick="showComment();">
        <script>
    var id= ' . json_encode($id) . ';
    showComment(id);
    </script>';

    return($html);
}

The "Add Comment" button shows up fine, but I can't get the to display, and the Firefox console shows a "TypeError: div is null" error when I click on the button. “添加评论”按钮显示正常,但我无法显示,并且当我单击该按钮时,Firefox控制台显示“ TypeError:div is null”错误。

I'm guessing I screwed up the JS variable assignment, but I'm at a loss as to how to fix it. 我猜想我搞砸了JS变量分配,但是我对如何解决这个问题不知所措。 Any thoughts? 有什么想法吗?

EDIT - Final code 编辑-最终代码

I figured out what I did wrong... I was defining the var when I didn't need to! 我发现我做错了什么...我在不需要时定义了var Here's the new function, which works: 这是新功能,可以正常工作:

function add_comment_url($table, $id) {
$html = '<div id="comment' . $id . '" name="comment_box" style="display: none">
    <form action="cgi-bin/add_comment.php" method="post">
        <textarea id="comment" name="comment"></textarea>
        <input type="hidden" name="id" value="' . $id . '">
        <input type="hidden" name="table" value="' . $table . '">
        <input type="submit" name="submit" value="Submit Comment">
    </form></div>
    <input type="button" value="Add Comment" onclick="showComment(' . $id . ');">';

return($html);

} }

You can't use <?php inside PHP strings. 您不能在PHP字符串中使用<?php You should use string concatenation or interpolation: 您应该使用字符串串联或插值:

function add_comment_url($table, $id) {
    $html = '<div id="comment' . $id . '" name="comment_box" style="display: none">
        <form action="cgi-bin/add_comment.php" method="post">
            <textarea id="comment" name="comment"></textarea>
            <input type="hidden" name="id" value="' . $id . '">
            <input type="hidden" name="table" value="' . $table . '">
            <input type="submit" name="submit" value="Submit Comment">
        </form></div>
        <input type="button" value="Add Comment" onclick="showComment();">
        <script>
        function showComment() {
        var id= ' . json_encode($id) . ';
        div = document.getElementById(\'comment\' + id);
        div.style.display = "block";}</script>';
    return($html);
}

Are you repeating this function definition for every comment block? 您是否为每个注释块重复此函数定义? I would recommend just defining showComment() once, and make it take the commentID as an argument. 我建议只定义一次showComment() ,并使其以commentID作为参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM