G
G
GrandinaWi2021-03-29 11:13:48
AJAX
GrandinaWi, 2021-03-29 11:13:48

How to get data from PHP in Ajax?

I'm trying to pass data from PHP to Ajax. But it is not possible to display the result of PHP processing.
HTML code where the result should be displayed JS code
<div class="title-form" id="result"></div>

$("#bronbtn").click(function(event)
{
   event.preventDefault();
   sendAjaxForm('result','ajax_form','bronirovanie.php');
  });
function sendAjaxForm(result,ajax_form,url){
  $.ajax({
    url: url,
    type: "POST",
    dataType: "json",
    data: $("#"+ajax_form).serialize(),
    success: function(response){
      var result = response;
      $('#result').html('Стоимость:'+result);
    },
    error: function(response) { // Данные не отправлены
            $('#result').html('Ошибка. Данные не отправлены.');
      }
  });
}

The PHP code where the processing takes place, on the site I choose one of the two options from the select and enter a number, then I compare the choice and decide which operation should be performed, then I display the result
$select= $_POST['select'];
   	                          $days=$_POST['days'];
   	                          $sum=0;
   	                          if ($select=='Отель') {
   	                          	# code...
                                      $sum=$days*3000;
   	                          }
   	                             if ($select=='Гостевой дом') {
   	                          	# code...
                                      $sum=$days*10000;
   	                          }
   	                          $sum2="5";
   	                          echo json_encode($sum);

When I click on the submit button of the form, I get the message "Error. Data not sent."

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Zhikhor, 2021-03-29
@shevchenko__k

well then the error is clear.
your $sum variable should be an array and now it's a number. do at least like this
$sum = ['sum' => $sum]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question