php - Index page comments not inserting into database using AJAX -



php - Index page comments not inserting into database using AJAX -

i'm trying insert comment database maintain getting object object alert whenever trying submit , never makes database. files in same folder.

any help appreciated, in advance.

index.php

<?php require_once('include/header.php'); require_once('include/browser.php'); ?> <div class="content"> <section> <div class="article"> <?php $usernamequery = "select * (select * `tbl_posts` order 'post_id' desc) t order `post_id` desc limit 3"; $result = mysqli_query($connection, $usernamequery) or die("error in query: ". mysqli_error($connection)); while ($row = mysqli_fetch_assoc($result)){ $post_id = $row ['post_id']; ?> <div class="wrapper"> <div class="titlecontainer"> <h1><?php echo $row['post_title']; ?></h1> </div> <div class="textcontainer"> <?php echo $row['post_content']?> </div> <?php if (!empty($row['imagepath'])) //this check if there path in textfield { ?> <div class="imagecontainer"> <img src="<?php echo $row['imagepath']; ?>" alt="article image"> </div> <?php } ?> <div class="timestampcontainer"> <b>date posted :</b><?php echo $row['post_timestamp']; ?> <b>author :</b> admin </div> <?php //selecting comments correspond post $selectcommentquery = "select * `tbl_comments` left bring together `tbl_users` on tbl_comments.tbl_comments_users_id = tbl_users.id tbl_comments.tbl_comments_post_id ='$post_id'"; $commentresult = mysqli_query($connection,$selectcommentquery) or die ("error in query: ". mysqli_error($connection)); //showing comments echo '<div class="comment-block_' . $post_id .'">'; while ($commentrow = mysqli_fetch_assoc($commentresult)) { ?> <div class="commentcontainer"> <div class="commentusername"><h1>comment by: <?php echo $commentrow['username']?></h1></div> <div class="commentcontent"><?php echo $commentrow['comment_content']?></div> <div class="commenttimestamp"><?php echo $commentrow['comment_timestamp']?></div> </div> <?php } ?> </div> <?php if (!empty($_session['cleanusername']) ) { ?> <form method="post" class="post-form" action="index.php" > <label>new comment</label> <textarea name="comment" class="comment"></textarea> <input type="hidden" name="postid" value="<?php echo $post_id ?>"> <input type="submit" name ="submit" class="submitcomment"/> </form> <?php } echo "</div>"; echo "<br /> <br /><br />"; } require_once('include/footer.php'); ?>

comments.js

$(document).ready(function(){ $(document).on('click','.submitcomment',function(e) { e.preventdefault(); //send ajax request var form = $(this).closest('form'); var comment = $('.comment',form); if (comment.val().length > 1) { $.ajax({ url: 'postcomments.php', type: 'post', cache: false, datatype: 'json', data: $(form).serialize(), //form serialize info can come in database beforesend: function(){ //changing submit button value text , disabling $(this).val('submiting ....').attr('disabled', 'disabled'); }, success: function(data) { var item = $(data.html).hide().fadein(800); $('.comment-block_' + data.id).append(item); // reset form , button $(form).trigger('reset'); $(this).val('submit').removeattr('disabled'); }, error: function(e) { alert(e); } }); } else { alert("please fill field!"); } }); });

postcomments.php

<?php if (isset($_server['http_x_requested_with'])): session_start(); require_once('ajaxconnection.php'); $connection2 = connecttomysql(); $userid = $_session['userid']; $username = $_session['cleanusername']; $comment = $_post['comment']; $postid = $_post['post_id']; $date_format = " y-m-d g : : s"; $time = date ($date_format); $insertcommentquery = "insert `tbl_comments` (`comment_content`,`tbl_comments_users_id`,`tbl_comments_post_id`) values ('$comment', $userid, $postid)"; $result = mysqli_query($connection,$insertcommentquery); $obj = array(); $obj['id'] = $postid; $obj['html'] = '<div class="commentcontainer"> <div class="commentusername"><h1> username :'.$username.'</h1></div> <div class="commentcontent">'.$comment.'</div> <div class="commenttimestamp">'.$time.'</div> </div>'; echo json_encode($obj); connecttomysql(0); endif?>

ajaxconnection.php

<?php function connecttomysql() { $connection2 = mysqli_connect("localhost","root","","ascaniobajada2ed8s") or die('error connecting database'); homecoming $connection2; } ?>

jquery .ajax returns error (jqxhr, textstatus, errorthrown). utilize see real error , not object object

error: function (jqxhr, textstatus, errorthrown) { alert(textstatus); }

php jquery ajax mysqli

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -