A
A
alexei_20022021-05-09 22:18:33
AJAX
alexei_2002, 2021-05-09 22:18:33

How to execute PHP script via ajax?

How to execute PHP script with ajax?

# Если кнопка "Yes" нажата
    if( isset( $_POST['btn_yes'] ))
    {
    // выполняем операции с базой данных
    $mysqli_query ="UPDATE check_vacancy SET control = '1' WHERE id = ".$_POST['rowid'].";";
    $qr_result = mysqli_query($link, $mysqli_query)
    or die(mysqli_error());
    }

    # Если кнопка "No" нажата
    if( isset( $_POST['btn_no'] ))
    {
    // выполняем операции с базой данных
    $mysqli_query ="UPDATE check_vacancy SET control = '0' WHERE id = ".$_POST['rowid'].";";
    $qr_result = mysqli_query($link, $mysqli_query)
    or die(mysqli_error());
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Toropov, 2021-05-10
@alexei_2002

Sending a POST request with AJAX is easy to do with jQuery

<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>AJAX Send</title>
</head>
<body>
    <button id="btn_yes">Btn_yes</button>
    <button id="btn_no">Btn_no</button>

    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script src="main.js"></script>
</body>
</html>

main.js file:
//"file.php" - Это тот файл на который будем отправлять AJAX запрос
$("#btn_yes").on('click', function() {
    $.post("file.php", { btn_yes: "btn_yes"})   
        .done(function( data ) {
            alert( "Сообщение: " + data );
    });
});

$("#btn_no").on('click', function() {
    $.post("file.php", { btn_no: "btn_no"})   
        .done(function( data ) {
            alert( "Сообщение: " + data );
    });
});

File code "file.php" (you can replace it with your own):
if( isset( $_POST['btn_yes'] )) {
    echo 'Отправлена кнопка btn_yes';
}

if( isset( $_POST['btn_no'] )) {
    echo 'Отправлена кнопка btn_no';
}

N
Nadim Zakirov, 2021-05-10
@zkrvndm

Everything is quite simple. PHP is such a language that the code in it is executed every time we open a page. In other words, in order to execute the code in some conditional test.php file, it is enough to open it (download). In JavaScript, you can use fetch to do this. Just make a request to the address of your script via fetch:
https://learn.javascript.ru/fetch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question