douzong5057 2014-06-27 07:27
浏览 27
已采纳

使用Ajax将图像传递给PHP

I am trying to pass an image and a title field value to PHP, I usually process file uploads straight with PHP using the $_FILES array, I am not sure how to create/pass this array using ajax to PHP. My form:

<form role="form" name="updateProductForm" id="updateProductForm" method="POST" enctype="multipart/form-data"> 
    <input name="imgOne" id="imgOne" type="file" title="Add Image">
    <a class="btn btn-primary" name="updateProduct" id="updateProduct">Update Product</a></div>
</form>

And I am trying to use this to pass to PHP:

$('#updateProduct').on('click', function() {
    try {
        ajaxRequest = new XMLHttpRequest();
    } catch (e) {
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert("Your browser broke!");
                return false;
            }
        }   
    }
    ajaxRequest.open("POST", "../Controller/ProductController.php", true);
    ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    ajaxRequest.send("title=" + $("#title").val() + "&imgOne=" + $("#imgOne"));

    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            console.log(ajaxRequest);
        }
    }

});

In PHP I am trying this:

if (isset($_POST["edit"])) {
    $id = $_POST["edit"];
    $imgName = $_FILES["file"]["imgName"];
}

Youssef

  • 写回答

3条回答 默认 最新

  • dongxiong1935 2014-06-27 07:47
    关注

    I did that in one of my project, and following code works for me. Please do required modifications in code as your need.

    My Form button:

     <form name="upload_img" enctype="multipart/form-data" id="upload_img">
    <input type="file" style="display:none" id="upload-image" name="upload-image"></input>
    <button id="upload_image" type="button">Save</button>
    </form>
    

    My JQuery/Ajax :

    $('#upload_image').click(function()
    {
        var form = new FormData(document.getElementById('upload_img'));
        //append files
        var file = document.getElementById('upload-image').files[0];
        if (file) {   
            form.append('upload-image', file);
        }
        $.ajax({
            type: "POST",
            url: "URL",
            data: form,             
            cache: false,
            contentType: false, //must, tell jQuery not to process the data
            processData: false,
            //data: $("#upload_img").serialize(),
            success: function(data)
            {
                if(data == 1)
                    $('#img_msg').html("Image Uploaded Successfully");
                else
                    $('#img_msg').html("Error While Image Uploading");
            }
        });
        //alert('names');
    
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了