-1

I made changes to my code

<html>
<body>
<form action="test.php" id="subscriber_form" method="post" enctype="multipart/form-data">
    <input type="text" id="title" name="title" ><br>
    <input type="file" id="file" name="file" ><br>
    <input type="submit" id="submit" value="submit">
    </form>
    <div id="show_subscriber_msg"></div>
<script src="js/jquery-1.7.1.min.js"></script>               

<script>  
$("form#subscriber_form").submit(function(){
$('#show_subscriber_msg').html('<div class=gen>Submiting..</div>');
e.preventDefault();
var formURL = $(this).attr("action");
var formData = new FormData($(this)[0]);
$.ajax({
url: formURL,
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false,
async: false,
success: function (res) {
                             if(res=='1'){
                                $('#show_subscriber_msg').html('<div class=gen>Thank you</div>');
                             }

                             if(res=='5'){
                             $('#show_subscriber_msg').html('<div class=err>Please enter a valid email address</div>');
                             }
                        }
});

});

 </script>

</body>
</html>

My sql table is getting updated and file is uploaded to the directory correctly But it's still redirecting me to test.php. I just want to run my php script i.e test.php in background.

Akshay N Shaju
  • 355
  • 4
  • 17
  • Also, this answer will probably help you in finding a solution, [http://stackoverflow.com/a/38112505/5517143](http://stackoverflow.com/a/38112505/5517143) – Rajdeep Paul Jul 02 '16 at 09:59

1 Answers1

2

You have to make changes in your form element change the encoding type to multipart/form-data

<form id="subscriber_form" method="post" enctype="multipart/form-data">

Your ajax call will be like this.

$("form#subscriber_form").submit(function(e){ 
  e.preventDefault();
  var formURL = $(this).attr("action");
  var formData = new FormData($(this)[0]);
  $.ajax({
    url: formURL,
    type: 'POST',
    data: formData,
    cache: false,
    contentType: false,
    processData: false
    async: false,
    success: function (data) {
        console.log(data);
    }
});

As far as backend part is concerned, I am not much into PHP. If you want Java code I can help you with that.

Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74