JqueryAjax和PHP逻辑 [英] JqueryAjax and php logic

查看:78
本文介绍了JqueryAjax和PHP逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在从php获取价值时遇到了问题.我知道这类问题很多,但我需要帮助.

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.

这是我的javascript

This is my javascript

$( document ).ready(function() {
    $(".loading_bg").hide();
});

$( document ).ajaxStart(function() {
  $(".loading_bg").fadeIn("slow");
});

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;

        }

这是我的php

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


class login {

    static function validate_user($username, $password) {


        //Call a new default connection
        $db = db::connect();

        //Prepare our sql
        $stmt = $db->prepare("SELECT * FROM Accounts WHERE username = :username AND password = :password");

        //Bind our values to the SQL statement
        $stmt->bindValue(':username', $username, PDO::PARAM_STR);
        $stmt->bindValue(':password', $password, PDO::PARAM_STR);
        $stmt->execute();

        //Get number of affected rows
        $results = $stmt->rowCount();

        //If to check if we can find any row with username and password
        if($results === 1) {
            //return json_encode("valid account");
        } else {
            return json_encode($username);
        }

    }

}
?>

当我执行请求时,我从我的var中收到一个未定义的错误时,我不知道如何解决该问题,如果可以的话,有人可以帮助我.

When i do the request im getting a undifned error from my var, i dont know how to fix it, can someone help me, if possible.

我认为我的$_POST ..因为如果使用login::validate_user("teste","teste);运行php,我可以获得json结果.

I think its something with my $_POST.. because if run the php with login::validate_user("teste","teste); i can get the json result..

推荐答案

一切都很好,您没有将数据正确传递给ajax调用.您正在查询字符串,但如果要在php的$ _POST中捕获它,则必须传递JSON对象;如果要在$ _GET数组中捕获,则可以附加到url.我已经通过以下两种方式纠正了您的功能:

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;

}

当您想在服务器端$ _GET中捕获数据时

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;

}

这是PHP代码

<?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

这篇关于JqueryAjax和PHP逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆