Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

i have question related to PHP - AngularJs, so i have simple PHP script

<?php
require_once '../../dbConnect.php';

$driverId = $_POST['driverId'];

if (isset($_POST['driverId'])) {

        $sql = "delete from drivers where driver_id='$driverId'";

        if ($mysqli->query($sql) === TRUE) {
            echo mysqli_insert_id($mysqli);
        } else {
            echo "Error updating record: " . $mysqli->error;
        }

        $mysqli->close();
}

?>

At the moment i pass data to script like this

return $http({
    method: 'POST',
    url: '/api/drivers/deleteDriver.php',
    data: $.param(driverObject),
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
});

And i don't like that code, on other Java project i send params to end points with angularjs $resource service like this

var deleteDriverResouce = $resource('/api/drivers/deleteDriver.php');

function deleteDriver() {
  deleteDriverResouce.save(driverObject);
}

And as you can see that is cleaner code and easier to use, i'm wondering can i use $resource service to pass object to php script ?

share|improve this question

So i have found solution and i will share it here so maybe someone will need it. In order to use AngularJs $resource service you just need to make small change in PHP script, just add $object = json_decode(file_get_contents("php://input"), true); on that way you can access to object that you sent via $resource. And here is example of one working PHP script.

<?php
require_once '../dbConnect.php';
session_start();

$object = json_decode(file_get_contents("php://input"), true);

if (isset($object['email']) && isset($object['password'])) {

    $email = $object['email'];
    $password = $object['password'];
    $query="select * from members where email='$email'";
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
    $row = mysqli_fetch_assoc($result);

    if($row) {
        if (password_verify($object['password'], $row['password'])) {
            $_SESSION["id"] = $row['id'];
            echo 'Login Success!';
        } else {
            session_destroy();
            var_dump(http_response_code(400));
        }
    } else {
        session_destroy();
        var_dump(http_response_code(406));
    }

    $mysqli->close();
} else {
    session_destroy();
    var_dump(http_response_code(400));
}
?>

And on UI i have this simple and minimal code:

var userLoginObject = {
  email: '[email protected]',
  password: 'password123'
};

var authenticationResource = $resource('/api/authentication/authenticate.php');

function logIn() {
  authenticationResource.save(userLoginObject );
}

That's much better and cleaner than using ugly

return $http({
    method: 'POST',
    url: '/api/drivers/authenticate.php',
    data: $.param(userLoginObject),
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
});
share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.