Jump to content

AJAJ / PHP Multiple Append Problem

Recommended Posts

I have a problem with the jQuery AJAX Append function. Everytime I append a comment to the <span>, not only does it append that single one, but also replicates every other comment dynamically generated by the PHP. So I end up with multiple repeated comments. However, the repeated comments aren't stored in the database, so whenever I refresh the page, it goes away. How can I prevent this without refreshing the page everytime?


PHP: To display all comments

<span id="commentHint"></span><?phpecho "<b>$comment_user said:</b><small>$comment_time</small><br>";            echo "$comment<br>";?>

AJAX: Comment On Submit

<script>       $(document).ready(function () {                var form = $('#form');                var submit = $('#submit');                form.on('submit', function (e) {                    // prevent default action                    e.preventDefault();                    // send ajax request                    $.ajax({                        url: 'commentpost.php',                        type: 'POST',                        cache: false,                        data: form.serialize(),                        beforeSend: function () {                            // change submit button value text and disabled it                            submit.val('Submitting...').attr('disabled', 'disabled');                        },                        success: function (data) {                            var item = data;                                $('#commentHint').append(item); //Appends it to div.                            form.trigger('reset');                            submit.val('Submit Comment').removeAttr('disabled');                        },                        error: function (e) {                            alert(e);                        }                    });                });            });</script>
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...