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条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么