Jump to content

Issue trying to get PHP to add a record from an Angular call


tikthra

Recommended Posts

Hey all,

I'm really new to PHP and this problem has me at a loss - I've an Angular app that calls my PHP on the server to add a new entry in the database, but it doesn't ever get added. I've been trying to use a transformRequest to get what's sent to the PHP formatted correctly but I'm not too sure even that's correct.

I'm getting a weird response from the PHP too - it gives me the status OK (which the PHP should only get to if it's inserted), but with an object that has the Err and its error message in it.

I really could use a pointer on this one..!!

my addUser.php:

<?php
include 'DB.php';
$db = new DB();
$tblName = 'members';
            if(!empty($_POST['obj'])){
                $userData = array(
                    'name' => $_POST['obj']['name'],
                    'email' => $_POST['obj']['email'],
                    'phone' => $_POST['obj']['phone'],
                    'group' => $_POST['obj']['group']
                );
                $insert = $db->insert($tblName,$userData);
                if($insert){
                    $data['obj'] = $insert;
                    $data['status'] = 'OK';
                    $data['msg'] = 'User data has been added successfully.';
                }else{
                    $data['status'] = 'ERR';
                    $data['msg'] = 'Some problem occurred, please try again.';
                }
            }else{
                $data['status'] = 'ERR';
                $data['msg'] = 'Some problem occurred, please try again.';
            }
echo json_encode($data);
exit;


my app.js call:

    $scope.addUser = function(){
        var obj = $scope.tempUserData;

        $http({
                   method: 'POST',
                   url: 'http://localhost:8080/sns/addUser.php',
                   data: obj,
                   headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                   },
                transformRequest:function(data) {
                    var str =[];
                    for(var p in data){
                    str.push(encodeURIComponent(p) +"=" + encodeURIComponent(data[p]))
                                }
                        return str.join("&");
                            }
                }).then(function(response){
                    if(response.status == 'OK'){
                    $scope.users.push({
                        id:response.data.id,
                        name:response.data.name,
                        email:response.data.email,
                        phone:response.data.phone,
                        group:response.data.group
                    });
                }
                $scope.tempUserData = {};
            });
    };

Screen shot of the console with the OK status and the response object with the err message:

 

Y728zPS.jpg

Link to comment
Share on other sites

it gives me the status OK (which the PHP should only get to if it's inserted)

 

You're confusing two different things.

 

The value of response.status is just the HTTP status code sent by the webserver. A lot of PHP programmers are too incompetent or lazy to set the correct code, so you always get a 200 (OK), even if the request failed. Your comparison is also wrong: You're comparing the number 200 with the text “OK”, which of course makes no sense.

 

You have to look at the custom status property within the JSON object: response.data.status. It clearly says “ERR”, so obviously something went wrong.

 

Your PHP code also looks strange. What is the “obj” in $_POST['obj'] doing? The data should be directly in the $_POST superglobal: $_POST['name'] etc. That might also be the explanation for the problem: The database code isn't even executed, because your PHP code already stops at the !empty($_POST['obj']) check.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.