Нужно вставить данные нажатием кнопки ajax - PullRequest
0 голосов
/ 09 октября 2018

Мне нужно это, нажав на кнопку, которую я могу вставить в базу данных с помощью ajax в WordPress. Пожалуйста, если вы можете мне помочь.Я не знаю, что не так

// Script.js

function action() {
    $.ajax({
        type: 'POST',
        url: 'process.php',
        data: {------------------->What's going on there?               
        }, 
        success: function (data) {
            $("#result").html(response);
        },
        error: function (data) {
            $("#result").html("Error");
        }
    });
}
<input type="submit" name="" value="rating" id="button" onclick = "action();">

<div id="result"></div>

<!-- process.php -->
<?php
    global $wpdb;
               
    $wpdb->insert('rating', array( 
        'rating_postid'=>'5', 
        'rating_posttitle'=>'title'
        )
    ); 
?>

Ответы [ 2 ]

0 голосов
/ 09 октября 2018

HTML-файл

<input type="submit" name="" value="rating" id="but">
<div id="result"></div>

Javascript-файл

but=document.getElementById('but');
but.addEventListener('click', function (){
    $.ajax({
        type: 'POST',
        url: 'process.php',
        data: {
            id: 1
        }, ??
        success: function (data) {
            $("#result").html(response);
        },
        error: function (data) {
            $("#result").html("Error");
        }
    });
});

php-файл process.php

<?php
    $conn=mysqli_query('localhost','root','','DB');
    $query=' Insert Query '; 
    $fireResult=mysqli_query($conn,$query);
    //json output
    json_encode($fireResult); 
?>
0 голосов
/ 09 октября 2018

Попробуйте этот код:

HTML-код:

<input type="submit" name="" value="rating" id="button" onclick = "action();">

<div id="result"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function action() {
    $.ajax({
        type: 'POST',
        url: 'process.php',
        data: {
            id: 1
        },
        success: function (data) {
            $("#result").html(response);
        },
        error: function (data) {
            $("#result").html("Error");
        }
    });
}
</script>

Ваш файл PHP в порядке ..

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...