G
G
GrandinaWi2021-04-28 12:59:07
SQL
GrandinaWi, 2021-04-28 12:59:07

How to send a PHP request using AJAX?

Hello guys, such a problem. There is a form, there is a PHP request and ajax code. Without AJAX, PHP sends data to the DB. With ajax for some reason not.

<form action="" id="ajax_form" method="POST">
  <label for="head_logo_photo" class="">Логотип </label><!---->
  <input id="head_logo_photo"  name="logo_link" type="text" placeholder="Введите название" class="form-control">
  <label for="head_adres">Адрес</label>
  <input type="text" id="head_adres"  name="adres" placeholder="Адрес" class="form-control">
  <label for="head_phone">Телефон</label>
  <input type="text" id="head_phone" name="phone" class="form-control" placeholder="Телефон">
  <label for="head_tab">Первая вкладка</label>
  <input type="text" id="head_tab" name="tab1" class="form-control" placeholder="Вкладка">
  <label for="head_tab2">Вторая вкладка</label>
  <input type="text" name="tab2" id="head_tab2" class="form-control" placeholder="Вкладка">
  <label for="head_tab3">Третья вкладка</label>
  <input type="text" id="head_tab3" name="tab3" class="form-control" placeholder="Вкладка">
  <label for="head_tab4">Четвертая вкладка</label>
  <input type="text" id="head_tab4" name="tab4" class="form-control" placeholder="Вкладка">
  <label for="" id="result"></label>
                                                 
  <div class="button-position">
    <input type="submit"  id="head_add" name="head_add" class="btn btn-success btn-block btn_struct" value="Сохранить">
    <input type="submit" name="head_delete" class="btn btn-danger btn-block btn_struct" value="Удалить">
  </div>
</form>

<?php
       require_once ("connection.php");
if (isset($_POST['head_add'])) {
     # code...

     $logo=$_POST['logo_link'];
     $adres=$_POST['adres'];
     $phone=$_POST['phone'];
     $tab1=$_POST['tab1'];
     $tab2=$_POST['tab2'];
     $tab3=$_POST['tab3'];
     $tab4=$_POST['tab4'];
     $query="INSERT INTO header (id, logo, adres, phone, tab1, tab2, tab3, tab4) VALUES (NULL,'$logo', '$adres', '$phone', '$tab1', '$tab2', '$tab3', '$tab4')";
     mysqli_query($link,$query);

 }

?>

$("document").ready(function(){
$("#head_add").click(function()
{
   event.preventDefault();
   sendAjaxForm('result','ajax_form','../PHP/header_add_delete.php');
   
  });
});
function sendAjaxForm(result,ajax_form,url){
  $.ajax({
    url: url,
    type: "POST",
    dataType: "html",
    data: $(this).serialize(),
     success: function(){
       alert("AJAX работает!");
     }
  });
}

When you click on the Save button, it displays an alert with the inscription AJAX is working. But the data is not sent to the database.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Telepnev, 2018-09-07
@AlexKulaga

Select country_id from (Select unnest(country) as counrry_id from table)_ group by country_id having count(*) > 5

A
Adamos, 2021-04-28
@Adamos

data: $(this).serialize(),
Here, presumably, the form was meant, and not the empty this of the called function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question