2012-05-11 58 views
0

如何使用JSON发送多个值以便像这样拾取?我如何将此更改为JSON?

foreach($_POST['listItem'] as $key => $values){ 

    list($eventDate, $eventTitle) = $values; 

} 

目前的值发送如下。但是这似乎不能正常工作,因为它只发送一些字符串。我也无法弄清楚在哪里/如何发送标题值。

这让我发疯。任何帮助都会很棒。

var txt = $("#listbox"); 

      var caltitle = copiedEventObject.title 

      var dtstart = $.fullCalendar.formatDate(copiedEventObject.start, 'yyyyMMdd'); 

      var txt = $('#listbox');  
      txt.append("<li class ='listItem'> " + dtstart + "</li>") 

      // remove the element from the "Draggable Events" list 
      $(this).remove();   

     } 
    }); 


     $('#calendarform').submit(function(e) { 
      var form = $(this); 
      var listItems = $('.listItem'); 
      listItems.each(function(index){ //For each event do this: 
       var listItem = $(this); 
        $("<input type='hidden'/>").val(listItem.text()).appendTo(form).attr('name', 'listItem[' + index + ']'); 
       }); 

      //Get text information from elements 

     }); 

编辑:

(使用JSON)

$('#calendarform').submit(function(e) { 
      var form = $(this); 

      var going_to_be_json = []; 
      list.each(function(i, item) { 
       going_to_be_json.push({ 
       'id':'test', 
       'title': 'test' 
       }); 
      }); 

      $('#stuff').val(JSON.stringify(going_to_be_json)); 

    }); 
+0

foreach中的$ values变量是什么样的?它是一个数组还是一个字符串? jQuery代码应该帮助我们什么? – shadyyx

+0

嗯,即时尝试制作一个数组。基本上我有一个日期和标题,我需要进入操作页面的foreach。我需要修改隐藏的输入来让它们都穿过,而我无法解决它。 –

+1

不需要在''.attr('name','listItem []');''中使用''index''变量。像这样尝试。 – Valour

回答

1

将数据准备到对象数组中,然后使用JSON.stringify()将该数组转换为JSON并设置隐藏表单va指向所述字符串(或使用XHR将其提交给服务器)。

下面是如何准备数据非常有限的例子:

var going_to_be_json = []; 
list.each(function(i, item) { 
    going_to_be_json.push({ 
    'id':item.id, 
    'title': item.title, 
    'date': (new Date()).now() 
    }); 
}); 
hidden_form_element.val(JSON.stringify(going_to_be_json)); 

和服务器上

<?php 
$json_data_string = $_POST['hidden_form_field_containing_json_data']; // sanitize however 
$array_data = json_decode($json_data_string); 

更多可从json.orgthe Tag Wiki准备约JSON(JavaScript对象符号) (虽然标签维基只是向你展示更多的例子和链接)

它似乎你需要一点点方向。我希望你不会复制和粘贴我所拥有的东西,并且你会阅读并试图理解它在做什么......

$('#calendarform').submit(function(e) { 
     var form = $(this), 
      listItems = $('.listItem'), 
      data = [], 
      hidden = $('<input />'); // create a hidden field for the form. 
            // or just select the hidden form element from the page 
     hidden[0].type = 'hidden'; // set the type 
     hidden[0].name = 'some_name_for_php_to_recognize'; // and name 

     listItems.each(function(i,el){ 
      data.push({'index': i, 'text': $(el).text()}); 
     }); 

     hidden.val(JSON.stringify(data)); 
     $(this).append(hidden); // not needed if the hidden element is already in the DOM 

    }); 
+0

似乎无法让它发送任何东西?我试图发送'id:'test', –

+0

你如何提交数据? – rlemon

+0

我用代码编辑了这个问题。表单中的隐藏字段的id为'stuff' –

0

与jQuery邮寄,或获取方法和PHP读取它,你可以发送多个值 使用$.post$.get

相关问题