0

I want to make a live username check and I want to use a PHP function.

-->
<?php
    require '../../core/init.php';
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <title>K&ouml;ppCMS - Registrieren</title>
    <link rel="stylesheet" href="../../../css/style.css">
    <script type="text/javascript" src="../../../js/jquery.js"></script>
    <script type="text/javascript" src="../../../js/footer.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#username").keyup(function (e) {

                //removes spaces from username
                $(this).val($(this).val().replace(/\s/g, ''));

                var username = $(this).val(); //get the string typed by user

                $.post('users.php', {'username':username}, function(data) { //make ajax call to users.php
                    $("#user-result").html(data); //dump the data received from PHP page
                });

            });
        });
</script>
</head>

Init.php:

<?php
session_start();
require 'database/connect.php';
require 'classes/users.php';
require 'classes/general.php';

$users         = new Users($db);
$general     = new General();

$errors     = array();
?>

So how can I call the check_username function and send the values to it? Hope you understand my question, because my English isn't that good.

i'd tried this in the users.php:

<?php
$users = new Users($db);
echo $users->check_username($_POST['username']);
class Users{
    private $db;

    public function __construct($database) {
        $this->db = $database;
    }

    public function check_username($data) {
        return $data+1;
    }
    function func1($data){
        return $data+1;
    }

}

Get this Error:

Notice: Undefined variable: db in C:\xampp\htdocs\kcms\system\cms\user\users.php on line 2

enter image description here

  • You want to use `return $username . " hi";` instead of `return $username + " hi";` Unless you want to do "math", but I doubt that very much ;-) (sidenote) – Funk Forty Niner Apr 21 '14 at 17:28
  • Oh thanks Fred -ii- ^^ So now lets solve my actually problem ^^ – user3557428 Apr 21 '14 at 17:39
  • You're welcome. Did you check Barmar's answer? – Funk Forty Niner Apr 21 '14 at 17:40
  • Yes of course, but what is with my js? I want to check it live so i dont know how to introduce it :/ – user3557428 Apr 21 '14 at 17:43
  • JS is not my strong points. I suggest you take it up with Barmar or any other answers that potentially come after. I'm a "server-side" kind of guy ;-) – Funk Forty Niner Apr 21 '14 at 17:43
  • Thanks anyway Fred :) Is there another way to contacte you for questions? – user3557428 Apr 21 '14 at 17:46
  • No, I don't leave contact information on SO. I'm purely here to help for immediate questions, unless you were a client ;-) – Funk Forty Niner Apr 21 '14 at 17:51
  • Before you edited your post I saw a bunch of php warnings in the second "Error" screenshot followed by the correct result of check_username. Use the console to verify that you are posting the username via ajax, and to see what the result is. To me it looks like you have some errors/warnings in the php which are creating unexpected text in the result sent back to $.post. The error you are now reporting seems to be because you are visiting the users.php page directly with your browser and not posting any data to it from ajax. – James Apr 21 '14 at 18:35

1 Answers1

3

Your PHP script should do something like:

<?php
require('init.php'); // This contains $users = new Users($db);
echo $users->check_username($_POST['username']);

For using the return value in your Javascript, see

How do I return the response from an asynchronous call?

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612