Reputation: 13
I am trying to update my database record by clicking on button but without any input field. I want to have something like static var.
$scope.test = "test"
And I want to put this "test" to the database by clicking on button.
For example:
I have DB table with:
Id Username State
1 Test Open
And after I click on button "update" I want to update my database record - state from "open" -> "close"
I know how to update my DB when I got something from input field:
<input type="hidden" ng-model="update_id_user">
<div class="input-field col s3">
<input ng-model="update_username" type="text" class="validate" placeholder="Username">
</div>
<div class="input-field col s3">
<input ng-model="update_state" type="text" class="validate" placeholder="State">
</div>
<div class="input-field col s2">
<a class="waves-effect waves-light btn blue" ng-click="updateuser()"><i class="material-icons right"></i>Update</a>
</div>
There is edituser() and update()
$scope.edituser = function(id_user) {
$http.post('edit.php',{'id_user' : id_user }
)
.success(function (data, status, headers, config) {
$scope.update_id_user = data[0]["id_user"];
$scope.update_username = data[0]["username"];
$scope.update_state = data[0]["state"];
})
.error(function(data, status, headers, config){
});
}
$scope.updateuser = function() {
$http.post('update.php',
{
'id_user' : $scope.update_id_user,
'username' : $scope.update_username,
'state' : $scope.update_state
}
)
.success(function (data, status, headers, config) {
$scope.getall();
});
}
edit.php file
<?php
require 'conx.php';
$data = json_decode(file_get_contents("php://input"));
$id_user = $data->id_user;
$result = $db->query("SELECT * from test WHERE id_user='$id_user'");
$d = array();
while($r = $result->fetch_object())
{
$d[] = array(
"id_user" => $r->id_user,
"username" => $r->username,
"state" => $r->state
);
}
print_r(json_encode($d));
return json_encode($d);
update.php file
<?php
require 'conx.php';
$data = json_decode(file_get_contents("php://input"));
$id_user = $data->id_user;
$username = $data->username;
$state = $data->state;
$result = $db->query("UPDATE test SET username='$username' , state='$state'");
Main problem is how to set value for example for state that is always set to "close" after click on button.
Upvotes: 0
Views: 205
Reputation: 1
You can try this :
$scope.edituser = edituser;
$scope.user = {
'id_user' : $scope.update_id_user
}
function edituser() {
return $http.post('/edit.php', user).then(function(response) {
return response.data;
});
}
Upvotes: 0