I changed my coding style for php and jQuery, but my Registration
$("#reg_form_company").bind("submit", function() {
$.fancybox.showActivity();
$.ajax({
type : "POST",
cache : false,
url : $(this).attr('action'),
data : $(this).serializeArray(),
success : function(data) {
$(".printArea").empty().append(data).css('visibility','visible');
}
});
return false;
});
Then this is my Form
<form id="reg_form_company" action="index.php?module=register&actionregister" method="post" >
<input>[...]</input>
</form>
Then after clicking the "Submit" button, it doesn't work, I assume that somebody can help me to solve this problem, coz the $.ajax might confuse about POST(for inputs) and the GET(for the parameters of the "action" form)
I appreciate for your help, you can also modify the entire jQuery code if it's required.
Sorry guys for not including the #reg_form_company, and the fancybox
Answer: Use the jQuery $. post() Method You can simply use the $. post() method in combination with the serialize() method to submit a form using AJAX in jQuery. The serialize() method creates a URL encoded text string by serializing form values for submission. Only "successful controls" are serialized to the string.
ajaxForm({ // dataType identifies the expected content type of the server response. dataType: 'json', // success identifies the function to invoke when the server response. // has been received.
jQuery $.post() Method The $.post() method requests data from the server using an HTTP POST request. Syntax: $.post(URL,data,callback); The required URL parameter specifies the URL you wish to request.
Hi,
the other answers has not worked for me since I needed to pass files inputs and thoses cannot be "serialized".
The good way is to pass it via FormData
and disable processData
:
$('form').on('submit', function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
type : "POST",
cache : false,
url : $(this).attr('action'),
data : formData,
success : (data) => console.log('Data returned from server => ', data),
contentType: false,
processData: false
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With