Good day. I send an ajax request to a PHP file and call it back in the hope of getting a page filled with the data I sent. However, PHP, instead of data, returns 0.

Ajax request:

$('#krok-form').submit(function(e) { e.preventDefault(); e.stopPropagation(); var form_data = $(this).serialize(); $.ajax({ type: 'POST', url: 'receipt/krok-receipt.php', data: form_data, success: function(data) { console.log('Data was sent successfuly'); $('#blank-frame').html(data); $('#blank-frame').css({'padding' : '15px'}); $('.must-hide').hide('slow', function(){ $(this).detach(); }); } }); }); 

The PHP file accepts data as follows:

 <?php $username = $_POST['user-fullname']; $address = $_POST['client-address']; $contract = $_POST['contract-id']; $amount = $_POST['amount-payment']; echo 'Приняты данные: \n' + $username + '\n' + $address + '\n' + $contract + '\n' + $amount; ?> 

The echo function prints 0.

    2 answers 2

    All because you are trying to add a string as numbers, while the lines are trying to convert to a number and become zeros.

    The string concatenation operator in PHP serves . (point).

    Your last line should be:

     echo 'Приняты данные: \n' . $username . '\n' . $address . '\n' . $contract . '\n' . $amount; 

    And as an option, you can use the record type:

     echo implode("\n", array ('Приняты данные:', $username, $address, $contract, $amount)); 

    Then each parameter passed in the array will be converted to a string separated by the symbol specified by the first parameter.

    In this case, the newline character \n

    • one
      Either echo implode("\n", array ('Приняты данные:', $username,$address,$contract,$amount)); The essence is the same, but in other cases it is shorter. - Evgeny Borisov
    • @YevgenyBorisov is an excellent solution for writing, if each line (as in this case) needs to be moved, I will add in response - Vasily Barbashev
    • @YevgenyBorisov yes, now the line "Data is received:" is displayed and that's it. My data transmitted via ajax request is not displayed. And most likely, the problem is that the data is sent from a modal window, the contents of which are also loaded using ajax. I could be mistaken, of course, but I suffer with this problem already for 24 hours. - JamesJGoodwin
    • @JamesJGoodwin see what comes to print_r($_POST) . There is most likely a string in json format. And you will need to json_decode using json_decode - Vasily Barbashev
    • @ Vasily Barbashev displays an empty array. Array () - JamesJGoodwin

    Do you have exactly such values ​​of the name attribute? And if I were you, I would look at the result of print_r ($ _ POST)

    What does he do?