I am trying to upload a file via ajax along with some fields on the form. However, this does not work. I get this error.
Undefined Index: - File
Here is my code.
HTML
<div class="form-group"> <label class="col-md-4 control-label" for="file">Upload Software / File</label> <div class="col-md-4"> <input id="file" name="file" class="input-file" type="file"> </div> </div> <div class="form-group"> <label class="col-md-4 control-label" for="price">Price($)</label> <div class="col-md-4"> <input id="price" name="price" type="text" placeholder="Price" class="form-control input-md" required=""> </div> </div>
Ajax
$("#add_product").click(function(e){ e.preventDefault(); product_name = $("product_name").val(); //d = $("#add_new_product").serialize(); $.ajax({ type: 'POST', url: 'ajax.php', data: $("#add_new_product").serialize(), success: function(response) { // alert(response); } }) });
Php
if (0 < $_FILES['file']['error']) { echo ":!"; } else { echo "ASa"; }
What am I missing here?
javascript jquery ajax php file-upload
Akshay
source share