-1

im trying to send a AJAX request to my database and get the information in JSON format back, then i want to replace my div with those json information. But my div wont get changed and i dont know why..

Here is my code and thank you very much for helping:

index.php

<form class="login-form" method="post" action="">
    <input type="text" name="lusername" placeholder="username"/>
    <input type="password" name="lpassword" placeholder="password"/>
    <input type="image" name="login" src="src/login.png" onClick="getLoginInfo()">
    <p class="message">Not registered? <a href="#" onClick="return false" onmousedown="changeContent('registerClick');"><span id="tab">Create an account</a></span></p>
    <div id="test">change me pls</div>
</form>

login.php

<?php
    require_once("connection.php");

    if(isset($_POST['login_x']) or isset($_POST['login_y'])) {
        $lusername = $_POST['lusername'];
        $lpassword = $_POST['lpassword'];
        $select = mysqli_query($connection, "SELECT Username, Password FROM Account WHERE Username='".$lusername."' AND Password='".$lpassword."'");

        if(mysqli_num_rows($select) == 0) {
            echo '<script>cantFindAccount()</script>';
        } else {
             $array = mysqli_fetch_row($select);
             echo json_encode($array);
        }
    }
?>

getLoginInfo.js

function getLoginInfo() {
    $.ajax({
        url: 'http://localhost/login.php',
        data: "",
        dataType: 'json',
        success: function(data) {
            var user = data[0];
            var pass = data[1];
            $('#test').html("<b>id: </b>"+user+"<b> name: </b>"+pass);
        }
    });
    return false;
}

1 Answers1

0

Try this, you have to post some data to your script so that it would process. Moreover you have to use method: 'POST' because you have used $_POST[]

 $.ajax({
    url: 'http://localhost/login.php',
    data: $(this).serialize(),
    method: 'POST',
    dataType: 'json',
    success: function(data) {
        var user = data[0];
        var pass = data[1];
        $('#test').html("<b>id: </b>"+user+"<b> name: </b>"+pass);
    }
});
Ali Rasheed
  • 2,629
  • 2
  • 15
  • 24