0

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);
1

1 Answer 1

0

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"]
// ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.