There are 2 fields and a button for sending data to ajax server

Here is the JavaScript :

 $('#find-btn').click(function () { if($('#from').val() == 0 && $('#to').val() == 0) { alert("Вы не заполнили поля для поиска!"); return; } else { var findFrom = $('#from').val(); var findTo = $('#to').val(); var data = 'from=' + findFrom + '&to=' + findTo; console.log(data); /*Данные записались*/ $.ajax({ type: 'POST', url: './php/search.php', data: data, error: function () { alert("error on ajax search"); /*Ошибок нет*/ } }) } return false; }) 

Here is the PHP :

 var_dump($_POST] //array(0) { } if(isset($_POST['from']) and (isset($_POST['to']))) { echo 'yes!'; /*Тут по прежнему false*/ } 

var_dump($_POST) also indicates that there is nothing in the array.

  • 2
    Wrong data variable format, try it like this var data = { from: findFrom, to: findTo } - S. Pronin
  • one
    you are trying to transfer the data as if they were $_GET , but you accept as $_POST - Vasily Barbashev
  • @ S.Pronin both formats are equivalent - Pavel Mayorov
  • @ Vasily Barbashev do not confuse the person. Correctly he transmits. The error is definitely not in it. - Pavel Mayorov
  • I forgot to say that this is all on the server, and not on localhost. Maybe because of this problem? - Pashtet

1 answer 1

Your case:

Javascript :

 $('#find-btn').click(function () { if($('#from').val() == 0 && $('#to').val() == 0) { alert("Вы не заполнили поля для поиска!"); return; } else { var findFrom = $('#from').val(); var findTo = $('#to').val(); var data = { from: findFrom, to: findTo }; console.log(data); /*Данные записались*/ $.ajax({ type: 'POST', url: './php/search.php', data: data, success: function(response){ alert("response from search"); console.log(response); }, error: function () { alert("error on ajax search"); /*Ошибок нет*/ } }) } return false; }) 

PHP :

 include('dbconnection.php'); if(isset($_POST['from']) and (isset($_POST['to']))) { echo 'yes!'; /*Тут false*/ } 

As Pavel Mayorov replied , both options are identical:

 var data = { from: findFrom, to: findTo }; var data = 'from=' + findFrom + '&to=' + findTo; 

Simulated the situation, worked: enter image description here

For the lazy, you can play like this:
test.php

 <script src="https://code.jquery.com/jquery-1.12.3.min.js" integrity="sha256-aaODHAgvwQW1bFOGXMeX+pC4PZIPsvn2h1sArYOhgXQ=" crossorigin="anonymous"></script> <button>ЖМАКНИ</button> <script> $('button').click(function () { var findFrom = 'findFrom_value'; var findTo = 'findTo_value'; var data = 'from=' + findFrom + '&to=' + findTo; console.log(data); /*Данные записались*/ $.ajax({ type: 'POST', url: '/test2.php', data: data, success: function(response){ alert("response from search"); console.log(response); }, error: function () { alert("error on ajax search"); } }) }) </script> 

test2.php

 <?php if( isset($_POST['from']) and (isset($_POST['to'])) ) { echo 'yes!'; /*Тут false*/ } else echo 'no!'; 

The question is, if something is marked as general, do not be shy, add, edit.

  • thanks for your reply! everything exactly on the php file var_dump ($ _ POST) gives array (0) {} - Pashtet
  • added to the question - Pashtet
  • Updated the answer - add the success section. Most likely it will be response from search , since you say that the error does not work. Look in the log, there should be an answer from the server. What returned? - borodatych