0

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:

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.

I'm guessing I screwed up the JS variable assignment, but I'm at a loss as to how to fix it. Any thoughts?

EDIT - Final code

I figured out what I did wrong... I was defining the var when I didn't need to! 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);

}

2
  • 2
    If you had looked at the page source your browser receives, you would have noticed that var id= "<?php echo json_encode($id); ?>"; does not produce the expected output. Commented Sep 2, 2013 at 10:56
  • I did notice that, which is why I knew it was a variable issue. Thanks! Commented Sep 2, 2013 at 11:21

1 Answer 1

3

You can't use <?php inside PHP strings. 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.

Sign up to request clarification or add additional context in comments.

8 Comments

You don't need the double quotes in the JS code surrounding json_encode.
@DCoder Good point. And if $id is always a number, you don't really need json_encode.
@Barmar - Thanks, that cleared up the JS error! However, the <div> doesn't appear when the button is clicked....
Are there multiple copies of this code on the page? If so, the showComment function will only be the last one defined. You really should do what I suggested: define the function once, and pass the ID in the argument.
There shouldn't be any var id in the comment blocks now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.