2016-12-02 12 views
0

私はかなり新しくphpですが、MySQLサーバーにデータを送受信するために私のWebサーバーに "API"を設定しています。

何かの理由で、register.phpファイル(登録エントリポイント)にhttp投稿リクエストを送信すると、$_POST変数は空のままです。しかし、phpconfig()を使用していくつかの掘り下げを行った後、私は送信されたパラメータが$_REQUEST変数に格納されていることに気づいたので、私はregister.phpを変更して動作しました。

私の質問は、それが起こっている理由と$_REQUESTが間違っているかどうかです。

私はangularjs' $のHTTPを使用してHTTPリクエストを送信します。

var link = 'http://www.temporaryurl.com/register.php' 

$http.post(link, {'name': 'Adam'},{'email': '[email protected]'},{'password': 'testingPass!'}).then(function(response) { 
console.log("Http success!"); 
console.log(response); 
}, function(response) { 
console.log("Http failure"); 
console.log(response); 
}); 

ここregister.php

<?php 
require_once 'include/DB_Functions.php'; 
$db = new DB_Functions(); 

// json response array 
$response = array("error" => FALSE); 

if (isset($_REQUEST['name']) && isset($_REQUEST['email']) && isset($_REQUEST['password'])) { 

    // receiving the post params 
    $name = $_REQUEST['name']; 
    $email = $_REQUEST['email']; 
    $password = $_REQUEST['password']; 

    // Remove all illegal characters from email 
    $email = filter_var($email, FILTER_SANITIZE_EMAIL); 

    // Validate e-mail 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
     echo("$email is a valid email address"); 
    } else { 
     echo("$email is not a valid email address"); 
    } 

    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
     // check if user is already existed with the same email 
     if ($db->isUserExisted($email)) { 
      // user already existed 
      $response["error"] = TRUE; 
      $response["error_msg"] = "User already existed with " . $email; 
      echo json_encode($response); 
     } else { 
      // create a new user 
      $user = $db->storeUser($name, $email, $password); 
      if ($user) { 
       // user stored successfully 
       $response["error"] = FALSE; 
       $response["uid"] = $user["unique_id"]; 
       $response["user"]["name"] = $user["name"]; 
       $response["user"]["email"] = $user["email"]; 
       $response["user"]["created_at"] = $user["created_at"]; 
       $response["user"]["updated_at"] = $user["updated_at"]; 
       echo json_encode($response); 
      } else { 
       // user failed to store 
       $response["error"] = TRUE; 
       $response["error_msg"] = "Unknown error occurred in registration!"; 
       echo json_encode($response); 
      } 
     } 
    } else { 
     echo("$email is not a valid email address"); 
     $response["error"] = TRUE; 
     $response["error_msg"] = "Email is invalid!"; 
     echo json_encode($response); 
    } 
} else { 
    $response["error"] = TRUE; 
    $response["error_msg"] = "Required parameters (name, email or password) is missing!"; 
    echo json_encode($response); 
} 
?> 
+0

私の投稿を更新します。 – awbasham

+1

この投稿を参照してくださいhttp://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefinedして試してみてください。 – Perumal

+0

'if(x === false)'はあなたが取り除く必要がある反パターンです。 'if(!x)'と 'if(!x === false)'を使うのは、それは三重項として読まれます:「xが文字通り偽でないならば」。このような無意味な曲を避けてみてください。同様に、あなたの電子メールの "検証"式は、本当に有効なアドレスの多くを拒否しようとしていると思います。 – tadman

答えて

0

私の問題は二重だったのです。 1つは、私は私のウェブブラウザ(私はGETの代わりにPOSTを強制的にクロム拡張子を使用することを忘れていた)のURLを入力してregister.phpファイルを素朴にテストしていたので、$ _POSTが空であったと思います。

また、私はこのように私angularjsファイルに$ HTTPと呼ばれる:

私は 'アプリケーション/ x-www-form-urlencodedで' のようにContent-Typeを定義してDATA1を渡していたもの助けたと思う
$http({ 
    method: 'POST', 
    url: link, 
    data: data1, 
    headers: { 
     'Content-Type': 'application/x-www-form-urlencoded' 
    }}).then(function(response) { 
     console.log("Http success!"); 
     console.log(response); 
    }, function(response) { 
     console.log("Http failure!"); 
     console.log(response); 
    }); 

var data1 = "name=adam&[email protected]&password=testPass!"; 
関連する問題