如何从FormData获取PHP中的数据和文件 [英] How get data and files in PHP from FormData

查看:573
本文介绍了如何从FormData获取PHP中的数据和文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Ajax将数据(输入文本,下拉列表等)和文件发送到PHP文件.我使用此函数添加名为 Action 的额外参数.动作可以是其中的一些文本:添加",编辑",读取"和删除"(压缩选项).参见下面的脚本:

I'm trying send data (input text, dropdown list, etc) and files to PHP file through Ajax. I use this function to add extra parameter named Action. Action can be some of these texts: "add", "edit", "read" and "delete" (crud options). See the script below:

function recordActions(action_name, id) {
  //id = (typeof id == "undefined") ? '' : id;

  var frm = document.getElementById(action_name + '_form');
  var form_data = new FormData();

  form_data.append('action', action_name);
  form_data.append('fd', frm);

  $.ajax({
          type: 'post',
          dataType: 'json',
          url: '<?php echo FILENAME_USERS_ACTIONS; ?>',
          data: form_data, 
          cache: false,
          processData: false,
          contentType: false,
          success:
            if (data.action == 'add' || data.action == 'edit') {
                $("#" + action_name + '_form')[0].reset();
                $("#" + action_name + '_div').slideUp();
            }
            showWeekAgenda();

            }          
      });

      // esta línea evita que la página se refresque dando a Cancelar la visita y evita que salga Error en Success de Arriba
      if (action_name == 'cancel') return false; 
    }

当Ajax调用PHP文件()时,我不知道如何访问FormData中包含的数据. 在Web Developer中看到了Params,我明白了:

When the Ajax call the PHP file (), I don't know how to access the data contains in FormData. Seeing the Params in Web Developer, I got it:

-----------------------------81668061412059330971865480216
Content-Disposition: form-data; name="action"

actadd
-----------------------------81668061412059330971865480216
Content-Disposition: form-data; name="fd"

[object HTMLFormElement]
-----------------------------81668061412059330971865480216--

然后,我在PHP中放入一些代码来查看参数及其值:

Then, I put some code in PHP to see the params and their values:

print_r($_POST);
print_r($_FILES);

echo '<br>Post: ' . $_POST['fd'];

但我无能为力.

Array
(
    [action] => actadd
    [fd] => [object HTMLFormElement]
)
Array
(
)
<br>Post: [object HTMLFormElement]

任何人都知道如何访问 fd 内的任何值吗? fd应该具有我的输入文本,下拉菜单,文本区域等的值.

Anyone knows how to access to any value inside of fd? fd should have values of my input texts, dropdown, textarea, etc.

在回答此问题后:同时上传这两者可以使用Ajax以一种形式存储数据和文件吗?我们可以将所有HTML控件都放在一个FormData中.

Following the answer of this question: Uploading both data and files in one form using Ajax? we can put all HTML Controls in one FormData.

预先感谢!

推荐答案

创建

When you create your FormData object, you should pass the form to the constructor to get it populated with the forms values. You can then append the action value to that result:

var frm = document.getElementById(action_name + '_form');
var form_data = new FormData(frm);
form_data.append('action', action_name);

以这种方式执行操作时,您应该在PHP代码中正确看到$_POST$_FILES.

When you do it this way you should see $_POST and $_FILES populated correctly in your PHP code.

这篇关于如何从FormData获取PHP中的数据和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆