Donoven Rally
Donoven Rally

Reputation: 1700

Passing variable from view to http post using AngularJS and PHP

I am taking first steps with angular here and breaking my head about two stuff. the first is i cant pass variable from the view to http.post call.

in this index.html ng-click passes the correct values:

<tbody>
    <tr ng-repeat="data in filtered = (list | filter:search | orderBy : predicate :reverse) | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">
       <td><a href="{{data.link}}" target="_blank">{{data.songName}}</td>
       <td>{{data.artist}}</td>
       <td><a ng-click="deleteSong(data.songName, data.artist, data.link)"><i class="glyphicon glyphicon-trash"></i></a></td>
    </tr>
</tbody>

and in app.js the deleSong function looks like this:

$scope.deleteSong = function($songName, $artist, $link) {
    // Posting data to php file
    $http({
        method  : 'POST',
        url     : 'ajax/deleteSong.php',
        data    : $scope.data, //forms user object
        headers : {'Content-Type': 'application/x-www-form-urlencoded'}
    })
        .success(function(data) {

            if (data.errors) {
                // Showing errors.
                $scope.errorSong= data.errors.song;
                $scope.errorArtist= data.errors.artist;
                $scope.errorLink= data.errors.link;
            } else {
                $scope.message = data.message;
            }
        });
    $scope.user = null
}

and finally , deleteSong.php looks like this:

<?php
include('../includes/config.php');

$errors = array();
$data = array();
// Getting posted data and decodeing json
$_POST = json_decode(file_get_contents('php://input'), true);

$song = $_POST['song'];

$query="DELETE FROM songs WHERE songName = '$song'";
$mysqli->set_charset('utf8mb4');
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

echo $query;
?>

When i click the delete song icon, the function get called but the response i get is this:

DELETE FROM songs WHERE songName = ''

any idea what am i missing and why is the query not getting the data i passed to deleteSong.php?

Upvotes: 2

Views: 4200

Answers (3)

Hitmands
Hitmands

Reputation: 14159

this is a known issue, in your php file, instead of using $_POST try to use $data = json_decode(file_get_contents("php://input")); (have a look here)

That might work assuming all the rest is without bugs... Ensure, with a console.log, that $songName, $artist, $link variables contain the correct value...

just a note, never use variables with $ because dollar is used for private angularjs variables

Upvotes: 0

vivask
vivask

Reputation: 32

You're assigning $_POST['song'] to the $song variable but it does not exist in the $_POST-array. Please replace $_POST['song'] with $_POST['songName']

Upvotes: 0

Partha Sarathi Ghosh
Partha Sarathi Ghosh

Reputation: 11576

Change you code as follows, You are taking the data as parameter of your ng-clik function. but you are not using those. Rather you are trying $scope.data (Which is something else). So change the code as follows.

$scope.deleteSong = function($songName, $artist, $link) {
// Posting data to php file
$http({
    method  : 'POST',
    url     : 'ajax/deleteSong.php',
    data    : {'song':$songName},//Changed Line Here 
    headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
    .success(function(data) {

        if (data.errors) {
            // Showing errors.
            $scope.errorSong= data.errors.song;
            $scope.errorArtist= data.errors.artist;
            $scope.errorLink= data.errors.link;
        } else {
            $scope.message = data.message;
        }
    });
$scope.user = null

}

Upvotes: 3

Related Questions