0

I'm new on PHP and now i'm learning about how to use AJAX, i just want to get the return value that passed from PHP(webservice.php) to JS. I dont know why it always return null value when im trying to alert it with the "alert(result)" on my JS Login function.

I'm sorry for my bad English, hope u guys understand what i mean

HTML :

<input class="button btnlogin" type="button" class="alt" id="btn_submit_data" value="Log In" onclick="login()" />

JS :

function login() {
    var id = document.getElementById('login_email').value;
    var pass = document.getElementById('login_pass').value;
    $.get("http://localhost/project/dist/bin/webservice.php", {
        ajx: "login",
        email: id,
        password: pass
    }, function (result) {
        if (result == true) {
            window.location.href = "http://localhost/project/index.php";
        } else {
            alert(result);//This is where i want to get the value but it always returning the null value
        }
    });
}

PHP (connection.php) :

<?php
    session_start();

    function getConnection(){
        $servername ="127.0.0.1";
        $username = "root";
        $password = "";
        $database = "project";
        $conn = new mysqli($servername, $username, $password,$database);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        return $conn;
    }
    getconnection();
?>

PHP (webservice.php) :

<?php
    include 'connection.php';

    if($_GET['ajx'] == "login"){
        $email = $_GET['email'];
        $password = $_GET['password'];
        Login($email,$password);
    }

    function Login($email,$password){
        return $email;
    }
?>
Sebastian
  • 85
  • 8

1 Answers1

0

Because your PHP code does not return anything. What you get in your browser is defined by what PHP returns in output buffer. As many said you should use:

$output = Login($email,$password);
echo json_encode($output);

You should also include:

header('Content-type:application/json;charset=utf-8');

See also this question.

Makla
  • 7,975
  • 9
  • 52
  • 118