使用formData提交表单后,php中的$ _POST数组为空 [英] $_POST array empty in php after submission of a form using a formData

查看:160
本文介绍了使用formData提交表单后,php中的$ _POST数组为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过几篇有关此问题的文章,但都没有解决我的问题.我正在使用XAMPP,并且在同一文件夹中有一个html index.html 和一个php remote.php .

I've seen several posts about the issue but none of them solved my problem. I'm working with XAMPP and I have an html index.html and a php remote.php in the same folder.

我要发送到php的 formData 是使用javascript中的表单( basic_form )创建的.由于 console.log(... formData); 打印 Array ["mode","basic"],数组["file",File] 正确,它们是字符串和输入文件.

The formData which I want to send to the php is created using a form (basic_form) in javascript. It resulted to be well constructed, since console.log(...formData); printed Array [ "mode", "basic" ], Array [ "file", File ] as expected, which are the string and the input file.

但不幸的是,在php中,命令 print_r($ _ POST); 仅输出 Array(),还输出命令 var_dump($ _ POST); 输出 array(0){} .因此,似乎表单的内容没有传递给php.

But unfortunately in the php the command print_r($_POST); outputs only Array() and also the command var_dump($_POST); outputs array(0){}. So it seems that the content of the form is not passed to the php.

这是html格式:

<body>
    <div>
        <form name="basic_form" enctype="multipart/form-data" action="remote.php" method="post">
            <input type="text" id="mode" name="mode" value="basic"/>
            <input type="file" id="file" name="file"/>
            <input type="submit" id="submit_basic_input" name="submit_basic_input" value="Submit"/>
        </form>
    </div>
</body>

使用JavaScript提交表单:

Here the javascript to submit the form:

<script>
    var basic_form = document.forms["basic_form"];
    basic_form.addEventListener('submit', e => {
        e.preventDefault(); 

        const url = 'remote.php';
        const formData = new FormData(basic_form);

        fetch(url, {
                method: 'POST',
                body: formData
            }).then(response => {
                console.log(response);
                if(response["status"] == 200)
                    location.replace(response["url"]);
        });
                
    });
</script>

这里的php打印表单内容:

Here the php to print the content of form:

<?php
    var_dump($_POST);
    $datapost = $_POST;
    print_r($datapost);
    print($datapost["mode"]);
?>

推荐答案

首先,您调用 fetch 发出POST请求

First, you call fetch which makes a POST request

获得响应后,您将记录响应对象(其中没有任何有用的东西,特别是响应的主体将显示为可读流而不是字符串).

When you get a response, you log the response object (which doesn't hold anything all that useful, in particular the body of the response will show up as a readable stream and not a string).

该响应显示 array(1){["mode"] =>string(5)基本"}虽然是Array([mode] => basic)basic ,但是您可以使用浏览器开发人员工具的网络"标签看到它.

That response does show array(1) { ["mode"]=> string(5) "basic" } Array ( [mode] => basic ) basic though, you can see it using the Network tab of the browser's developer tools.

记录响应后,您可以设置 location.replace(response ["url"]); ,该请求发出GET请求(到相同的URL)并浏览到该浏览器.

After logging the response you set location.replace(response["url"]); which makes a GET request (to the same URL) and navigates the browser to it.

GET请求是一个不同请求,并且没有来自POST请求的请求正文.

The GET request is a different request and does not have the request body from the POST request from it.

由于它是一个不同的请求,因此它得到的响应也不同,现在 $ _ POST 是一个空数组.

Since it is a different request, it gets a different response and now $_POST is an empty array.

如果要导航到响应,请放弃JavaScript.只需使用表单发出POST请求,然后让浏览器呈现结果页面即可.

If you want to navigate to the response then get rid of the JavaScript. Just make the POST request with the form and let the browser render the resulting page.

如果要使用Ajax发出请求,则:

If you want to use Ajax to make the request then:

  • 不要立即离开页面
  • 对响应做一些有用的事情(首先调用 response.text()从响应主体中获取数据,然后也许使用 createElement appendChild 和朋友将数据添加到当前文档中)
  • Don't immediately navigate away from the page
  • Do something useful with the response (starting by calling response.text() to get the data out of the response body and then perhaps using createElement, appendChild and friends to add the data to the current document)

这篇关于使用formData提交表单后,php中的$ _POST数组为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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