javascript - Trouble with first comment of a list of comments via AJAX -
summary
i have page more less same structure find in twitter: post("tweet") feed , each post interal ordered list (ol) comments post (tweet) reside. using ajax submit post (from form on same page) , ajax submit comment specific (or other) post.
problem
when submit first comment post, page reloads (in case goes post_comment.php , presents result). after having first comment , submiting second comment, goes fine.
i'll present code below. please me out? may simple thing.
flow
- main_page.php - declaration scripts jquery, jquery form plugin, , post_comment are, , main structure presented.
- main_page post - post done via ajax (ajaxform - jquery form plugin) main_page.php. post has in form, contains 2/3 elements sent submit comment.
- post comment - submit done via post submitted. call post_comment.php done via ajaxform. described, first time (when comments ol empty) fails. after having first comment, new comments added fine without reloading page.
js:
$(function() { //var = $(this).closest('.form_wrapper-feed').next().find('.comments-feed').child("ol.post_comment_list"); var options = { //clearform: true, //resetform: true, //beforesubmit: showrequest, url: 'post_comment.php', success: function(html) { var arrhtml = html.split('|'); var postid = $.trim(arrhtml[0]); var html_code = arrhtml[1]; var target = 'ol#post_comment_list'+postid; console.log('codigo html'+html_code); console.log('aqui vai o post id'+postid); console.log('aqui vai o target'+target); $('ol#post_comment_list'+postid).append(html_code); //$('ol#post_comment_list'+postid 'li:first').slidedown('slow'); $('.footer-post').hide(); $('.comments-feed').delay(2000).slideup({duration: 1000, queue: true}); $('.small-textarea-main-feed').removeclass('set-large'); resetform($('.footer-comment')); }, error: function() { alert('error: unable upload files'); }, complete: function() { //$('form#post-question-form')[0].reset(); //resetform($('#post-question-form')); }, }; $(".footer-comment").ajaxform(options); function showrequest(formdata, jqform, options) { var querystring = $.param(formdata); alert('beforesend method: \n\nabout submit: \n\n' + querystring); return true; } function resetform($form) { $form.find('input:text, input:password, input:file, select, textarea').val(''); $form.find('input:radio, input:checkbox') .removeattr('checked').removeattr('selected'); } }); php (to post comment)
<?php include 'c:/xampp/htdocs/dh/core/init.php'; // form registration validation if (empty($_post) === false) { $required_fields = array('comment'); foreach($_post $key => $value) { if (empty($value) && in_array($key, $required_fields) === true) { $errors[] = 'all fields required'; break 1; } } if (empty($errors) === true) { // preg_match retunns int, has 2 == oppposed === if (strlen($_post['comment']) > 300) { $errors[] = 'your answer must less 300 characters'; } } } //print_r($errors); ?> <?php if (empty($_post) === false && empty($errors) === true) { //register user $post_comment = array( 'comment' => $_post['comment'], 'id' => $_post['id'], ); //echo $post_comment['id']; $user_id = $_session['user_id']; post_comment_db($user_id, $post_comment); //print_r($post_question['tags']); echo load_comment($user_id,$post_comment); //add_new_comment(); //load_top_questions(); } else{ echo output_errors($errors); } ?> php/html (...adding new comment following code - output of post_comment.php -this part computed in separeted users.php file)
function load_comment($user_id,$post_comment){ $comment = $post_comment['comment']; $username = mysql_result(mysql_query("select `username` `users` `user_id` = $user_id"), 0, 'username'); $comment_id = mysql_result(mysql_query("select `comment_id` `comments` `message` = '$comment'"), 0, 'comment_id'); $timestamp = mysql_result(mysql_query("select `timestamp` `comments` `comment_id` = $comment_id"), 0, 'timestamp'); //$timestamp = mysql_result(mysql_query("select `timestamp` `comments` `user_id` = $user_id"), 0, 'timestamp'); $r = format_time($timestamp); $question_id = $post_comment['id']; $q = "select `comment_id` `question_has_comments` `question_id` = $question_id order `timestamp` desc limit 1" ; $q = "select `comment_id` `comments` `question_id` = $question_id order `timestamp` desc limit 1" ; $post_id = $post_comment['id']; $send_back = $post_id . '|' . '<li id="' .$post_comment['id']. '" class="post_comment"> <div id="" class="give-margin"> <div id="" class="profile-page-avatar-wrapper"> <a href="#"><img id="" class="profile-page-avatar-image" src="./images/test/chemistry.jpg" alt=""></a><!-- imagem --> </div> <div id="" class="profile-page-uploader-tools"> <div id="" class="profile-image-btn"> <div id="" class="profile-page-btn-wrapper"> <div id="" class="header-id"> <a href="#"><span id="user-name">' . $username . '</span></a> </div> <div id="" class="question-page-feed-answer-header-timer"> <a id="feed-answer-header-timer" href="#"><span class="timer" data-time="">' . $r . '</span></a> </div> </div> <!-- fecha div wrapper botao--> </div> <p>' . $post_comment['comment'] . '</p> </div> </div> </li> '; echo $send_back; }
Comments
Post a Comment