0

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.

1 Answer 1

0

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;
        });
}
Sign up to request clarification or add additional context in comments.

1 Comment

I got an error ReferenceError: id_user is not defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.