S
S
Sergey Bondarenko2019-02-16 18:11:53
AJAX
Sergey Bondarenko, 2019-02-16 18:11:53

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>

php:
<? 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!";
}

I also use RedBeanPHP
Thanks in advance for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ao, 2019-02-16
@darknefrit

in php print before if
and alert will show you what comes in php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question