0

I have a 5x5 grid of div boxes (25 of them) and I am using jQuery UI to register when I drop a item in it. It will receive the title of the box it was dropped in and the name of the item, that part works.

I want to pass the title and name to PHP without refreshing the page (because then the items will reset). I get a "success!" but it seems like it doesn't pass the data.

index.php

<script>
    $(function() {
        $( "li img" ).draggable({ 
            snap: ".grid", 
            start: function (event, ui ) {
                item = $(this).attr('title');
            }
        });
        $( "li .grid" ).droppable({
            drop: function( event, ui ) {
                box = $(this).attr('title');
                $.ajax({
                    type: "POST",
                    url: 'index.php',
                    data: { box : 'box' },
                    success: function(data){
                        alert("success!");
                    }
                });

            }
        });
    });        
</script>

sessions.php

<?php
   session_start();
if(isset($_POST['box']))
{
   // store session data
   $_SESSION['box'] = $_POST['box'];
}
   //retrieve session data
   echo $_SESSION[box];
?>

How do I pass the title and name to PHP without refreshing the page?

2
  • @Dagon You're far out of line. Commented Jan 16, 2014 at 3:23
  • Thanks, you just gave me an idea, i was stuck on it like 3 hours now. Commented Apr 16, 2015 at 11:11

2 Answers 2

1

Try to change the url and your data from this:

url: 'index.php',
data: { box : 'box' }

to this:

url: 'sessions.php',
data: { box : box }
Sign up to request clarification or add additional context in comments.

1 Comment

i feel like a total idiot right now.. oh well thanks. while im at it how do i make it add instead of overwrite the data in session[box] ?
0

Given that you're doing this...

box = $(this).attr('title');

I assume you want to pass that variable to the server. However, you're just passing the literal string "box", not the variable box.

In your code, you've written this...

data: { box : 'box' },

which needs to be this, to pass the value in box:

data: { box : box },

As an aside, you've created a global variable box. Instead of box = $(this).attr('title');, you should use var box = $(this).attr('title'); to create a variable local to the function.

3 Comments

i just tryed that, as before i get "success!" but it dosent get the data. i have <?php echo $_SESSION['box']; ?> in my index.php to se if the data is there but it dosen't write enything. and thanks for the "var" tip.
Verify the data is actually being sent. Check the value of box or open your browser's developer tools and inspect the AJAX request.
Felix found the stupidest error i have made ever. But big thanks to you for trying to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.