2

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?

5
  • what is the output of print_r($_POST); ? Commented Nov 2, 2015 at 9:31
  • i tried checking but it is empty... Commented Nov 2, 2015 at 9:34
  • I would check the data sent by the POST request angular is making using the browser inspector tools. Commented Nov 2, 2015 at 9:34
  • 2
    You are sending $scope.data in the POST request, but you are passing $songName, $artist and $link to the function. When are you setting the $scope.data variable that you are setting as the data for the POST request? By the way, why naming the parameters with a $ before? Commented Nov 2, 2015 at 9:37
  • too many data.... why not simply declare $scope.deleteSong = function( _data) { ? Commented Nov 2, 2015 at 9:41

3 Answers 3

3

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

}

Sign up to request clarification or add additional context in comments.

Comments

0

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']

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.