Answer the question
In order to leave comments, you need to log in
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('Ошибка. Данные не отправлены.');
}
});
}
$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);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question