How can I grab data from more than one droppable element - php

In my script I'm experimenting with jquery draggable & droppable, See here. I'm trying to grab the data (to eventually put in a database) from each draggable element that's dropped into the droppable div. Right now I have it so that I can only get the data from the div the was last dropped.
$('#sortcard, #dropbox, #dropbox1').droppable({
accept: '.sorting',
hoverClass: 'border',
tolerance: 'touch',
drop: function(e, ui) {
$(this).append(ui.draggable.html() + '<br/>');
$("#add_friend").show().fadeOut(12000);
$(e.target).droppable("disable");
$(e.target).append("<input type='button' name='Sub' value='clear'/>").click(function() {
$(this).empty().droppable("enable");
});
var dropbox = $('#dropbox').html();
var dropbox1 = $('#dropbox1').html();
if(dropbox && dropbox1 !== '') {
$.post("account_main.php", {data: $(this).text()}, function(data) {
$('#demo').html(data);
});
}
}
});
I'm thinking the culprit may be
{data: $(this).text()}, function(data) {
$('#demo').html(data);
});
I have toyed with it and still not getting the results I desire. I feel maybe my coding on this is sketchy and needs a makeover, but I just need some ideas to push me in the right direction to make it efficient and would be very appreciative as well of any tips.

You're only getting the data from the last droppable because your sending $(this).text().
In that context $(this) is the last droppable that was dropped. Since you want more than that, you'll want to replace:
{data: $(this).text()}
with something like:
{
sortcard: $('div#sortcard').text(),
dropbox: $('div#dropbox').text(),
dropbox1: $('div#dropbox1').text()
}
Which will send the data from those three DIVs.

Related

ajax call only works when tablesorter is clicked

I really don't know how to explain this. I have a table and I am using tablesorter to make columns sortable.
at the end of each row i have a button that has a jquery listener to fire off an ajax call. For debugging purposes, all that called script does is print_r($_POST). This ajax call only works after I click on a column to sort table. IF i don't, i get no response from ajax call. In firebug, if i dont click on column to sort, I get a red http:Post response, if i click on a table column, i get the response i would expect.
//tablesorter call
$('#pendingItems').tablesorter();
//dialog setup
$('#removeItem').dialog(
{
autoOpen:false,
width: 500,
modal: true,
resizable: false,
closeOnEscape: true,
buttons:
{
"Ok": function()
{
//window.location.replace('items.php');
}
}
});
//listener for button click
$('.removeItem').click (function()
{
var attrId = $(this).attr('id');
var gid = attrId.split('_');
var itemId = gid[1];
$.ajax({
type: "POST",
url: "removeItems.php",
data: "itemId="+itemId,
success: function(result)
{
alert('hi');
$('#removeItem').html(result);
$('#removeItem').dialog('open');
}
});
});
and in the table.
<input type='image' src='images/trashcan2.png' id='remove_" . $r['id'] . "' name='remove_" . $r['id'] . "' class='removeItem'>
where $r['id'] is a number.
in firebug:
looking at net tab. on failed attempt, the post goes to items.php (the original page). if i click on table column, and then the button, the post goes to the removeItems.php (the correct page)......
Maybe (probably...) tablesorter is modifying the DOM, causing your bindings to disappear.
To see if that is the problem - and to solve it - just change:
$('.removeItem').click (function()
to:
$('.removeItem').on('click', function()
Note that on() requires jQuery 1.7+
Also in POSt type, data should be like: data: {temId: itemId},
So i found that input type image submits the form. Adding return false; to the click event fixed the problem.

Pushing data to a html list for search using ajax, jquery and php, codeigniter

First I want to show you guys what I am trying to do.
I am trying to create a search functionality in my application using AJAX, jquery and php
and as for the framework I am using Codeigniter, but it's not necessary to be CI, this could be similar to all(well I suppose).
I have this piece of code to observe a focus and blur event.
$("#searchbox").on({
keyup : debounce(function(){
MSI.Interface.search();
},350,false),
blur : function(){
$("#search_results").hide();
}
});
I have not finished it yet that is why the blur event has only .hide() . I can't think of what other else to include, maybe reset the html of the #search_results to make it blank, but I don't know if that is reasonable.
1st question: What do you think is a more reasonable solution for that?
As you can see in the previous code, i have the debounce function, just to prevent per character request on the server, I wonder if that is correct.
Then I have this search function
search : function() {
var keyword = $("#searchbox").val();
if (keyword == '') {
} else {
$.ajax({
url : MSI.variables.base_url + 'search',
type: 'POST',
data: {
keyword : keyword
},
dataType : 'json',
success: function(output) {
$.each(output, function() {
$.each(this, function(key, value){
$("#search_results").show().prepend("<p>"+value+"</p>");
});
});
}
});
}
}
With that code, the script will be able to add contents to the #search_results div.
2nd Question: What do you think is a better solution for this?

$.post not working when element dropped

I'm teaching myself jQuery and PHP and I'm having a few problems. I need the element id to be posted to my php script once dropped over my droppable div. The following code does not post when I drag and drop an element over a droppable div. I have tried the following:
I have removed everything apart from an alert box to check that an item has been dropped which it does, so the item is being seen as droppable!.
I have entered some content into the div manually to ensure results can been seen
I have replaced name to var name="test", to test the varible contents.
The javascript file and the php script reside in the same directory, file names are correct. The javascript drag and drop script is included after droppable area
<script type="text/javascript" src="js/drag-drop.js"></script>
I have replaced the result of php script as plain text to see if the problem could be the variable received, nothing returns. So the post does not seem to be getting to the php script. Hope someone can provide a few pointers!
Below is my code, the HTML is just a div.
$(document).ready(function() {
$('#carousel-ul li').draggable({
helper: 'clone',
revert: true
});
$('#drop').droppable({
hoverClass: 'drophover',
accept: '#carousel-ul li',
drop: function(){
//alert("has been accepted and dropped");
var name = ui.helper.attr('id').val();
$.post('javascript.php', { name: name }, function(data){
$('#drop-content').html(data);
});
}
});
});
<?php
//echo "id was posted";
if(isset($_POST['name'])){
$name = $_POST['name'];
var_dump($name);
echo name;
//echo "id was posted inside";
}
?>
The API documentation for the Droppable widget says that the ui parameter is a jQuery object, so you should try something like this:
$('#drop').droppable({
hoverClass: 'drophover',
accept: '#carousel-ul li',
drop: function(event, ui) {
var name = $(ui.helper).attr('id');
var content = $(ui.helper).html();
$.post('javascript.php', { name: name, content: content }, function(data) {
$('#drop-content').html(data);
});
}
});

Print the value in loop in Jquery ready function

somehow still not able to do what I’m inted to do. It gives me the last value in loop on click not sure why. Here I want the value which is been clicked.
Here is my code:
$(document).ready(function() {
var link = $('a[id]').size();
//alert(link);
var i=1;
while (i<=link)
{
$('#payment_'+i).click(function(){
//alert($("#pro_path_"+i).val());
$.post("<?php echo $base; ?>form/setpropath/", {pro_path: $("#pro_path_"+i).val()}, function(data){
//alert(data);
$("#container").html(data);
});
});
i++;
}
});
Here the placement_1, placement_2 .... are the hrefs and the pro_path is the value I want to post, the value is defined in the hidden input type with id as pro_path_1, pro_path_2, etc. and here the hrefs varies for different users so in the code I have $('a[id]').size(). Somehow when execute and alert I get last value in the loop and I don’t want that, it should be that value which is clicked.
I think onready event it should have parsed the document and the values inside the loop
I’m not sure where I went wrong. Please help me to get my intended result.
Thanks, all
I would suggest using the startsWith attribute filter and getting rid of the while loop:
$(document).ready(function() {
$('a[id^=payment_]').each(function() {
//extract the number from the current id
var num = $(this).attr('id').split('_')[1];
$(this).click(function(){
$.post("<?php echo $base; ?>form/setpropath/", {pro_path: $("#pro_path_" + num).val()},function(data){
$("#container").html(data);
});
});
});
});
You have to use a local copy of i:
$('#payment_'+i).click(function(){
var i = i; // copies global i to local i
$.post("<?php echo $base; ?>form/setpropath/", {pro_path: $("#pro_path_"+i).val()}, function(data){
$("#container").html(data);
});
});
Otherwise the callback function will use the global i.
Here is a note on multiple/concurrent Asynchronous Requests:
Since you are sending multiple requests via AJAX you should keep in mind that only 2 concurrent requests are supported by browsers.
So it is only natural that you get only the response from the last request.
What if you added a class to each of the links and do something like this
$(function() {
$('.paymentbutton').click(function(e) {
$.post("<?php echo $base; ?>form/setpropath/",
{pro_path: $(this).val()},
function(data) {
$("#container").html(data);
});
});
});
});
Note the use of $(this) to get the link that was clicked.

How can I revert a 'draggable' upon failed AJAX request?

I have a draggable div, that needs to be dropped on a droppable. If it is dropped correctly, the droppable calls an AJAX request. If the draggable does not have a valid id or the AJAX request fails, the draggable should be reverted to it's original position. Is that something that can be done with jQuery?!
Any help is greatly appreciated!!
$("div.draggable").livequery(function(){
$(this).draggable({
revert: 'invalid',
distance: 20
})
});
$("#trash").droppable({
tolerance: 'touch',
drop: function(event, ui)
{
var item = ui.draggable;
var id = item.attr('id');
if (id)
{
$.ajax(
{
type: "POST",
url: "delete.php",
data: "id=" + id,
success: function(html)
{
if (html)
{
item.remove();
}
else
{
// FAILED AFTER AJAX
// is it possible to revert the draggable from here??
}
}
});
}
else
{
// FAILED BEFORE AJAX
// is it possible to revert the draggable from here??
}
}
});
I had the same problem,
You can simply do:
item.css({"top":0, "left":0});
tip: draggable has relative position.
Im not too familiar with jquery - but id suggest using the prototype framework (along with scriptaculous) - within prototype you can definately achieve this effect.
If you can guess what it's original position was in the fail block, just use a selector to find it and the manipulation functions to remove it from its current position and move it to the old one.
If you need to, perhaps you can store the original position (parent and child-index) when it is dragged, then move it back there when it fails

Categories