weixin_33711647 2012-08-08 02:50 采纳率: 0%
浏览 28

获取Ajax POST PHP

I am trying to get an ajax file uploader to work, and to pass some additional form data to php. The php script is able to get $_FILES['fileToUpload'] but I'm getting Undefined Index for $_POST["fname"];

PHP

if(isset($_FILES['fileToUpload'])){

$fname = $_POST["fname"];

$name = htmlentities($_FILES['fileToUpload']['name']);
}

JAVASCRIPT

var fd = new FormData();
fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "uploader.php");
xhr.send(fd);

HTML

<form id="form1" name="form1" enctype="multipart/form-data">
                <div id="user">
                    <input type="text" id="fname" name="fname" />
                </div>
                <div id="fileInfo">
                    <div id="fileName"></div>
                    <div id="fileSize"></div>
                    <div id="fileType"></div>
                </div>
                <div class="row"></div>
                <div class="row">
                    <input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();"/>
                </div>
                <div class="row">
                    <input type="button" onclick="uploadFile()" value="Upload" />

  • 写回答

1条回答 默认 最新

  • ℙℕℤℝ 2012-08-08 03:00
    关注

    Because your using ajax to send the Post, you need to do something like

    fd.append("fname", document.getElementById('fname').value);
    

    Otherwise, there's nothing to tell the 'onclick' to send your fname input field.

    Input fields are only sent if you use a submit button.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog