简体   繁体   中英

POST request with Angular and PHP

So my problem is sending data to server with angular and post method with PHP. On the server side when submit button is clicked, and it adds, new ID as primary key. But value from input is not send. Thanks for help.

My html file

<h1>{{name}}</h1>
<h1 id="number" ng-click="getNumber(1)">0</h1>
<form on-submit="addNew()">
    <input type="text" ng-model="formModel.text" id="text" name="text"/>
    <button ng-click="addNew()">Add new text</button>
</form>

Angular file

app.controller('newCtrl', ['$scope','$http',function($scope,$http){
    $scope.name = "Nikson";
    document.getElementById('title').style.display = "none";

    $scope.formModel = {};
    $scope.addNew = function() {
        console.log("Submited");
        console.log($scope.formModel);

        $http.post('post.php', $scope.formModel).
        success(function(data){
            console.log("ok")
        }).error(function(data){
            console.log("err");
        });
    };
}]);

And finaly PHP file

$conn = mysqli_connect('localhost','nemkeang','nemkic23', 'recs');

if(!$conn) 
    die("Error");

$text = $_POST['text'];

$sql = "INSERT INTO images (text)
VALUES ('$text')";

if (!mysqli_query($conn,$sql)) {
    die('Error: ' . mysqli_error($conn));
}
echo "success";

mysqli_close($con);

To receive the text in PHP you need to get it from request body. So try this:

<?php
$input = json_decode(file_get_contents('php://input'), true);
$text = $input["text"]
// ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM