Answer the question
In order to leave comments, you need to log in
AJAX request not coming to php handler?
Hello, there was a problem, I created a registration form using an ajax request, everything seemed to go according to plan, but alas ...
The problem is this - when I press the button it should register, but it just displays an alert and that's it, that is, the php code is not processed as only I will remove the line event.preventDefault(); in the ajax request; then everything works, only opens a new page.
What can be wrong?
here is the html
code :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<form method="POST" action="reg.php">
<p><input type="text" name="firstname" value="" placeholder="Ваше Имя" style="text-align: center;">
<p><input type="text" name="surname" value="" placeholder="Ваша Фамилия" style="text-align: center;">
<p><input type="submit" name="btn" id="btn-c" value="KKK">
</form>
<script type="text/javascript">
$(document).ready(function(){
$("form").submit(function(){
event.preventDefault();
$.ajax({
type:$(this).attr('method'),
url: $(this).attr('action'),
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function(result){
alert(result);},
});
});
});
</script>
<? require_once('db/db-connect.php');
if(isset($_POST['btn'])){
$cat = R::dispense('userlists');
$cat->firstname=$_POST['firstname'];
$cat->surname=$_POST['surname'];
R::store($cat);
echo "OK!";
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question