0

I am loading a number from the db and setting this number to the DOM using angularjs.

What I want is to sum 1 to this number, update the view and then update this new number to the db. So I was reading some questions but they use inputs to insert data into db and no example has worked for me.

html

<ion-view title="Home" id="page6" style="background-color:#FFFFFF;">
  <ion-content padding="true" style="background: url(img/gyZnJFCoRPC41przFivh_bg.png) no-repeat center;background-size:cover;" class="has-header">
    <div>
        <img src="img/XKpA1sK1QMeh5stz2Pe8_logo.png" width="100%" height="auto" style="display: block; margin-left: auto; margin-right: auto;">
    </div>
    <div id="home-markdown8" style="color:#000000;font-size:16px;text-align:center;">
        <p>Comparecerão:
            <strong>{{vou}}</strong>
        </p>
    </div>
    <div id="home-button-bar4" class="button-bar margin-top-100">
        <button id="home-button12" style="font-style:italic;color:#008BBB;border-radius:30px 30px 30px 30px;" class="button button-light  button-block audiencia_btns" ng-click="test()" name="add">Vou</button>
        <button id="home-button13" style="font-style:italic;color:#008BBB;border-radius:30px 30px 30px 30px;" class="button button-light  button-block audiencia_btns">Não Vou</button>
        <button id="home-button14" style="font-style:italic;color:#008BBB;border-radius:30px 30px 30px 30px;" class="button button-light  button-block audiencia_btns">Talvez</button>
    </div>
    <div id="home-markdown9" style="color:#000000;text-align:center;">
        <p>{{vou}} - {{nao_vou}} - {{talvez}}</p>
    </div>
    <center>
        <a ui-sref="menu.temporada2016" id="home-button15" style="font-style:italic;color:#008BBB;font-size:18px;margin-top:50px;border-radius:30px 30px 30px 30px;width:250px;" class="button button-light  button-block icon-right ion-chatbubbles pics-news-btn">Fotos e Novidades</a>
    </center>
  </ion-content>
</ion-view>

angularjs:

.controller('homeCtrl', ['$scope', '$http', function ($scope, $http) {

$http.get("http://localhost/select-audiencia.php").then(function(response){
    //console.log(response);
    //console.log(JSON.stringify(response));

    $scope.all = response;
    $scope.vou = Number(response.data[0].VOU);
    $scope.nao_vou = Number(response.data[0].NAO_VOU);
    $scope.talvez = Number(response.data[0].TALVEZ);

});

$scope.test = function(){ 

    $scope.vou += 1;

    $http.post("http://localhost/insert-audiencia.php", {'vou': $scope.vou}).then(function(resp){
        console.log(JSON.stringify(resp));
        console.log(resp); 
    });
  }

}])

php

<?php 

  include_once('conn.php');

  if(isset($_POST['add'])){

    $vou = $_POST['vou'];
    $sql = $mysqli->query("UPDATE audiencia SET vou = '".$vou."'");

}

?>

This is my callback

{"data":"","status":200,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"url":"http://localhost/insert-audiencia.php","data":{"vou":2},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":"OK"} 

I can see the new number into "data":{"vou":2} but it is not working with php. Any suggestion?

madsongr
  • 653
  • 1
  • 11
  • 29

1 Answers1

0

I fixed my php file using $data as a JSON object

<?php

include_once('conn.php');

$data = json_decode(file_get_contents("php://input"));

if(property_exists($data, 'vou')){

  $vou = $data->vou;
  $sql = $mysqli->query("UPDATE audiencia SET vou = '".$vou."'");
  $mysqli->close();

}else if(property_exists($data, 'getnaovou')){

  $naovou = $data->getnaovou;
  $sql = $mysqli->query("UPDATE audiencia SET nao_vou = '".$naovou."'");
  $mysqli->close();

}

?>
madsongr
  • 653
  • 1
  • 11
  • 29