无法从PHP文件中的AJAX访问序列化数据

时间:2015-01-11 20:47:48

标签: ajax wordpress

尝试在Wordpress插件中获取发送数据,但我无法使数据正常工作。那么如何从php文件中的序列化部分获取数据呢?

//代码

<form method="post" id="form-settings" action="ajaxpro.php" >  

  <?php wp_nonce_field( 'settings', 'settings_nonce', false );?> 

  <input    name="field-one" type="text"/>
  <input    name="field-two" type="text"/>
  <textarea name="field_three">Hello world</textarea>

  // notice the double fields

  <input name="field-x[]" type="text"/>
  <input name="field-x[]" type="text"/>
  <input name="field-x[]" type="text"/>
  <input name="field-x[]" type="text"/>
  <input name="field-x[]" type="text"/>

  <button id="submitme" name="save-form">Save</button>

</form>



$('body').on('submit', '#form-settings', function(e){

  var seri =$(this).serializeArray()
   $.post(ajaxurl, {
       action:  'save_ajax_func',
       posted: seri
   }, function(response) {
       console.log( response );     
   });


   return false;

});


//notice that I do use double field names(field-x)

echo $_POST[ 'posted' ][ 'field-one' ] // not working

1 个答案:

答案 0 :(得分:0)

要在html中使用[]表示法,它将是posted[field-x]

  <input name="posted[field-x]" type="text"/>

ajax必须改为

var seri = $(this).serializeArray();
seri.push({name:'action',value:'save_ajax_func'});
$.post(ajaxurl, seri, function(response) {
       console.log( response );     
});