14 Replies - 3350 Views - Last Post: 17 December 2012 - 05:21 PM

#1 makamo66  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 42
  • Joined: 04-February 09

ui.draggable isn't getting pushed into an array and joined

Posted 19 November 2012 - 06:18 AM

I want the ids of the draggable divs to show up as a comma separated list in the alert button if they were dropped into the droppable_box. Instead there is an alert for each dragged div one after the other instead of as one list.

<script type="text/javascript">
   $(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 currentId = $(ui.draggable).attr('id');

			var stuff = [];
			$.each($(ui.draggable), function(i,e) {
  				stuff.push(e.id);
			});

			var energy = stuff.join(", ");
	
			var data = "X-Requested-With=XMLHttpRequest";
	
	$('#myId').click(function(){

		$.post (
			"process.php",
			data,
			function(data)
			{
			alert(energy);
			
			},
			'html'
			);
		 });

		}
		});
	});
</script>	


In the html there are 3 draggable divs

<div id="draggable1" class="draggable_meal"></div>
<div id="draggable2" class="draggable_meal"></div>
<div id="draggable3" class="draggable_meal"></div>



Is This A Good Question/Topic? 0
  • +

Replies To: ui.draggable isn't getting pushed into an array and joined

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3521
  • View blog
  • Posts: 10,163
  • Joined: 08-June 10

Re: ui.draggable isn't getting pushed into an array and joined

Posted 22 November 2012 - 03:51 AM

I think the point is that you drag (code-wise) the divs only one-by-one, hence an AJAX call for each which results in 3 alerts.

you would definitely have to check, whether the energy array contains what you expect.

This post has been edited by Dormilich: 22 November 2012 - 03:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 makamo66  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 42
  • Joined: 04-February 09

Re: ui.draggable isn't getting pushed into an array and joined

Posted 24 November 2012 - 06:45 AM

I changed the jquery a little and published it here:

http://maureenmoore....412/112412.html

You can look at the view source. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 makamo66  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 42
  • Joined: 04-February 09

Re: ui.draggable isn't getting pushed into an array and joined

Posted 10 December 2012 - 08:02 AM

In process.php it gives me 1 to 3 arrays depending on how many divs I dragged. I want to push the $_REQUEST object into an array using
foreach ($_REQUEST as $key => $value) {
			array_push($stack,$value);
          }
print_r($stack);


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?
Was This Post Helpful? 0
  • +
  • -

#5 makamo66  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 42
  • Joined: 04-February 09

Re: ui.draggable isn't getting pushed into an array and joined

Posted 16 December 2012 - 06:36 AM

At http://maureenmoore....412/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);


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?
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 5,979
  • Joined: 08-June 10

Re: ui.draggable isn't getting pushed into an array and joined

Posted 16 December 2012 - 06:53 AM

I've merged your new topic into your old one. Please don't double post your questions.

OK, so this is the code from your site that is responsible for handling the drag and drop thing. (I've indented it properly. See how much clearer and easier to read it is this way?!)
$(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');
 			$("#response").append(data);
            $('#myId').click(function(event){
                $.post("process.php",({id : data}),function(data,status){
                    alert("Data: " + data);
                });
            );
        }
    });
});


The reason you are getting three distinct arrays, each with only one of the dragged elements, is because your $('#myId').click handler is being set inside the drop event handler of the #droppable_box element. As a result, each time you drop one of the three elements into the box, a NEW click event is added to the #myId element, so that when you click the button, each of those handlers is called and you end up sending each element to PHP in a separate request. PHP is never aware that the three elements are supposed to be grouped, because you only send it one of them at a time.

What you want to be doing is creating an array in Javascript, and each time something is dropped onto the droppable area, add whatever is dropped there into this array. Then define the button's click handler outside all of that, and have it send the contents of the array instead. Then PHP will receive all three elements and can process them together.


By the way, it's generally not a great idea to use $_REQUEST in PHP. You are better of using whatever, specific source you need, like $_POST, $_GET or $_COOKIE. (That would be $_POST, in your case.)
Was This Post Helpful? 0
  • +
  • -

#7 makamo66  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 42
  • Joined: 04-February 09

Re: ui.draggable isn't getting pushed into an array and joined

Posted 16 December 2012 - 09:25 AM

At http://maureenmoore....412/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);


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?
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2927
  • View blog
  • Posts: 10,118
  • Joined: 08-August 08

Re: ui.draggable isn't getting pushed into an array and joined

Posted 16 December 2012 - 11:41 AM

Check your $_REQUEST array. When I run this:
<?php
$stack = array();
$r = array("key1"=>"valueone", "key2"=>"valuetwo", "key3"=>"valuethree");
foreach ($r as $key => $value) {
			array_push($stack,$value);
          }
print_r($stack);


I get this result:
Array ( [0] => valueone [1] => valuetwo [2] => valuethree )

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,353
  • Joined: 12-December 12

Re: ui.draggable isn't getting pushed into an array and joined

Posted 16 December 2012 - 04:23 PM

Your click event is assigned within the drop method, so each time you drop a div you are creating a completing separate click event on the same button. When you click Submit you are then posting separate 'data' to the other page, which is why you receive more than one alert.

$(document).ready(function(){
    var data = [];

Each time a div is dropped, push the id into this array. If the same div is dragged back and forth you'll need to remove the duplicate occurrences of the id. One way to do this in JS:
uniqueArray = myArray.filter(function(elem, pos) {
    return myArray.indexOf(elem) == pos;
})


You can then move the click event handler completely out of the drop method. I don't believe you can just pass an array as the data parameter: it is expecting a value or an object. You could use Javascript join() to convert the array to a string, and PHP explode() to convert it back to an array.

Actually, you could probably post an object which happens to have an array as its only member(?). But there are other ways to do this (json, etc..). So you could just use an object (var data = {}) rather than an array.

BTW The other page is not receiving an array (of ids) - it is just that $_REQUEST is an array.

I meant PHP implode(), to convert an array to a string: explode() does the reverse.

Oops: re. explode().. I was right the first time! :whistling:
Was This Post Helpful? 1
  • +
  • -

#10 makamo66  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 42
  • Joined: 04-February 09

Re: ui.draggable isn't getting pushed into an array and joined

Posted 17 December 2012 - 12:02 PM

The winning code was by adeneo at stack overflow You can view the source here: http://maureenmoore....21712_1400.html
Was This Post Helpful? -5
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,353
  • Joined: 12-December 12

Re: ui.draggable isn't getting pushed into an array and joined

Posted 17 December 2012 - 12:32 PM

View Postmakamo66, on 17 December 2012 - 12:02 PM, said:

The winning code was by adeneo at stack overflow You can view the source here: http://maureenmoore....21712_1400.html

This is not a competition for your benefit. You should not cross-post as it wastes my time and possibly others.

If you post in one forum and do not receive a response within a reasonable time then it is understandable that you would try a different forum. But when you receive an answer you should confirm this in the other forum.
Was This Post Helpful? 4
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9076
  • View blog
  • Posts: 34,119
  • Joined: 12-June 08

Re: ui.draggable isn't getting pushed into an array and joined

Posted 17 December 2012 - 01:47 PM

A bit rude there OP.

If anyone cares:
http://stackoverflow...-request-object
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,353
  • Joined: 12-December 12

Re: ui.draggable isn't getting pushed into an array and joined

Posted 17 December 2012 - 03:09 PM

.. and if you drag the divs back and forth you end up with duplicate ids :smartass:
Was This Post Helpful? 0
  • +
  • -

#14 laytonsdad  Icon User is online

  • Cheese and Sprinkles
  • member icon

Reputation: 436
  • View blog
  • Posts: 1,847
  • Joined: 30-April 10

Re: ui.draggable isn't getting pushed into an array and joined

Posted 17 December 2012 - 04:30 PM

This is not a competition site!!

This is a site to get help!! People volunteer their time here to help others LEARN and not just get a snippet handed to them with no knowledge of how it works.

Also, in my opinion, The folks here are the best at teaching what they know and you people should not be wasting our time that could be spent helping others.

This post has been edited by laytonsdad: 17 December 2012 - 04:31 PM

Was This Post Helpful? 0
  • +
  • -

#15 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 5,979
  • Joined: 08-June 10

Re: ui.draggable isn't getting pushed into an array and joined

Posted 17 December 2012 - 05:21 PM

makamo66, like I told you before: Do not double post your questions here! I've, again, merged your threads. Don't do this again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1