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

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

So data: formData will automatically fetch post data from my form, including file ... am i right ?
Yes. select it as answer if you found it useful.
My page is getting refreshed when i click on submit.... i wanna post data in the background .. i mean without refreshing my page .
add e.preventDefault();
made changes in a answer, do check.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.