javascript - cannot pass data from php script to angularjs using angularjs php
one text
when ever i tried to update form response from php script shows undefined.
script.js
$scope.Edit = function() {
$http.post("update.php", {
'id' : $scope.id,
'Name': $scope.Name
})
.then(function(response){
$scope.names = response.data;
console.log(response.data.success);
}, function(){
console.log(response.data.fail);
})
}
update.php
<?php
try {
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf-8');
$link = new PDO("mysql:host=localhost;dbname=angularjs", "root", "");
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$response = array();
$json = json_decode(file_get_contents("php://input"), true);
$Sql = "UPDATE register SET Name = :Name WHERE id = :id";
$stmt = $link->prepare($Sql);
$stmt->bindParam(":Name", $json['Name'], PDO::PARAM_STR);
$stmt->bindParam(":id", $json['id'], PDO::PARAM_STR);
$stmt->execute();
$response['success'] = "successfully inserted";
} catch (Exception $e) {
$response['fail'] = "Error.".$e;
}
?>
template.html
<div class="container" ng-controller="welcomeController">
<h1>{{ name }}</h1>
<p>{{ success }}</p>
<p>{{ fail }}</p>
<div class="table-responsive">
<table class="table table-hover table-borderless">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>DOB</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody ng-repeat="users in names">
<tr>
<form>
<td><input type="text" ng-model="users.id" readonly></td>
<td><input type="text" ng-model="users.Name"></td>
<td>{{ users.Email }}</td>
<td>{{ users.DOB }}</td>
<td><button type="submit" ng-click="Edit()" class="btn btn-success">Edit</button></td>
<td><button type="submit" ng-click="Delete()" class="btn btn-danger">Delete</button></td>
</form>
</tr>
</tbody>
</table>
</div>
</div>
i need response from php script as updated successfully but it shows undefined. i tried to debug many times i cannot see what mistake i have done.
Source