I have a form (using PHP & HTML) and my strict requirement is that, I want to upload an image and data to database without refreshing the page. In my form there is some input fields for collecting data, one browse button for picking up image (as input type="file") & a button called submit and when user clicks the submit button I want to call an ajax function to collect data from form and to submit them. (I don't want to refresh the page and hence using ajax call) Please any help...!!!!
Asked
Active
Viewed 1,376 times
-1
-
What have you tried for this? You have just posted your requirement here!!!!!!!!!!! – Vinoth Babu Apr 10 '13 at 05:05
-
I have tried from within form.....and working on this concept...not yet suceeded.... – Caution Continues Apr 10 '13 at 05:06
-
http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/ – open source guy Apr 10 '13 at 05:07
-
where you stuck now? post code here – open source guy Apr 10 '13 at 05:07
-
Look: http://stackoverflow.com/questions/15824177/image-upload-ajax-php-mysql/15824563#15824563 – egig Apr 10 '13 at 05:08
-
@messifan Dude code is very long....this is very small part of the project.....I am not able to do both the thing in just one click...right now I can either upload image or data... – Caution Continues Apr 10 '13 at 05:10
-
@Caution Continues please check my answer it will help you – Praveen kalal Apr 10 '13 at 05:19
-
Possible duplicate of http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery?rq=1 – asprin Apr 10 '13 at 05:29
2 Answers
1
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#myForm').ajaxForm(function() {
alert("Thank you for your comment!");
});
});
</script>
</head>
<body>
<form id="myForm" action="test2.php" method="post" enctype="multipart/form-data">
Name: <input type="text" name="name" />
Comment:<input type="file" name="image">
<input type="submit" value="Submit Comment" />
</form>
</body>
</html>
php file test.php here you process your file upload and DB operation
<?php
echo "<pre>"; print_r($_FILES); exit;
?>

Praveen kalal
- 2,148
- 4
- 19
- 33
0
Use JQuery to submit the form, there is a predefined function called .ajaxForm
$(document).ready(function() {
$('#formName').ajaxForm(function() {
});
});
It will directly contact your action page and you can perform the operation in the controller.

sandy
- 1,126
- 1
- 7
- 17