0

index.html I am submitting this form using ajax and i want all form post data in controller to submit form in database.But its not working.

<form id="uploadproImage" enctype="multipart/form-data">
     <div class=" form-group">
             <label>Select Image</label>
             <input type="file" name="product_file" multiple />
             <span class="help-block">Allowed File Size - 1MB MAX.</span>
             <span class="help-block">Allowed File Type - webp.</span>
      </div>
     <button type="submit" name="btnupload">Upload</button>

 </form>

main.js

$('#uploadproImage').on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'appcode/product.php',
            data: new FormData(this),
            dataType: 'json',
            contentType: false,
            cache: false,
            processData: false,
            beforeSend: function () {

            },
            success: function (response) {
                alert(response);
                // if (response.status == 1) {
                //     $('#uploadproImage')[0].reset();
                //     alert(response);
                // } else {
                //     alert(response);
                // }
            }
        });
    });

product.php

 
if(isset($_POST['btnupload'])){
    if(!empty($_FILES["product_file"]["name"])){

    
    }else{
        echo 'File Not Selected'; 
    }
}else{
    echo 'Sorry, there was an error uploading your file.'; 
}

Output: 'Sorry, there was an error uploading your file.'

But I Want To 'File Not Selected'

  • Why are you checking if `btnupload` is set? Isn't enough to just check if file isn't set – noah1400 Feb 08 '23 at 15:06
  • I was simply checking whether my form are submitted as post or not. – shilpa Kukreja Feb 08 '23 at 15:11
  • You submitted the request via AJAX, so buttons are not included in the submitted form data, as far as I know. That only happens if you submit the form directly. Demo: https://jsfiddle.net/mx7r631h/ – ADyson Feb 08 '23 at 15:12
  • You can check this by checking `$_SERVER['REQUEST_METHOD']` variable: `$_SERVER['REQUEST_METHOD']=== 'POST'` – noah1400 Feb 08 '23 at 15:13
  • Does this answer your question? [FormData doesn't include value of buttons](https://stackoverflow.com/questions/48322876/formdata-doesnt-include-value-of-buttons) – ADyson Feb 08 '23 at 15:15
  • Does this answer your question? [Detecting request type in PHP (GET, POST, PUT or DELETE)](https://stackoverflow.com/questions/359047/detecting-request-type-in-php-get-post-put-or-delete) – noah1400 Feb 08 '23 at 15:15
  • thank you, everyone, I understand my problem, and after your comment, My issue is solved. thanks a lot...:) – shilpa Kukreja Feb 08 '23 at 15:45

0 Answers0