AJAX是不是将值传递到PHP [英] AJAX is not passing values to PHP

查看:112
本文介绍了AJAX是不是将值传递到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一个解决方案来访问通过AJAX传递给我的PHP脚本变量的这个简单的问题。我甚至尝试使用isset($ _ POST),但它仍然未能找到用户名和密码的变量。

下面是AJAX调用:

 变种U = $(#用户名的形式).VAL();
变种P = $(#密码,形式).VAL();

//测试
的console.log('用户名:'+ U); //'约翰'
的console.log(密码:+ P); // '测试'

如果(U ==''和;&放大器,P = =!!){

    $阿贾克斯(网址:{url:http://www.domain.net/php/user-login.php,
    数据:{用户名:U,密码:P},
        类型:'后',
    异步:真正的,
    数据类型:JSON,
    beforeSend:函数(){
        //这个回调函数数据之前将触发发送
        $ .mobile.loading('秀'); //这将显示ajax的微调
    },
    完成:函数(){
        //这个回调函数将在发送的数据触发/接收完成
        $ .mobile.loading(隐藏); //这将隐藏阿贾克斯微调
    },
    成功:功能(数据){
        //保存后返回的数据localStorage的手动按钮来回切换
        //window.localStorage.setItem('topGenderDataArray',data);
        的console.log(登录成功+数据);
        console.dir(数据);

        警报(欢迎回来+数据['用户名']);
        $(#loginButton)removeAttr(禁用)。
    },
    错误:函数(XHR,要求,错误){
        //这个回调函数将在不成功的行动触发
        (出现网络错误,请重试!'+ XHR +|+要求+|+错误)警告;
    }
});
 

下面是PHP脚本的开头:

 如果(使用isset($ _ POST ['用户名'])及和放大器;使用isset($ _ POST ['密码']))
{
    $数据['用户名'] = $ _ POST ['用户名'];
    $数据['密码'] = $ _ POST ['密码'];
}
其他{
    $数据['消息'] =抱歉,发生错误[]!;
    $数据['USER_ID'] = -1;
    回声json_en code($的数据);
    出口();
}
 

解决方案

现在的问题是在你的PHP code 你做回声 json_en code($数据)其他子句中,而你应该后做在如果其他就像这样:

 如果(使用isset($ _ POST ['用户名'])及和放大器;使用isset($ _ POST ['密码']))
{
    $数据['用户名'] = $ _ POST ['用户名'];
    $数据['密码'] = $ _ POST ['密码'];
}
其他
{
    $数据['消息'] =抱歉,发生错误[]!;
    $数据['USER_ID'] = -1;
}
回声json_en code($的数据);
 

I can't find a solution to this simple problem of accessing variable passed to my PHP script via AJAX. I have even tried isset($_POST) but it still fails to find the username and password variables.

Here is the AJAX call:

var u = $("#username", form).val();
var p = $("#password", form).val();

//testing
console.log('Username: '+u); // 'John'
console.log('Password: '+p); // 'test'

if(u !== '' && p!=='') {

    $.ajax({url: 'http://www.domain.net/php/user-login.php',
    data: {username:u,password:p},
        type: 'post',                   
    async: true,
    dataType:'json',
    beforeSend: function() {
        // This callback function will trigger before data is sent
        $.mobile.loading('show'); // This will show ajax spinner
    },
    complete: function() {
        // This callback function will trigger on data sent/received complete
        $.mobile.loading('hide'); // This will hide ajax spinner
    },
    success: function (data) {
        //save returned data to localStorage for manual button toggling later
        //window.localStorage.setItem('topGenderDataArray',data);
        console.log("Login successful: "+ data);
        console.dir(data);

        alert("Welcome back "+data['username']);
        $("#loginButton").removeAttr("disabled");
    },
    error: function (xhr,request,error) {
        // This callback function will trigger on unsuccessful action   
        alert('Network error has occurred please try again! '+xhr+ " | "+request+" | "+error);
    }
});

Here is the beginning of the PHP script:

if(isset($_POST['username']) && isset($_POST['password']))
{
    $data['username'] = $_POST['username'];
    $data['password'] = $_POST['password'];
}
else{
    $data['message']= "Sorry, an error occurred! []";
    $data['user_id']= -1;
    echo json_encode($data);
    exit();
}

解决方案

The problem is in your php code you do the echo json_encode($data) inside the else clause, while you should do it after the if and else just like this:

if(isset($_POST['username']) && isset($_POST['password']))
{
    $data['username'] = $_POST['username'];
    $data['password'] = $_POST['password'];
}
else
{
    $data['message']= "Sorry, an error occurred! []";
    $data['user_id']= -1;
}
echo json_encode($data);

这篇关于AJAX是不是将值传递到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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