0

I'm just starting to learn jQuery, php etc. I have a div (scale) with images with two attributes. I want to write those attributes in a database. As they can change (drag & drop) I want to send all img attributes to the database to update every time a new item is dropped into the div. But instead of sending each child of the div only the current (new added) item is send (multiple times).

jQuery

stop: function(event, ui) {
       $('#scale').children().each(function(){
                var idd = $(this).attr("data-id");
                var size2 = parseInt($("#scale > img").length);
                var pos = parseInt($(this).index());
                var rat = 1.00-(1/size2*pos);
                $(this).attr('newRating', rat);

                console.log("We send Movie with ID : " + idd + " on Position: " +pos + " to the DB");

                $.post("userInfo.php",
                {
                    id:idd,
                    rating:rat

                },
                function(data, status){
                    console.log("Data: " + data  + "\nStatus: " + status + " \nof " +  $(ui.item).attr('data-id') + " \nwith " + $(ui.item).attr('newRating') );
                }
                );

            });
}

The first console.log gives me the correct ids and ratings, but the data that I get back from the .post() call is wrong.

php

        include_once('db.php');

    $id = $_POST['id'];
    $rating =   $_POST['rating'];


    if (mysql_query("INSERT INTO MovieRating SET id = '$id', rating ='$rating' ")){
        echo "Successfully Inserted";
    }
    else{
        echo "Insertion Failed";
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Nina
  • 11
  • 2
  • 1
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 07 '14 at 18:27
  • Have you watched the request / response in the browser's console? You can see what is sent and what is returned. – Jay Blanchard Nov 07 '14 at 18:28
  • Why don't you send all of them as one request instead of bombarding your server with tons of requests? – epascarello Nov 07 '14 at 18:31
  • Thank you I will look into that. I just checked the request/response. It looks like the correct data is posted. So it's probably a problem in my php? – Nina Nov 07 '14 at 18:34
  • UM, I think your log statement is wrong...You are reading the UI and not the item you sent.. – epascarello Nov 07 '14 at 18:34
  • @epascarello because I couldn't find a good way to do it. I did use the jquery sortable .toArray function but that only gives me back one attribute. I'm sorry I'm still very much a beginner to all this. Thanks for your help – Nina Nov 07 '14 at 18:36
  • @Nina it sounds like you've checked the *request* (you say the correct data is POSTed). Have you checked the *response* (the data that comes back)? That will tell you whether the PHP is correct or not. What does it return, and what are you expecting it to return? – Shai Nov 07 '14 at 18:36

1 Answers1

0
console.log("Data: " + data  + "\nStatus: " + status + " \nof " +  $(ui.item).attr('data-id') + " \nwith " + $(ui.item).attr('newRating') );
                                                                     ^^^^^^^                                   ^^^^^^

You are reading ui.item every time you log to the console. You want the item from the current iteration.

console.log("Data: " + data  + "\nStatus: " + status + " \nof " +  idd + " \nwith " + rat );
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • You're right. For some reasons I got undefined earlier. It does work now. Thank you. – Nina Nov 07 '14 at 18:39