PHP array_push doesn't work with jquery $_REQUEST object

Discussion in 'PHP' started by makamo66, Dec 16, 2012.

  1. #1
    At http://maureenmoore.com/momp_112412/112412.html I am trying to make an array of the divs that were dragged into the droppable box. Please see the view source.


    I use $.post to send the divs to the php file process.php and it gives me 1 to 3 arrays depending on how many divs I dragged instead of just one combined array. I want to push the $_REQUEST object (i.e., the dropped div) into an array using


    $stack = array();
    foreach ($_REQUEST as $key => $value) {
                array_push($stack,$value);
              }
    print_r($stack);
    
    Code (markup):
    but it gives me 1 to 3 arrays each with the key of zero. How do I get one array out of for example 3 dragged divs?
     
    makamo66, Dec 16, 2012 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    This is more of a JS issue than PHP.

    In your code, you are attaching a click event EVERY time you move a box into the right grid. If you move it out then move it again, then when you click submit, it will make two post calls.

    The other problem is that before you make the click() function, data gets reset to the current box value, thus never sending all the data.

    Here is a possible solution:

    
    var selectedBoxes = new Array();
    $(document).ready(function(){
        $( "#draggable1" ).draggable({ grid: [ 20,20 ] });
        $( "#draggable2" ).draggable({ grid: [ 20,20 ] });
        $( "#draggable3" ).draggable({ grid: [ 20,20 ] });	 
        $( "#droppable_box" ).droppable({
            drop: function( event, ui ) {
                var data = $(ui.draggable).attr('id');
                if ($.inArray(data , selectedBoxes ) >= 0) {
                     // if data not in selectedBoxes , add it
                     selectedBoxes[selectedBoxes.length] = data;
                }
             });
           },
           // Dropped out, remove it
           out: function( event, ui ) {
               var data = $(ui.draggable).attr('id');
               var sub;
               sub = $.inArray(data , selectedBoxes )
               if (sub >= 0) {
                   selectedBoxes.splice(sub, 1);
               }
           }
        });
        $('#myId').click(function(event){
            $.post("process.php",({data: selectedBoxes}),function(data,status){	
            alert("Data: " + data);
        });
    });
    
    PHP:
    Not tested, so you might have to fix some issues.
     
    ThePHPMaster, Dec 16, 2012 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    My advice -- swing an axe at the jquery bloat and stop pissing on accessibility, and use checkboxes instead. Latch an onchange to do a class swap on the parent, forgetting the whole drag and drop nonsense. There's a reason you don't see this type of stuff on 'real' websites.
     
    deathshadow, Dec 17, 2012 IP
  4. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    I implemented your suggested code at maureenmoore.com/momp_112412/121712.html but it didn't work. I also tried putting the click function outside of the drop function at maureenmoore.com/momp_112412/121712_900.html but that didn't work either. Thanks for the help anyway.
     
    makamo66, Dec 17, 2012 IP
  5. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    The winning code was from adeneo at stack overflow You can view the source here: maureenmoore.com/momp_112412/121712_1400.html
     
    makamo66, Dec 17, 2012 IP
  6. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    Thanks to ThePHPMaster for the out code for when the draggable is taken out of the droppable box. I ended up using it after all.
     
    makamo66, Dec 22, 2012 IP