Jump to content

Inserting data into db with ajax Problem


Recommended Posts

Hi, I have a problem with ajax posting into db, the post_id is not inserted into db, instead is inserted 0.

In the firebug console i get this message: POST http://localscript.dev/stream/post-comment 200 OK


If i inspect the element with firebug the hidden input with the id is correct.


I have a form looking like this:

<form action="stream/post-comment" method="POST" class="ajaxForm">	<inpu type="hidden" name="stream_id" value="<?php echo $stream['id']; ?>">    <div class="col-md-10" style="padding-left: 0px;"> <input type="text" name="commentText" class="form-control ajaxClear"> </div>    <div class="col-md-1" style="padding: 0px;"> <input type="submit" value="Post" class="btn btn-primary postStreamComment"> </div></form>

on the stream/post-comment file i have something like this:

$id = $_POST['stream_id'];$comment = htmlspecialchars($_POST['commentText']);$user = $_SESSION['user_id'];$mysqli->query("INSERT INTO stream_comments('post_id', 'comment', 'posted_by') VALUES('{$id}', '{$comment}', '{$user}')") or trigger_error($mysqli->error);

and in my js file i have this code:

$(document).ready(function(){  $('.postStreamComment').click(function(){    $.ajax({      url: 'stream/post-comment',      type: "post",      data: {        'commentText':$('input[name=commentText]').val(),        'stream_id':$('input[name=stream_id]').val()    },      success: function(data){        $('.ajaxClear').val('');      }    });    return false;  });});

What is the problem with my code?



Link to comment
Share on other sites

Sorry, the typo was repaired in my file (just after posting this and I forgot to correct here too), but in the database is inserted only the id of the the last post.


I'm a jquery beginner (so... probably I'm wrong, because i'm still learning), but I think it should be something like this:

$(document).ready(function(){  $('.postStreamComment').click(function(){    //   e.preventDefault();    $.ajax({      url: 'stream/post-comment',      type: "post",      data: {          $('.ajaxForm').each(function(){              'commentText':$('input[name=commentText]').val(),              'stream_id':$('input[name="stream_id"]').attr('value')          });    },      success: function(data){        $('.ajaxClear').val('');        alert($('input[name=stream_id]').val());        //this alerts the id of the last post every time...      }    });    return false;  });});
Link to comment
Share on other sites

In the network tab i get this in the general section:

Request URL:http://localscript.dev/stream/post-commentRequest Method:POSTStatus Code:200 OK


And this in the form data section:

stream_id:3commentText:my test text goes here

Now it sends the correct id/text to the db but it redirects me to http://localscript.dev/stream/post-comment ...

Edited by JustMike
Link to comment
Share on other sites

I changed my jquery code and now I have this:

$(document).ready(function(){$('.ajaxForm').each(function (){if( $(this).attr('data-id') == $('.postStreamComment').attr('data-id') ) {    $('.postStreamComment').click(function(e){        e.preventDefault();            $.post(              '/stream/post-comment',              {                  commentText:$('.comment-'+$(this).attr('data-id')).val(),                  stream_id:$(this).attr('data-id')              },              function( data ){                $('.comment-'+$(this).attr('data-id')).val('')              },              'json'            );    });}});});

Now it posts the correct information into the database but the code that should clear the comment input (this code: $('.comment-'+$(this).attr('data-id')).val('') ) is not working...


Can you tell me what I'm doing wrong now please?

Thank you!





Solved the problem, put the code just outside $.post().

Thanks for the help!

Edited by JustMike
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...