解释为什么Ionic无法设置发送到php的POST变量 - isset($ _ POST())总是假的?

时间:2016-05-05 07:12:27

标签: php ionic-framework

我的问题

除了CORS之外,是否有人有解释,为什么Ionic不能将发布数据发送到php并且php无法通过

查看设置数据
   SELECT * 
   FROM first_table A, second_table B 
   WHERE A.FULL_NAME = B.FULL_NAME 
   AND Lower(A.FULL_NAME) LIKE '%name%'

对于实例,我尝试将一些登录信息发送到我的php文件" login_check.php"。

以下是我的controller.js

   isset($_POST(u_name))

我尝试通过我的" login_check.php"如下

  .controller('LoginCtrl', function($scope, $http, $ionicPopup, $state) {

       $scope.data = {};

       $scope.login = function() {
            console.log("Login pge :: getlogin");
            var link = 'http://192.168.1.2:80/mySite/login_check.php';
            var request = $http.post(link, { "u_name": $scope.data.username, "u_pass": $scope.data.password }, {'Content-Type': 'application/x-www-form-urlencoded'} );

            request.success(function(response) { 
                console.log(response);  
                if(response.success === 1 ){
                    $state.go('tab.dash');
                    console.log(response);

                }else{
                    //error handling
                }

            })
            request.error(function(response) {
                 //error handling
            });
        } 
   })

每当我尝试发送数据时,它总是说

<?php
    include_once './database_conn.php'; //my DB configuration details

    // array for JSON response
    $response = array();
    // check for required fields
    if(isset($_POST('u_name')) && isset($_POST('u_pass'))){

        $username = trim($_POST['u_name']);
        $pass = trim($_POST['u_pass']);

        // connecting to db
        $db = new DbConnection();
        // mysql checking the login
        $result = mysql_query("SELECT tu_user, tu_pass FROM twp_users WHERE tu_user = '$username' AND tu_pass = '$pass'");

        if (!empty($result)) {
            // check if row selected or not
            if (mysql_num_rows($result) > 0) {
                // successfully retrieve data into database
                $row = mysql_fetch_assoc($result);
                $response["success"] = 1;
                $response["message"] = "successful";

                echo json_encode($response);
            } else {

                // failed to retrieve row
                $response["success"] = 0;
                $response["message"] = "Login failed. Either username or password is incorrect";

                echo json_encode($response);
            }

        } else{
            $response["success"] = 0;
            $response["message"] = "No login found";

            echo json_encode($response);
        }


   } else {
         // required field is missing
         $response["success"] = 0;
         $response["message"] = "Required field(s) is/are missing";
         echo json_encode($response);

   }    
?>

注意:

  • 我的观点是,我尝试在多个客户端使用相同的数据库相同的服务器端脚本。到目前为止,Java EE和Android正在从这个php返回数据。

  • 如果我使用$ _GET()并通过URL发送数据,这个php适用于Cordova / Ionic。

2 个答案:

答案 0 :(得分:0)

请更改行:

if(isset($_POST(u_name)) && isset($_POST(u_pass)))

要:

if(isset($_POST['u_name']) && isset($_POST['u_pass']))

我希望它有所帮助。

答案 1 :(得分:0)

忘记引号和括号

if(isset($_POST['u_name']) && isset($_POST['u_pass']))

像这样更改mysql查询:

$result = mysql_query("SELECT tu_user, tu_pass FROM twp_users WHERE tu_user = '{$username}' AND tu_pass = '{$pass}'");