I am working on a website for my app development class and I have the weirdest issue.
I am using a bit of JQuery to send form data to a php page called \'process.ph
The element, when placed in a form, will submit the form automatically unless otherwise specified. You can use the following 2 strategies:
to override default submission behaviorevent.preventDefault() in the onSubmit event to prevent form submissionInsert extra type attribute to your button markup:
Prevent default form submission when button is clicked. Note that this is not the ideal solution because you should be in fact listening to the submit event, not the button click event:
$(document).ready(function () {
// Listen to click event on the submit button
$('#button').click(function (e) {
e.preventDefault();
var name = $("#name").val();
var email = $("#email").val();
$.post("process.php", {
name: name,
email: email
}).complete(function() {
console.log("Success");
});
});
});
In this improvement, we listen to the submit event emitted from the element:
$(document).ready(function () {
// Listen to submit event on the
name attributes to your input:The name attribute is required for .serialize() to work, as per jQuery's documentation:
For a form element's value to be included in the serialized string, the element must have a name attribute.
And then in your JS:
$(document).ready(function () {
// Listen to submit event on the