The main reason why people use forms is so that you can define an
action (in your case a php script),
method (GET or POST) and a
submit button that captures all the information in the form and automatically sends it to the server.
This is nice when you have 20-30 inputs or you are handling multiple file inputs. In your case you are handling the data retrieval in your ajax function, you have no submit button, action or method defined so you could make things a lot easier by not using a form at all....
$.ajax({
url: '/new-user.php',
type: 'POST',
dataType: "json",
data: {
name: $('#name').val(),
email: $('#email').val(),
password: $('#password').val()
}
}).done(function(data){
alert(JSON.stringify(data));
});
I've left out the formatting divs for simplicity...
<input type="text" id="name" class="form-control" autofocus="autofocus">
<input type="text" id="email" class="form-control">
<input type="password" id="password" class="form-control">
The above code will grab the data in your inputs, send it to the php script and output the data that is sent back from your php script in the alert.
And most importantly your page will NOT refresh!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…