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 post
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 post
Share on other sites

Check the network tab to look at the ajax request and see what data you're sending to the server. The data object in your code above isn't formatted correctly, that's not how to define an object.

Link to post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...