php message警告:在第0行的Unknown中,multipart / form-data POST数据中缺少边界 [英] php message Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0

查看:143
本文介绍了php message警告:在第0行的Unknown中,multipart / form-data POST数据中缺少边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的javascript

This is my javascript

function ajax_post(){
            // Create our XMLHttpRequest object
            var hr = new XMLHttpRequest();
            // Create some variables we need to send to our PHP file
            var url = "LiveUpdate.php";
            var sb = document.getElementById("LiveUpdate").value;
            var FirstName = document.getElementById("FirstName").value;
            var images = document.getElementById("images").value;

            var vars = "update="+sb+"&FirstName="+FirstName+"&images="+images;
            hr.open("POST", url, true);
            // Set content type header information for sending url encoded variables in the request
            hr.setRequestHeader("Content-type", "multipart/form-data");
            // Access the onreadystatechange event for the XMLHttpRequest object
            hr.onreadystatechange = function() {
                if(hr.readyState == 4 && hr.status == 200) {
                    var return_data = hr.responseText;
                    document.getElementById("message").innerHTML = return_data;
                }
            }
            // Send the data to PHP now... and wait for response to update the status div
            var formData = new FormData(document.querySelector("form"));
            hr.send(formData); // Actually execute the request // Actually execute the request
            document.getElementById("message").innerHTML = "processing...";
        }
            //show image before uploading
                var loadFile = function(event) {
                var output = document.getElementById('output');
                output.src = URL.createObjectURL(event.target.files[0]);
                };//end image

这是表格。 javascript和表单在同一个文件中。文件名是sample.php

and this is the form. javascript and the form is in the same file. the file name is sample.php

<form action="popup.php" method="post" id="myForm" name="myForm" enctype="multipart/form-data">
    <div id="message"></div>
    <img src="" id="output" />
    <input type="file" class="filestyle" data-buttonBefore="true" accept=".png, .jpg, .jpeg" onchange="loadFile(event)" name="images" id="images">
    <input class="form-control" type="text" placeholder="First Name" name="FirstName" id="FirstName" value="">
    <a href="#" class="btn btn-success" role="button" name="update" id="LiveUpdate"  onclick="javascript:ajax_post();">Update</a>
</form>

此代码中的想法是。我想在不刷新页面的情况下将FirstName和Image等基本信息插入到数据库中。我选择标签提交ajax的原因。因此页面URL Account.php?Edit = 1将不会更改为Account.php,因为如果它更改为account.php,则弹出编辑模式将关闭。这段代码中的问题是。我不知道如何修复错误警告:在第0行的未知中的multipart / form-data POST数据中缺少边界任何人都可以帮我解决这个问题。谢谢!

the idea in this code is. i want to insert the basic information like FirstName and Image into the database live without refreshing the page. the reason why i choose tag to submit the ajax. so the page url Account.php?Edit=1 will not change to Account.php because if it change to account.php the popup editing mode will be close. the problem in this code is. i don't know how to fix the error Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0 can anyone help me how to fix this. thank you!

推荐答案

如果您避开,浏览器会设置正确的标题(包括Content-type中正确的多部分边界指示)不要手动指定任何东西。

The browser sets the correct headers (including the correct multipart boundary indication in the Content-type) if you haven't manually specified anything.

所以你需要做的就是删除以下行:

So all you need to do is remove the following line:

 hr.setRequestHeader("Content-type", "multipart/form-data");

否则你需要自己设置边界,正如Ellias Van Ootegem在使用XMLHttprequest上传文件 - 多部分/表格中缺少边界 - 数据

Otherwise you will need to set the boundary yourself as it is suggested by Ellias Van Ootegem in Uploading a file with XMLHttprequest - Missing boundary in multipart/form-data:

hr.setRequestHeader("Content-type","multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2));

这篇关于php message警告:在第0行的Unknown中,multipart / form-data POST数据中缺少边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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