Looked into this post but didn’t help me im afraid, at the moment I press submit and the page refreshes but nothing is actualy submitted with my ajax call… Can you guys see any error?
$('.formen').on('submit',function(e){
alert("TEST");
e.preventDefault();
$.ajax({
type: "POST",
url: "booked.php",
data: $('.formen').serialize(),
complete: function(data){
$('#namn, #epost').val('');
},
success: function(response){
alert("Complete!");
}
});
});
I tried to put an alert when the form is submitted but nothing happend
book.php
<form class = "formen" autocomplete="off">
<div class="form-group">
<label for="">Name</label>
<input type="text" id = "namn" class="form-control" name="name">
</div>
<div class="form-group">
<label for="">Email</label>
<input type="email" id = "epost"class="form-control" name="email">
</div>
<input type = 'hidden' name = "datum" id = "datum" value = "<?php echo $date; ?>">
<input type = 'hidden' name = "room" id = "room" value = "<?php echo $room; ?>">
<button class="btn btn-primary" type="submit" name="submit">Submit</button>
</form>
And yes the values are defined, I think the problem is my submit function. This is what shows in my url when i press submit .php?name=David&email=Test%40asdsa&datum=2020-05-28&room=1&submit=
What am I missing?
I get the form by clicking on a button which I aswell include with ajax:
$(document).on("click", '.boka', function() {
$.ajax({
url:"book.php",
type:"POST",
data:{'date': $(this).data('date')},
success: function(data){
$("#conf").html(data);
}
});
});
3
Answers
Pass the
event
to yoursubmit
function and prevent the event from submitting the form which is its default functionality like this:P.S.
$('#namn, #epost').val()
condition would only check if the value ofname
is present, if it does the form will get submitted even if theemail
value isnull
Your code lacks document.ready() function.
so this should alert TEST. and works
or you can try another way.
1.) Remove the form elements
2.) Add loader div to show a text during submision
3.) Add result div to display data from server end
4.) add formen variable to class in a submit form
booked.php