-1

I would like to select streamitem_id from my insert.php and add it to my existing post div. I'm needing the id of the post made so I can add it into my div so later when I add my delete button it will delete the post and the div that holds it. Hope I've made sense and I can do this with what I have as I'm trying to learn but it is a difficult language like any when first starting out.

AJAX

 <script>
    $(document).ready(function(){
        $("form#myform").submit(function(event) {
            event.preventDefault();
            var content = $("#toid").val();
            var newmsg = $("#newmsg").val();

            $.ajax({
                type: "POST",
                url: "insert.php",
                data: "toid=" + content + "&newmsg=" + newmsg,
                success: function(){
                    $("#homestatusid").prepend("<div id='divider-"+WHERE MY STREAMITEM_ID NEEDS TO BE+"'><div class='userinfo'>"+newmsg+"</div></div>");
                }
            });
        });
    });
    </script>

INSERT.PHP

$check = "SELECT streamitem_id FROM streamdata WHERE streamitem_id=$user1_id";
        $check1     =    mysql_query($check);
        $check2     =    mysql_num_rows($check1);
    echo $check2;
dave
  • 1,009
  • 5
  • 15
  • 26
  • 2
    First, please note that `mysql_*` functions are being deprecated and should no longer be used. Instead use [PDO](http://php.net/manual/en/book.pdo.php) or [mysqli](http://php.net/manual/en/book.mysqli.php). If you're not sure which one to use, [read this article from SO](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – Matt Aug 02 '12 at 14:12
  • Yeah. I need to update my whole site, going to put some time aside just to do that. Cheers. – dave Aug 02 '12 at 14:14
  • Second, it looks like you never pass `$user1_id` to *insert.php*, then once you're done with your query, you echo the number of rows, **not** the `streamitem_id`. – Matt Aug 02 '12 at 14:14
  • User1_id is toid and I didn't post my full insert.php page as I just needed that little section. So how do I get the post id into the div say I post into my database entry 2012 I need that number. – dave Aug 02 '12 at 14:17
  • 1
    In your case, (again, stay away from `mysql_*` functions going forward) assuming this returns one row, `$resultArr = mysql_fetch_array($check1); echo $resultArr['streamitem_id'];` – Matt Aug 02 '12 at 14:19
  • Can I use the same method for adding other things into the post like my delete button etc as long as I echo it out? – dave Aug 02 '12 at 14:29
  • This is programming, dave. You can do anything you need to do! (What I'm saying is, trust in your own creativity and you'll eventually get the answer you need. In time, the answers will come quicker because you'll notice how many problems are eerily similar to one another.) – Matt Aug 02 '12 at 14:31
  • How right you are Matt. Thank you for your time. And thank you to everyone else. You've all been very helpful. Its developers like yourselves that make this place a great place to come and hang out and learn from. – dave Aug 02 '12 at 14:33

3 Answers3

2

In your javascript should be

       $.ajax({
            type: "POST",
            url: "insert.php",
            data: {toid:content, newmsg: newmsg}, # pay attention to this line
            success: function(data){
                $("#homestatusid").prepend("<div id='divider-"+data+"'><div class='userinfo'>"+newmsg+"</div></div>");
            }
        });

PHP

$toid = isset($_POST['toid']) ? $_POST['toid'] : null;
$newmsg = isset($_POST['newmsg']) ? $_POST['newmsg'] : null;

And do not use mysql_* since it deprecated

Alexander Larikov
  • 2,328
  • 15
  • 15
  • 1
    Yeah, I'm fully aware. Just need to update all my code. Which I shall do shortly. Thanks. – dave Aug 02 '12 at 14:19
1
success: function(response){
    $("#homestatusid").prepend("<div id='divider-"+response+"'><div class='userinfo'>"+newmsg+"</div></div>");
}
Constantine
  • 119
  • 8
1

The first argument passed to the success callback is the responseText from the AJAX call. Modify your jQuery code to this:

success: function(responseText){
    $("#homestatusid").prepend("<div id='divider-"+responseText+"'><div class='userinfo'>"+newmsg+"</div></div>");
    // I'm assuming that insert.php returns just the ID you're interested in
}
Anthony Grist
  • 38,173
  • 8
  • 62
  • 76