This is the error that appears
Warning: Undefined array key “name” in C:OSPanelhomehikmetpubliccatalogcontrollerdemotest.php on line 13Warning: Undefined array key “tel” in C:OSPanelhomehikmetpubliccatalogcontrollerdemotest.php on line 14{“success”:0}
HTML
<form action="/" method="post" class="main-hero-form">
<div class="form-row">
<input class="form-control" type="text" name="name" placeholder="Ваше имя" autocomplete="name" required="" id="name">
<input class="form-control" type="tel" name="tel" placeholder="Телефон" autocomplete="phone" required="" id="tel">
<input class="d-none" type="text" name="service" value="Тепловые центры на жидком и газовом топливе с автономной работой">
</div>
<input type="submit"value="Запросить стоимость, условия и времени поставки" class="main-hero-form-send">
<p class="main-hero-text">В течении <span>15 минут</span> вы получите звонок от нашего менеджера, который предоставит подробную консультацию.</p>
</form>
jQuery
$(document).ready(function() {
$('.main-hero-form-send').on('click', function() { //активация функции при нажатии на кнопку с class='main-hero-form-send'
$.ajax({
url: 'index.php?route=demo/test&language=ru-ru',
type: 'post',
dataType: 'json', // нужно ли указывать именно json?
data: {'name': $('#name').val(), 'tel': $('#tel').val()}, // записываем в массив данные из инпута
success: function(data){
if (data.success){
alert('Заметка добавлена');
} else {
alert('Ошибка записи');
}
}
});
});
});
PHP
$json = array();
$name = $this->request->post['name']; // получаем обе эти переменные из посланных в аяксе через POST данных
$tel = $this->request->post['tel'];
try {
$this->db->query("INSERT INTO " . DB_PREFIX . "customer (firstname, telephone) VALUES ($name, $tel)");
$json['success'] = 1; // все успешно
} catch (Exception $e) {
$json['success'] = 0; // не получилось
}
$this->response->addHeader('Content-Type: application/json'); // формируем и отправляем данные в формате json (как и было указано в аяксе )
$this->response->setOutput(json_encode($json));