Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm a beginner when it comes to AngularJS but I have just figured out how to pass POST data using the $http service to a PHP file to insert to my database.

This is my add.php file. It displays my form and has the insert code block at the bottom:

<h2>Add Car</h2>
<div ng-controller="formCtrl">
    <form method="POST">
        <label>Make:</label>
        <select ng-model="car.make">
            <option>Make 1</option>
            <option>Make 2</option>
            <option>Make 3</option>
        </select>
        <label>Model:</label>
        <input type="text" ng-model="car.model">
        <input type="submit" ng-click="addCar(car)">
    </form>

    <pre>
        {{message}}
    </pre>
</div>


<?php
$data = json_decode(file_get_contents("php://input"));
$make = $data->make;
$model = $data->model;

$con = new PDO('mysql:host=localhost;dbname=mydb', 'user', 'pass');

$stmt = $con->prepare("INSERT INTO cars (model, make) VALUES(?, ?)");
$stmt->bindParam(1, $model);
$stmt->bindParam(2, $make);
$stmt->execute();
?>

This is my controller:

app.controller('formCtrl', function ($scope, $http) {

    $scope.addCar = function (car) {

        $http({
            method: 'POST',
            url: 'tools/add.php',
            data: car,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        })
        .then(function (response) {
           $scope.message = "great!"; 
        });
    }
});

Now is there anyway to check to see if the insert was successful? I know you can use the rowCount() function to check on the PHP side and then set your message to either be an error or a successful insert, but I'd like to know if there's anyway for my JS to know whether a row was actually inserted or not.

share|improve this question
    
You'll have to send it as a response from the server-side code, no other way. – Daan Jan 7 at 7:15
    
Send success response from php end and handle the same in controller.. – Rayon Dabre Jan 7 at 7:16
1  
Your PHP file should check if everything is okay and then print a JSON with the result. For example {"status":"ok"} or {"status":"error"}. In angular, you can then get this data by catching the promise, which is returned by the $http service (like in your code). The response then contains an object data, which is exactly the response from the server (response.data.status is either ok or error). You could also send another HTTP status code (200 vs. e.g. 400 or 500). Then you can catch the success case in the first argument function of then, and the error in the 2nd. – ssc-hrep3 Jan 7 at 7:21
    
Would adding something like if ($stmt->rowCount()) { $status = json_encode('{"status" : 1}'); } else { $status = json_encode('{"status" : 0}');} be a waste of time vs. echoing it out into a page and then using another ajax call to get the json? Is there a better way after I encode the json that I could just pass the data back to my JS without making another call? - I apologize in advance for my ignorance, very new to the JS world, especially Angular. – Bryner Jan 7 at 8:24
1  
You have one PHP page which does all the stuff: The Insertion into the DB and the check if it is in the DB. If it is in there, you return status 1 and if not you return status 0. Angular is waiting until you get the final response of PHP (the page is fully loaded). So, you do not have to make another call for that (of course, you could do that..). If you want to pass the inserted data back to angular, you just create an extended version of the return object. Something like: {"status":1,"data":[{"model":"Audi"},{....}]} or any other data structure you have in PHP. (use json_encode for that) – ssc-hrep3 Jan 7 at 8:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.