Reputation: 5920
I have an HTML form on my site that submits via Ajax to a php file after it is validated using jQuery. Here's the opening form tag:
<form action="#" method="post">
I was thinking of putting the path_to_process_file.php in the form action
(as seen below) so that if they have JavaScript disabled in their browser, they will still be able to fill out and submit the form.
<form action="../path_to_process_file.php" method="post">
My QUESTION:
**UPDATE: Here's my JS used to submit via Ajax.
$( document ).bind( "pageinit", function( event, data ) {
$("#rackback-quote-request-form").validate({
// Custom validation messages
messages: { quote_name: "Please enter your full name."},
errorElement: "p",
submitHandler: function(form){
//Get the data from the form fields and format correctly
var product = $("#rackback-quote-request-form #product").val();
var data = 'product=' + product;
$.ajax({
type: "POST",
url: "../php/QR-rackback.php",
data: data,
success: function(data) {
$("#rackback-quote-request-wrapper").hide();
$("#rackback-quote-request-success").append("<center><h3>Thanks! Your quote request has been sent.</h3></center>" );
},
error: function(){
alert("Javascript error, please try again");
}
});
return false;
}
});
});
Where would I put the event.preventDefault();
?**
Upvotes: 2
Views: 826
Reputation: 9758
JavaScript is first executed, and with it you have the possibility to supress the normal (HTML way) of sending the form.
There are many ways to do this depending on the framework you use, but with simple plain JavaScript you can follow these steps:
Upvotes: 2
Reputation: 8640
Well, your JS will need to attach to the submit event. In that event, you can prevent it from doing what it would naturally do, like this:
$('#form_id').submit(function(event) {
event.preventDefault();
// use jQuery's ajax() method here to do what you want
});
Upvotes: 4