php - Ajax form only submitting sometimes -


there several questions on none of answers have worked me. have tried them all.

i tried minimize code pasting, it's kind of hard script

i have comment form submitted via ajax php script saves comment , gets comments , redisplays them new comment can displayed without refreshing page.

only comments submit database , redisplay properly. usually every other submit comment saved. every other time nothing seems happen.

my real issue comments not being saved every time 1 submitted.

here javascript , ajax call:

$(document).ready(function(){     var working = false;      $('#commentform').submit(function(e){          if(working) return false;          working = true;         $('#submitcomment').val('working..');         $('span.error').remove();          $.post('/ajax/comment.process.php',$(this).serialize(),function(msg){              working = false;             $('#submitcomment').val('submit');              if(msg.status){                  $('#commentarea').slidedown().$(msg.html).prepend('#commentarea');             $('#blogcomment').val('');             }             else {                  $.each(msg.errors,function(k,v){                     $('label[for='+k+']').append('<span class="error">'+v+'</span>');                 });             }         },'json');     }); }); 

and here function submits comment:

public function addcomment($user_id) {      $validate = new data_validation;      $_post = $validate->sanitize($_post);      $newcom = $_post['blogcomment'];     $blog_id = intval($_post['blogid']);     $photosubmit = $_post['comphoto'];      $newcomquery = $this->mysqli->query("insert b_comments (blog_id, user_id, date, content, photo) values ('".$blog_id."', '".$user_id."', now(), '".$newcom."', '".$photosubmit."')");      if($newcomquery === false) {         echo "query failed";     }else{          $returncom = $this->commarkup($blog_id);         echo $returncom;      }            } 

and here piece of commarkup() function echos comments (it important pieces):

//  method outputs xhtml markup of comment public function commarkup($blog_id) {      $sql = $this->mysqli->query("select * b_comments blog_id = '".$blog_id."' order date desc");      while($d = $sql->fetch_assoc()) {          $d = $validate->sanitize($d);          echo "              <div class='comment-block'>                 <span class='com-img'><img src='".$photo_path."' /></span>                 <h3 style='display: inline;'><a href='".$profile."'>".$username."</a></h3>                 <div class='com-date'>".$d['date']."</div>                 <p>".$comcontent."</p>             </div>         ";     } } 

edit: here comment.process.php code requested:

    session_start();  include_once('../classes/comment.class.php'); include_once('../classes/db.class.php'); include_once('../classes/user.class.php');  $user_id = $_session['user_id'];  $db = new dbconnection; $comments = new comment($db); $user = new user($db);  $blogid = intval($_post['blogid']);  $addcom = $comments->addcomment($user_id);  echo json_encode(array('status'=>1,'html'=>$addcom)); 

given description, guess has working variable, , fact not set false @ end of $.post().

but there few logic, efficiency, , manageability issues way you've drawn process. i'd recommend having @ official jquery docs $.post(), .done(), .fail(), , .always() chained methods.

i'd recommend naming php variable other $_post, not become confused php super global.

finally, i'd recommend treating comment object , using pdo (this link pdo:query kind of "immersion" approach sure read docs). save ton of headaches in database interaction.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -