简体   繁体   中英

How to retrieve data from php using AJAX?

Good day, I am new to AJAX. I'm trying to get a data from a function inside a class using a handler but when I include the class file the output becomes a bunch of html instead of the actual echoed string.

JS

$('#login-btn').on('click', function () {
    var username = $('#username').val();
    $.post("./php/functions/loginHandler.php",
        {
        usernameInput: username
        }, function (data) {
            console.log(data);
    });
});
PHP Handler
<?php
    include_once './php/database/DBOperations.php';
    if (isset($_POST['usernameInput'])) {
        echo 'test';
}

Thank you, have a good day.

Please use this ajax code, maybe working :

 $('#login-btn').on('click', function () { var username = $('#username').val(); $.ajax({ type: "POST", url: 'your url', data: username, success: function(response) { console.log(response) } }); });

warning : you must add jquery in your html file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM