0

here is the code. showing alert as object form data. My plan is to send the file name through ajax and upload the file in proper path using php. In the first step i want to send the file using jquery. i stuckup here

      <script> 
           $(document).on('click','#btn_photo',function(){

              var fd = new FormData();
              var files = $('#photo')[0].files;
             // Check file selected or not
             if(files.length > 0 )
             {
                 fd.append('file',files[0]);
                 alert(fd);
                $.ajax({
                    url: 'ajax_upload.php',
                    type: 'post',
                    data: fd,
                    contentType: false,
                    processData: false,
                  success: function(response)
                  {
                     if(response != 0)
                     {                   
                        alert(response);
                     }
                     else
                     {
                       alert('file not uploaded');
                     }                 
                 }
             });
         }
         else
         {
             alert("Please select a file.");
         }
     }); 

HTML form is given below:

   <form method="post" enctype="multipart/form-data">   
     <div class="panel panel-info col-lg-4">
       <div class="panel-heading"><b>UPLOADING PHOTO</b></div>
        <div class="panel-body">
            <div class="form-group" id="notext3">
                 <div class='preview'>
                    <img src="" id="photo_disp" width="100" height="100">
                </div>
                <label for="photo">Photo Upload (size < 50Kb only jpg)</label>
                <div class="input-group">
                    <span class="input-group-addon transparent"><span class="glyphicon 
                     glyphicon-cloud-upload"></span></span>
                 <input type="file" name="photo" id="photo" class="form-control" accept=".jpeg,.jpg" required/>
                </div>
            </div>              
       </div>
        <div class="text-center">
            <button type="submit" id="btn_photo" name="submit" class="btn btn-info btn-sm">Upload Photo</button>
        </div><br>
     </div>
   </form>    
nriit
  • 23
  • 3
  • You should at LEAST also show the corresponding HTML. Also: what have you tried, whats not working, error messages etc etc. – Marian Theisen Jan 27 '22 at 10:30
  • ok i will post the hmtl also – nriit Jan 27 '22 at 10:32
  • The AJAX code is fine. You may need to include a call to `preventDefault()` to stop the standard form submission. You've not given details of specifically what isn't working, nor any debugging information, so we can't really help you, other than to say check the console for errors and debug your PHP code. Also, check the duplicate question I marked, as it covers what the PHP code should look like too. – Rory McCrossan Jan 27 '22 at 10:38

0 Answers0