php - Submitting form data with javascript (No page reload) -
i've got wall/social system (much facebook) there's statuses, want able like, dislike , comment on status without page reloading, form below how complete this?
if(empty($_get['action'])){ postupdate(); if(isset($_post['scomment'])){ $dbc = mysqli_connect(db_host, db_user, db_password, db_name); $comment = mysqli_real_escape_string($dbc, trim($_post['comment'])); $sid = mysqli_real_escape_string($dbc, trim($_post['sid'])); $dbc = mysqli_connect(db_host, db_user, db_password, db_name); $query = "insert comments (`user`, `post`, `time`, `content`) values ('".$_session['user_id']."', '$sid', now(), '$comment')"; $data = mysqli_query($dbc, $query); } $dbc = mysqli_connect(db_host, db_user, db_password, db_name); $query = "select posts.*, user.* posts join user on user.user_id = posts.user_id join friends on friends.user = ".$userinfo['id']." , friends.with = posts.user_id order posts.post_id desc"; $data = mysqli_query($dbc, $query); while($row = mysqli_fetch_array($data)){ echo'<div class="shadowbar"> <div class="postedby"><b> posted <a href="index.php?action=user&u='.$row['user_id'].'" class="btn-link">'. $row['firstname'] .' '. $row['lastname'] .'</a> on '. date('m j y g:i a', strtotime($row['postdate'])) .'</b></div>'; $parsed = $parser->parse($row['post']); echo '<pre>'.$parsed.'</pre> <hr> <form method="post" action="index.php" class="commentform"> <fieldset> <div class="input-group"> <textarea cols="150" rows="1" style="resize: none;" placeholder="comment..." class="form-control" type="text" id="commentbox" name="comment"></textarea> </div> <input type="hidden" value="'.$row['post_id'].'" name="sid"> </fieldset> <input class="link lbutton" type="submit" value="add comment" name="scomment" /> </form> </div>'; } echo '</div>'; } the form can changed fit code. i'm assuming it's javascript need use.
here's code started.
first of all, add id or class in order identify form(s):
<form method="post" action="index.php?action=comment" class='commentform'> since tagged jquery, shall use that:
$(document).on('submit', '.commentform', function(e) { e.preventdefault(); // prevents default page reload after form submission $.ajax({ type: $(this).prop('method'), url: $(this).prop('action'), data: $(this).serialize() }).done(function() { // after submits alert('thanks comment!'); }).fail(function() { // there error alert('an error occurred'); }); }); you can read more jquery.ajax in jquery documentation.
Comments
Post a Comment