2

I am new to AngularJs. I was trying to create a simple application using angularjs. In a simple application i am not able to print posted data via $_POST, $_GET or$_REQUESTin my php script. While usingfile_get_contents("php://input")` i am able to print data.

Can any one let me know why $_GET, $_POST and $_REQUEST are not working?

My Index.html Code :

<!DOCTYPE html>
<html ng-app>
<head>
<title>Search form with AngualrJS</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
    <script src="http://code.angularjs.org/angular-1.0.0.min.js"></script>
    <script src="search.js"></script>
</head>

<body>

    <div ng-controller="SearchCtrl">
    <form class="well form-search">
        <label>Search:</label>
        <input type="text" ng-model="test" class="" placeholder="Keywords...">
        <button type="submit" class="btn" ng-click="search()">Search</button>
        <p class="help-block">Try for example: "php" or "angularjs" or "asdfg"</p>      
    </form>
<pre ng-model="result">
{{result}}
</pre>
   </div>
</body>

</html>

search.js code :

function SearchCtrl($scope, $http) {
    $scope.url = 'search.php'; // The url of our search

    // The function that will be executed on button click (ng-click="search()")
    $scope.search = function() {

        // Create the http post request
        // the data holds the keywords
        // The request is a JSON request.
        $http.post($scope.url, { "data" : $scope.test}).
        success(function(data, status) {
            $scope.status = status;
            $scope.data = data;
            $scope.result = data; // Show result from server in our <pre></pre> element
        })
        .
        error(function(data, status) {
            $scope.data = data || "Request failed";
            $scope.status = status;         
        });
    };
}

Search.php Code :

<?php
// The request is a JSON request.
// We must read the input.
// $_POST or $_GET will not work!

$data = file_get_contents("php://input");

$objData = json_decode($data);

// perform query or whatever you wish, sample:

/*
$query = 'SELECT * FROM
tbl_content
WHERE
title="'. $objData->data .'"';
*/

// Static array for this demo
$values = array('php', 'web', 'angularjs', 'js');

// Check if the keywords are in our array
if(in_array($objData->data, $values)) {
    echo 'I have found what you\'re looking for!';
}
else {
    echo 'Sorry, no match!';
}

Any assistance will be appreciated.

Amit Kumar
  • 3,384
  • 6
  • 25
  • 42

1 Answers1

0

To go along with my comment about a possibility of a bad setting in php.ini, this article(dead link) (linked through this one, from my comment) also offered this quote:

If the Content-Type is empty or not recognized in the HTTP message then the PHP $_POST array is empty. Not sure if this is a bug or is by design…

The symptoms being the same, (empty $_POST but data available through php://input), this brings up the number of possible reasons this error is occurring to two:

  1. Syntax error in php.ini post_max_size setting
  2. Bad value or empty Content-Type header being sent from the front-end

If your setting for post_max_size is correct, then it could be that angular is removing the Content-Type header for the post. According to this question and this question, angular will remove the Content-Type header if there is no payload or an empty payload going along with any request, which I believe is your issue.

Try adding this in your SearchCtrl:

function SearchCtrl($scope, $http) {
    $scope.url = 'search.php'; // The url of our search
    $scope.test = {test:'test'}; // Add me
    // ...

I suspect your $_POST array will become filled, since there will now be a Content-Type header being sent along with the request, and actual request data being sent. If that works, it means that your test input isn't binding correctly to the scope, or you're submitting empty data. Try adding a guard clause before the HTTP Request:

if($scope.test) {
    $http.post($scope.url, { "data" : $scope.test})
    // ...
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96