weixin_33721427 2018-11-12 14:51 采纳率: 0%
浏览 21

文件上传ajax php发布[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/23980733/jquery-ajax-file-upload-php" dir="ltr">jQuery AJAX file upload PHP</a>
                            <span class="question-originals-answer-count">
                                (6 answers)
                            </span>
                    </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/2320069/jquery-ajax-file-upload" dir="ltr">jQuery Ajax File Upload</a>
                            <span class="question-originals-answer-count">
                                (24 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2018-11-12 14:54:45Z" class="relativetime">2 years ago</span>.</div>
        </div>
    </aside>

How do I use AJAX, Javascript and PHP to POST data AND a file?

I can post the data inputs "name" and "lname", but not the file "myfile".

I have a input page called "input.php" and a post page called "postit.php".

input.php

My html:

<form method="post" name="form" action="postit.php" enctype="multipart/form-data">
    <input type="text" name="name" id="name" placeholder="Name" >
    <input type="text" name="lname" id="lname" placeholder="lname">
    <input type="file" name="myfile" id="myfile" placeholder="myfile">

    <button type="button" onclick="myFunction()">Save</button><br>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

My Javascript:

function myFunction() {
    var name = document.getElementById("name").value;
    var lname = document.getElementById("lname").value;
    var myfile = document.getElementById("myfile").value;
    $.ajax({
        type : "POST",
        url  : "postit.php",
        data : { name : name, lname : lname, myfile: myfile },
        success: function(res){                                      
            setTimeout(function () {location.reload()}, 400);
        }
    });
}

postit.php

<?php 
$name = $_POST['name']; 
$lname = $_POST['lname']; 
$myfile = $_FILES['myfile']['name'];
?>
</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 宇视监控服务器无法登录
    • ¥15 PADS Logic 原理图
    • ¥15 PADS Logic 图标
    • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
    • ¥20 气象站点数据求取中~
    • ¥15 如何获取APP内弹出的网址链接
    • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
    • ¥50 STM32单片机传感器读取错误
    • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
    • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)