duancuan7057 2015-12-25 11:12
浏览 49
已采纳

Isset不适用于ajax调用

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 :

<?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...

$(".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:

<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.

  • 写回答

3条回答 默认 最新

  • duanna2026 2015-12-25 11:28
    关注

    You should send your FormData as a whole data object not a part of another data object. 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']. 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'])){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料