简体   繁体   English

Isset不适用于ajax调用

[英]Isset does not work with ajax call

I am making a simple page where user can upload a image without refreshing the whole page. 我正在制作一个简单的页面,用户可以在不刷新整个页面的情况下上传图像。 But if(isset($_post[oneimgtxt])) is not working.. here is my serverSide Code that upload image : 但是if(isset($_post[oneimgtxt]))无法正常工作..这是我的serverSide代码上传的图片:

<?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);
}
?>

in chrome inspect element i got this.. image and this is my js code with ajax... 在铬检查元素,我得到了这个.. 图像 ,这是我的js代码与ajax ...

$(".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);}
        });
    });

Here is html code: 这是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>

Thanks for any help. 谢谢你的帮助。

You should send your FormData as a whole data object not a part of another data object. 您应该将FormData作为一个完整的数据对象发送,而不是另一个数据对象的一部分。 So, it should be like this - 因此,应该像这样-

$(".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'] . 例如,如果您在表单中有任何名称为inputxt输入,则应该可以使用$_POST['inputxt']进行访问。 And if you have any input type="file" with the name upimage , you need to access through $_FILES['upimage'] . 并且,如果您有任何名为upimage input type="file" ,则需要通过$_FILES['upimage']进行访问。 So, if you want to do isset() for that. 因此,如果您想这样做isset() You can do like this : 您可以这样:

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

add enctype on form any time using file inputs 随时使用文件输入在表单上添加enctype

  <form enctype="multipart/form-data" >
  <input type=file />
  ...
  </form>

and make sure it's always a POST request. 并确保它始终是POST请求。 Good luck...! 祝好运...!

I had headaches for this thing! 我为此事感到头疼! you should use $_FILES['name_of_dom_element']; 您应该使用$_FILES['name_of_dom_element']; in your php code. 在你的PHP代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM