1
I have a page with job opportunities, when I click on a button it calls a modal and displays a form where when sending the information I need it to not reload the page and display a confirmation msg.
What has been happening is that after sent, it closes the modal and if I click the button to register it loads the sent message.
I’m trying to use Ajax, but I’m not succeeding!
$("#form1").submit(function(event){
// event.preventDefault(); //prevent default action
var post_url = $(this).attr("action"); //get form action url
var request_method = $(this).attr("method"); //get form GET/POST method
var form_data = $(this).serialize(); //En
$.ajax({
url : post_url,
type: request_method,
data : form_data
}).done(function(response){
alert('acabou');
});
});
This form is already sending the registration with Ajax or is reloading the page?
– Sam
It sends the registration and reloads.. If I use Event.preventDefault() it won’t let me post anymore. My button uses type Submit.. if there is no influence.
– Rafael Din
Oh yes, have in question saying that recharges, I did not see right. Well, then in case you convert to Ajax.
– Sam
I think it would be interesting to post the HTML code of your page, we need to know how your modal works and which libraries you are using.
– Thales Chemenian
Thales, I’m working with a cms, it creates everything kind of automatic. I can only manipulate javascript... this is being my difficulty.
– Rafael Din
Sam, when the form is filled and sent, I need it to give me the msg of sent then.. but it closes the modal without displaying. If I go to the button on the first page (apply for a position), it opens the modal with msg
– Rafael Din
You have disabled the
event.preventDefault();
... it will prevent page reloading.– Sam
If I activate it, I cannot send. The button does not send.
– Rafael Din
this is my form <form name="Form1" method="post" enctype="Multipart/form-data" action="/opportunities.aspx" id="Form1">
– Rafael Din
No, preventDefault will only cancel Submit, but the Ajax that comes after will be processed normally.
– Sam
I put an alert, when I am with preventDefault , it shows nothing. Without it shows me the alert.
– Rafael Din
Check the console for any errors. The page to which data is being sent may not be ready to receive data via Ajax, may have redirects etc...
– Sam
Since you said you can’t change pages, only the JS, then I guess there’s nothing to do in your case. To send data via Ajax, the landing page must contain only the codes that will process the data, it cannot be a common page.
– Sam
event.preventDefault()
prevents the normal behavior of the form that issubmit
.– Marconi