javascript - Isset 不适用于 ajax 调用

标签 javascript php jquery ajax file-upload

我正在制作一个简单的页面,用户可以在其中上传图像而无需刷新整个页面。但是 if(isset($_post[oneimgtxt])) 不起作用.. 这是我上传图像的服务器端代码:

<?php
$maxmum_size = 3145728; //3mb 
$image_type_allowed = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if(isset($_POST["oneimgtxt"])){//<!------------------ this line is not working
        if((!empty($_FILES[$_FILES['upimage']['tmp_name']])) && ($_FILES["upimage"]['error'] == 0)){
            $file=$_FILES['upimage']['tmp_name'];
            $image_count = count($_FILES['upimage']['tmp_name']);
            if($image_count == 1){
                $image_name = $_FILES["upimage"]["name"];
                $image_type = $_FILES["upimage"]["type"];
                $image_size = $_FILES["upimage"]["size"];
                $image_error = $_FILES["upimage"]["error"];
                if(file_exists($file)){//if file is uploaded on server in tmp folder (xampp) depends !!
                    $filetype =exif_imagetype($file); // 1st method to check if it is image, this read first binary data of image..
                    if (in_array($filetype, $image_type_allowed)) {
                        // second method to check valid image
                        if(verifyImage($filename)){// verifyImage is function created in fucrenzione file.. using getimagesize
                            if($ImageSizes < $maxmum_size){//3mb 
                                $usr_dir = "folder/". $image_name;
                                move_uploaded_file($file, $usr_dir);
                            }else{
                                $error_container["1006"]=true;
                            }
                        }else{
                            $error_container["1005"]=true;
                        }
                    }else{
                        $error_container["1004"]=true;
                    }
                }else{
                    $error_container["1003"]=true;
                }
            }else{
                $error_container["1002"]=true;
            }
        }else{
            $error_container["1007"]=true;
        }
    }else{//this else of image issset isset($_POST["oneimgtxt"])
        $error_container["1001"]=true;//"Error during uploading image";
    }
    echo json_encode($error_container);
}
?>

在 chrome 检查元素中我得到了这个.. image 这是我使用 ajax 的 js 代码...

$(".sndbtn").click( function(e){
        var form = $("#f12le")[0];
        var formdata = new FormData(form)
        $.ajax({
            type:'POST',
            //method:'post',
            url: "pstrum/onphotx.php",
            cache:false,
            data: {oneimgtxt : formdata},
            processData: false,
            contentType: false,
            success:function (e){console.log(e);}
        });
    });

这是html代码:

<form  method="post" id="f12le" enctype="multipart/form-data">
   <input type="file" name="upimage"/>
   <label for="imgr">Choose an Image..</label>
   <textarea placeholder="Write something about photo"></textarea>
   <input   type="button" name="addimagedata" value="Post" class="sndbtn"/>
</form>

感谢您的帮助。

最佳答案

您应该将 FormData 作为整个数据对象而不是另一个数据对象的一部分发送。所以,应该是这样的 -

$(".sndbtn").click( function(e){
    var form = $("#f12le")[0];
    var formdata = new FormData(form)
    $.ajax({
        type:'POST',
        //method:'post',
        url: "pstrum/onphotx.php",
        cache:false,
        data: formdata,
        processData: false,
        contentType: false,
        success:function (e){console.log(e);}
    });
});

Now, you should be able to access the form as it is. For example if you have any input with name inputxt inside the form, you should be able to access it with $_POST['inputxt']. And if you have any input type="file" with the name upimage, you need to access through $_FILES['upimage']. So, if you want to do isset() for that. You can do like this :

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

关于javascript - Isset 不适用于 ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34462353/

相关文章:

php - 如何根据分配给每个页面的类别为 wordpress 菜单列表着色?

php - 如何更改 dokku PHP 内存限制?

javascript - 如何关注具有相同类的 img

javascript - 使用YouTube和非YouTube链接解析正则表达式URL

javascript - 当它是包含在多个页面中的单独的 html 文件时,在页眉中显示页面标题

javascript - 如何从 JSON 文件中的属性在 Vue.js 中生成唯一 ID?

php - 为什么我不能在 php mysql 中显示我的记录?

jquery - 如何在 jQuery 中访问 Rails 2 flash[] 哈希值?

javascript - 页面重新加载时 float 导航超出边界

javascript - jQuery 在 Blogger 中不起作用