Skip to content Skip to sidebar Skip to footer

JqueryAjax And Php Logic

Hey guys im with a problem getting a value from php. I know we have a lot of problems with this kind of issues, but i need help. This is my javascript $( document ).ready(function(

Solution 1:

Everything else is fine, you are not passing data correctly to ajax call. You are making query string but you have to pass JSON object if you want to capture it in $_POST in php and can append to url if you want to capture in $_GET array. I have corrected your function in both ways below:

function validate_user() {

    //We get data input
    var username = $('.username').val();
    var password = $('.password').val();

    //We create a datastring ex: functions.php?function=validate_user&username=username&password=password
    var datastring = { 'function': 'validate_user', 'username': username, 'password': password }

    //The json Ajax Request
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '@loginAPI/functions.php',
        data: datastring,
        success: function(result) {
            console.log(result);
            $(".loading_bg").fadeOut("slow");
        },
        error: function(xhr, status){
            console.log(status);
        }

    });
    return false;

}

When you want to capture data in $_GET at server side

function validate_user() {
    //We get data input
    var username = $('.username').val();
    var password = $('.password').val();

    //We create a datastring ex: functions.php?function=validate_user&username=username&password=password
    var datastring = 'function=validate_user' + '&username=' + username + '&password=' + password; 

    //The json Ajax Request
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '@loginAPI/functions.php?' + datastring,
        data: {},
        success: function(result) {
            console.log(result);
            $(".loading_bg").fadeOut("slow");
        },
        error: function(xhr, status){
            console.log(status);
        }

    });
    return false;

}

Here is PHP Code

<?php
require_once('../@configs/db_connect.php');
//Lets send our data back to index
if(isset($_GET['function'])) {
    $user = $_GET['username'];
    $password = $_GET['password'];
    echo login::validate_user($user, $password);   
}
.... // Remaining Class will come here

Solution 2:

Im sorry to bother all of you, the real problem its my form input feilds.. i forgot to set a class... Thank you all, and once again, im sorry to make you lose time with such a silly problem.


Post a Comment for "JqueryAjax And Php Logic"