0

I'm noob, and i recently knew about Ajax, so dont worry if my question seems idiot.

I tried to do that, but i had no success, but i will explain what i tried to do:

I have one draggble box with some words, and everytime that i drag some elements to a certain place, i want to record this transition into my database.

So, i did that in Ajax:

UPDATE

 $(document).ready(function() {
$(".event").draggable();
$(".drop").droppable({
    drop: function(event, ui) {
        var id = ui.draggable.attr("id");
         var targetid = event.target.id ;

        $.ajax( {
            type: 'post',
            url: "new.php",
             data : { 'day' : '3' },
             success: function( response ) {

                alert( response );

            }
        });


    }
});
});

New file:

function eventTransition($day){
$day = $_POST['day'];
    $insert="INSERT INTO events (event_day) VALUES (".$day.")";
      mysql_query($insert);

}

eventTransition($day);

I tried to automatically put a value to the day variable.

  • 1
    First, please read [this question](http://stackoverflow.com/q/12859942/1883647) and [this question](http://stackoverflow.com/q/60174/1883647) regarding your use of SQL statements in PHP. Second, can you describe what happened/went wrong, and what you expected? – ajp15243 Jul 22 '14 at 20:50
  • 1
    in theory thats more or less right, but theres no db connection you call $day in the function call yet its not defined .. –  Jul 22 '14 at 20:51
  • 1
    Your function parameter `$day` is unused, and overwritten by the superglobal `$_POST['day']`, but in doing so, it looks like this code should work to me. – IMSoP Jul 22 '14 at 20:51
  • when i print the response, appears a error message, thats what i am getting, i checked the connection and everything seems alright, thanks for the the answers – Steve Steve Jul 22 '14 at 20:57
  • @SteveSteve What is the error message? The more details you give sooner, the better everyone can help you more quickly. – ajp15243 Jul 22 '14 at 20:58
  • i receive "object not found" – Steve Steve Jul 22 '14 at 21:01

2 Answers2

0

Please try this in php file and reply if this helps you

function eventTransition($day){

    $insert="INSERT INTO events (event_day) VALUES (".$day.")";
    mysql_query($insert);
}

$day = $_POST['day'];
eventTransition($day);
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
baxri
  • 307
  • 1
  • 10
  • I didn't downvote, but the likely reason is not due to an error in your code, but due to the fact that your answer lacks an explanation as to what you changed and why. – ajp15243 Jul 22 '14 at 20:59
  • Thanks I hoped you understan :) I foresea:) – baxri Jul 23 '14 at 06:11
0

You cannot call a PHP function directly using Ajax. A(n over-)simplified new.php file may look like the following:

$day = $_REQUEST['day'];
$insert="INSERT INTO events (event_day) VALUES (".$day.")";
mysql_query($insert);

In your ajax call you must specify:

dataType: 'json'

As @baxri advises, add an error handler.

ron tornambe
  • 10,452
  • 7
  • 33
  • 60