0

My Ajax script is as follows

Script

<script type="text/javascript">
$(document).ready(function(){

        $('.costExcel').on("submit",function(event){
                event.preventDefault()
                var url = "ajax.php";

                var formData = new FormData(this);

                $.ajax({
                    type:'POST',
                    url: url,
                    data:formData,
                    cache:false,
                    contentType: false,
                    processData: false,
                    success:function(data){
                        console.log("success");
                        console.log(data);
                    },
                    error: function(data){
                        console.log("error");
                        console.log(data);
                    }
                });
            });

        });
</script>

and the Ajax.php looks like this

PHP

if(isset($_POST['ajax_call'])){

    if ( isset($_FILES["file"])) {

            echo "Yo its coming ";
            //if there was an error uploading the file
            if ($_FILES["file"]["error"] > 0) {
                    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
            }
            else {
                    if (file_exists($_FILES["file"]["name"])) {
                            unlink($_FILES["file"]["name"]);
                    }
                            $storagename = "discussdesk.xlsx";
                            move_uploaded_file($_FILES["file"]["tmp_name"],  $storagename);
                            $uploadedStatus = 1;
                }
            } else {
                    echo "No file selected <br />";
            }
}

HTML

<form class="costExcel form-horizontal" role="form" enctype="multipart/form-data">
                        <div class="form-group">
                          <label class="control-label col-sm-2" for="email">Upload :</label>
                          <div class="col-sm-10">
                            <input type="file" class="form-control" id="filex" placeholder="">
                          </div>
                        </div><input type="hidden" value="1" name="step"><input type="hidden" value="1" name="ajax_call">
                        <div class="form-group">        
                          <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-default">Submit</button>
                          </div>
                        </div>
                      </form>

I am using a Bootstrap modal to upload the file , in response I am getting

echo "No file selected
";

Even though I am uploading a file , Can any one tell me where I am doing it wrong

Thanks

Vikram Anand Bhushan
  • 4,368
  • 12
  • 46
  • 118

1 Answers1

1

Add name to your file input field as follows.

<input type="file" class="form-control" id="filex" name="file" placeholder="">

isset($_FILES["file"]) will be working based on the name = 'file'

Subin Thomas
  • 1,350
  • 7
  • 18