简体   繁体   中英

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 :

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

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'])){

add enctype on form any time using file inputs

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

and make sure it's always a POST request. Good luck...!

I had headaches for this thing! you should use $_FILES['name_of_dom_element']; in your php code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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