hello stackoverflow team, i am using a button with onclick
function on it which make call to an ajax and that ajax populate a form in a div under that button. In the form there are various text fields and one input type=file
now i want to upload this file using ajax and side by side upload the data into data base. I can upload the data to the database using ajax, but i am unable to upload the file using ajax. Here is my js code
var selectedCategory = $("#category option:selected").val();
var prod_name = $("#prod_name").val();
var prod_code = $("#prod_code").val();
var price = $("#price").val();
var age = $("#age").val();
var description = $("#description").val();
var img_path = $("#image_path").val();
var formValues = "&selectedCategory="+selectedCategory+
"&prod_name="+prod_name+
"&prod_code="+prod_code+
"&price="+price+
"&age="+age+
"&description="+description+
"&image_path="+img_path;
alert(formValues);
uploadData(formValues);
$.ajax({
url: "UploadFile",
type: 'POST',
data: img_path,
success: function (data)
{
$("#panel_body").html(data);
//showProducts();
}
});
}
Here is my form that is created using ajax call to my servlet:
<table cellpadding="5" cellspacing="5" style="margin: 0 auto;" id="add_record_table">
<form action="UploadImage" method="post" enctype="multipart/form-data"></form>
<tbody><tr>
<td><label>Select Category</label></td>
<td>
<select name="category" id="category"><option id="0" value="-1">--Select category--</option><option id="1" value="Alphabets and Numbers">Alphabets and Numbers</option></select>
</td>
</tr>
<tr>
<td>
<label>Product Name</label>
</td>
<td>
<input type="text" name="prod_name" id="prod_name" placeholder="Product Name">
</td>
</tr>
<tr>
<td>
<label>Product Code</label>
</td>
<td>
<input type="text" name="prod-code" id="prod_code" placeholder="Produce Code">
</td>
</tr>
<tr>
<td>
<label>Price</label>
</td>
<td>
<input type="text" name="price" id="price" placeholder="Price">
</td>
</tr>
<tr>
<td>
<label>Recomended Age</label>
</td>
<td>
<input type="text" name="age" id="age" placeholder="Recomended Age">
</td>
</tr>
<tr>
<td>
<label>Description</label>
</td>
<td>
<textarea id="description" rows="5" cols="50" placeholder="Description" style="resize: vertical; max-height: 250px;"></textarea>
</td>
</tr>
<tr>
<td>
<label>Upload Image</label>
</td>
<td>
<input type="file" name="image_path" id="image_path" accept="image/jpeg">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" name="submit" onclick="validateForm();" class="btn btn-primary btn-large" value="ADD" id="add_data">
</td>
</tr>
</tbody></table>
Please assist me to get through this problem.