javascript - AJAX improvement for validation -


so have piece of ajax work should want add validation it.i dont want send empty information.the ajax done comment system.it takes value form , php inserts databse. problem haveing insert empty comment database

index.php

<?php      require_once("menu.php"); ?>         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>         <script  src="comments.js" type="text/javascript" ></script>         <script type="text/javascript">             function validateform()             {                 var comment = document.getelementsbyname('comment').value;                  if (comment == "" )                  {                     alert("please fill in fields");                     return false;                 }                 else                 {                     return true;                 }             }         </script> <?php      $connection = connecttomysql();       $selectpostquery = "select * (select * `tblposts` order id desc limit 3) t order id desc";      $result = mysqli_query($connection,$selectpostquery)         or die("error in query: ". mysqli_error($connection));     while ($row = mysqli_fetch_assoc($result))      {         $postid = $row['id'];  ?>         <div class="wrapper">         <div class="titlecontainer">         <h1><?php echo $row['title']?></h1>         </div>         <div class="textcontainer">         <?php echo $row['content']?>         </div> <?php         if (!empty($row['imagepath'])) #this check if there path in textfield         { ?>             <div class="imagecontainer">             <img src="images/<?php echo "$row[imagepath]"; ?>" alt="article image">             </div> <?php         } ?>         <div class="timestampcontainer">         <b>date posted :</b><?php echo $row['timestamp']?>         <b>author :</b> admin         </div> <?php         #selecting comments corresponding post         $selectcommentquery = "select * `tblcomments` left join `tblusers` on tblcomments.userid = tblusers.id tblcomments.postid ='$postid'";          $commentresult = mysqli_query($connection,$selectcommentquery)             or die ("error in query: ". mysqli_error($connection));          #renderinf comments          echo '<div class="comment-block_' . $postid .'">';          while ($commentrow = mysqli_fetch_assoc($commentresult))          { ?>             <div class="commentcontainer">             <div class="commentusername"><h1>username :<?php echo $commentrow['username']?></h1></div>             <div class="commentcontent"><?php echo $commentrow['content']?></div>             <div class="commenttimestamp"><?php echo $commentrow['timestamp']?></div>             </div> <?php         } ?>         </div> <?php           if (!empty($_session['userid']) )          { ?>             <form method="post" class="post-frm" action="index.php" onsubmit="return validateform();">             <label>new comment</label>             <textarea name="comment" class="comment"> </textarea>             <input type="hidden" name="postid" value="<?php echo $postid ?>">             <input type="submit" name ="submit" class="submitcomment"/>             </form> <?php         }         echo "</div>";         echo "<br /> <br /><br />";      }  require_once("footer.php") ?> 

my ajax code.i have tried validation doesnt work , dont know wrong it.i quite new ajax not rlealy able debug myself.

 $(document).ready(function(){     $(document).on('click','.submitcomment',function(e) {          e.preventdefault();         //send ajax request         var form = $(this).closest('form');         var comment = $('.comment');         if (!comment.val()){                 alert('you need write comment!');                }         else{             $.ajax({                 url: 'ajax_comment.php',                 type: 'post',                 cache: false,                 datatype: 'json',                 data: $(form).serialize(), //form serialize data                 beforesend: function(){                     //changeing submit button value text , disableing                     $(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);                 }             });         }     }); }); 

my php code insert database.

 <?php if (isset($_server['http_x_requested_with'])):      session_start();     include('connection.php');     $connection = connecttomysql();          $userid = $_session['userid'];         $username = $_session['username'];         $postid = $_post['postid'];         $comment = $_post['comment'];         $date_format = " y-m-d  g : : s";         $time = date ($date_format);             $insertcommentquery = "insert `tblcomments`                                   (`content`,`userid`,`postid`,`timestamp`)                                  values (                                    '$comment','$userid','$postid',                                     current_timestamp)";           $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?> 

you never call function validateform()

and part

if (!comment.val()){                 alert('you need write comment!');                } 

will pass if there input field class comment, if it's empty. like:

//send ajax request if(validateform()===false){     alert('you need write comment!');     return false;     }     else{      .... 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -