I'm trying to call a php file using the $.ajax() function in jQuery however it is not working. The following code is run when a button on the page is clicked:
if($error === false) {
alert($error);
$.ajax({
url: '/new-user.php',
type: 'POST',
data: {
name: $('#name').val(),
email: $('#name').val(),
password: $('#name').val()
}
});
This is my form:
<form onClick="return false;" class="reg-form">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="name">First Name</label>
<input type="text" id="name" class="form-control" autofocus="autofocus">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="email">Email Address</label>
<input type="text" id="email" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" class="form-control">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="password-confirm">Confirm Password</label>
<input type="password" id="password-confirm" class="form-control">
</div>
</div>
</div>
<button class="btn trans reg-btn">Sign Up</button>
<div class="reg-msg">
<span id="password-error-msg">Passwords do not match.</span>
</div>
</form>
I have set the form's onClick to return false so that the form does not reload the page when submitted so that the jQuery can run.
Any help is greatly appreciated.
See Question&Answers more detail:os