2012-04-09 24 views
0

我需要從通過表單(通過電子郵件發送)以及數據在此處發送的元素標識中獲取的數據(使用jqueryui draggables/droppables/sortables)在表單提交時發送元素標識

 function submitForm() { 
$.ajax({type:'POST', 
     url: 'send_tile.php', 
     data:$('#tiles_collected').serialize(), 
     success: function(response) { 
    $('#tiles_collected').find('.form_result').html(response); 
}}); 

return false; 
    } 

用於獲取ID的功能是這樣的(不知道是否可行權尚未其一):

function getSku() { 
var myIds = new array(); 
     $("#collection li").each(function(index, element){ 
       alert($(this).attr('id')); 
      myIds.push(element.id); 

      }); 

和形式如下:

 <form id="tiles_collected" name='tiles_collected' method="post" onsubmit="return submitForm();"> 
     Email:<input name='email' id='email' type='text'/><br /> 
     Name:<input name='name' id='name' type='text' /><br /> 
     Zip code: <input name='zip' id='zip' type='text' /><br /> 
     <input type='hidden' id='sku' /> 
     <input type="submit" name="submit" value="Email collection to yourself and us" /><br/> 
     <div class="form_result"></div> 
     </form> 

任何人都可以給我一個幫助嗎?

+0

而且你在哪裏使用myIds陣列 – 2012-04-09 11:06:27

回答

0

要將ID添加到發送到服務器的數據中,您可以使用擴展或僅將它們添加到Ajax函數中。

擴展:

function submitForm() { 
    var MyData = $('#tiles_collected').serialize(); 
    $.extend(MyData, {IDs: myIds}); //myIds will have to be accessable, right now it's a local variable in your getSku() function 

    $.ajax({type:'POST', 
     url: 'send_tile.php', 
     data: MyData, 
     success: function(response) { 
      $('#tiles_collected').find('.form_result').html(response); 
     } 
    }); 
    return false; 
} 

或者只是添加它們直接:

function getSku() { 
    var myIds = new array(); 
    $("#collection li").each(function(index, element){ 
     myIds.push(element.id); 
    }); 
    return myIds; 
} 

var IDs = getSku(); 

$.ajax({type:'POST', 
    url: 'send_tile.php', 
    data: {data : $('#tiles_collected').serialize(), ids : IDs}, 
    success: function(response) { 
     $('#tiles_collected').find('.form_result').html(response); 
    } 
}); 
+0

論send_tile.php你的第二個例子,我只是把它稱爲'ids'實際上可以將這些值打印到電子郵件中。 – MirlyMir 2012-04-09 15:07:54

+0

是的,在PHP中,在ajax函數中發送的值在$ _POST ['ids']等中可用。只需執行print_r($ _ POST)來查看它保存的內容或變量轉儲等。 – adeneo 2012-04-09 15:40:43

+0

$('tiles_collected' ).serialize()似乎不再使用該方法,並且我仍然沒有看到任何'ids' – MirlyMir 2012-04-10 11:45:46