Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript throwing TypeError, document.getElementById when passed PHP variable
    primarykey
    data
    text
    <p>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. Then, it should POST to a separate PHP file. Here's the code I have so far:</p> <pre><code>function add_comment_url($table, $id) { $html = '&lt;div id="comment' . $id . '" name="comment_box" style="display: none"&gt; &lt;form action="cgi-bin/add_comment.php" method="post"&gt; &lt;textarea id="comment" name="comment"&gt;&lt;/textarea&gt; &lt;input type="hidden" name="id" value="' . $id . '"&gt; &lt;input type="hidden" name="table" value="' . $table . '"&gt; &lt;input type="submit" name="submit" value="Submit Comment"&gt; &lt;/form&gt;&lt;/div&gt; &lt;input type="button" value="Add Comment" onclick="showComment();"&gt; &lt;script&gt; var id= ' . json_encode($id) . '; showComment(id); &lt;/script&gt;'; return($html); } </code></pre> <p>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.</p> <p>I'm guessing I screwed up the JS variable assignment, but I'm at a loss as to how to fix it. Any thoughts?</p> <p>EDIT - Final code</p> <p>I figured out what I did wrong... I was defining the <code>var</code> when I didn't need to! Here's the new function, which works:</p> <pre><code>function add_comment_url($table, $id) { $html = '&lt;div id="comment' . $id . '" name="comment_box" style="display: none"&gt; &lt;form action="cgi-bin/add_comment.php" method="post"&gt; &lt;textarea id="comment" name="comment"&gt;&lt;/textarea&gt; &lt;input type="hidden" name="id" value="' . $id . '"&gt; &lt;input type="hidden" name="table" value="' . $table . '"&gt; &lt;input type="submit" name="submit" value="Submit Comment"&gt; &lt;/form&gt;&lt;/div&gt; &lt;input type="button" value="Add Comment" onclick="showComment(' . $id . ');"&gt;'; return($html); </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload