-1

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...!!!!

Caution Continues
  • 743
  • 2
  • 10
  • 25

2 Answers2

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