jQuery ajax data retrieve and append -
dead stackoverflow community,
i working on comment system project. use jquery ajax call data, using pdo in php database scripting. able store comments database, , when refresh page, can see comments listed out fine. thing can't "append" comment right after clikcing post comments, in console, stats
"uncaught error: syntax error, unrecognized expression: sqlstate[hy000]: general error <div class="comment"> <p class="name"><--the user has commented--></p><p class="comment"><--their comment--></p>
"
i know close, can plase me out? have been struggling 2 days now. following 3 pieces of codes:
index page form:
<form id="form" method="post' "class="comment"> <input type="hidden" name="post_id" value="<?php echo $post_id ?>" /> <input type="hidden" name="user_id" value="<?php echo $_session['user_id'] ?>" /> <textarea name="comment" class="newcomment" rows="6" placeholder="comment..." required></textarea> <input type="submit" class="csubmit" value="post comment" /> <div class="cancelbt">cancel</div> </form><div class="allcomments" />
ajax requrest:
$(document).ready(function(){ var form = $('form'); var submit = $('.csubmit'); form.on('submit', function(e) { e.preventdefault(); $.ajax({ url: 'ajax_comment.php', type: 'post', cache: false, data: form.serialize(), //form serizlize data beforesend: function(){ submit.val('submitting...').attr('disabled', 'disabled'); }, success: function(data) { var item = $(data).hide().fadein(800); $('.allcomments').append(item); form.trigger('reset'); submit.val('post comment').removeattr('disabled'); }, error: function(e){ alert(e); } }); }); });
and ajax file:
<?php if (isset( $_server['http_x_requested_with'] )): require_once ("newconfig.php"); if(!empty($_post['post_id']) , !empty($_post['user_id']) , !empty($_post['comment'])) { $artwork_id = ($_post['post_id']); $user_id = ($_post['user_id']); $comment = ($_post['comment']); $sql = "insert `comments` (`post_id`, `user_id`, `comment`) values (:pid, :uid, :cmt)"; $stmt = $db->prepare($sql); try { $stmt->bindvalue(":pid", $post_id); $stmt->bindvalue(":uid", $user_id); $stmt->bindvalue(":cmt", $comment); $stmt->execute(); $results = $stmt->fetchall(); } catch (exception $ex) { echo $ex->getmessage(); } } ?> <div class="comment"> <p class="name">posted by:<?php echo $user_id;?></p> <p class="comment"><?php echo $comment;?></p>
s
thank taking time read codes, think problem retrieving data off ajax file index file, why cant append data want, not sure how fix it. feedback appreciated!
i think problem in below line, data sent server sqlstate[hy000]: general error <div class="comment"> <p class="name"><--the user has commented--></p><p class="comment"><--their comment--></p>
var item = $(data).hide().fadein(800);
then creates invalid jquery selector, trying create elements , display them
var item = $('<div />', { html: data }).hide().appendto('.allcomments').fadein(800);
Comments
Post a Comment