dqly83915 2017-03-06 11:09
浏览 72
已采纳

AngularJS:如果没有指定新值,如何获取占位符的值

Im making a form in order to let the user update some of his personal info , but I would like to keep some placeholder values (actual and verified values - or already submitted values) if no new value is specified when submitting the form.

I don't want the user to have to re fill all fields just to update one specific field...

Is that possible, and safe by proceeding like that (xss?) I tried something for purpose but doubt it would work. (beside this, the server just went down for maintenance I guess so I can't test it right now)
Thats my php code for the request :

//get params
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);

if (isset($request->email)) {
  $email = $request->email;
}
else $email = vm.email; // app side value... of course it won't work, only for you to see what I wish to do !

$telephone = $request->telephone;
echo 'telephone<br/>'.$telephone;
$adresse = $request->adresse;
echo 'adresse<br/>'.$adresse;
$email = $request->email;
echo 'email<br/>'.$email;

// Vérification des identifiants
try {
  $req = $pdo->prepare('INSERT INTO users (telephone, adresse, email) VALUES (:telephone, :adresse, :email) ON DUPLICATE KEY UPDATE email= :email, telephone = :telephone, adresse = :adresse');
  $req->execute(array(
      'telephone' => $telephone,
      'adresse' =>$adresse,
      'email' => $email
    ));
    echo '<br>';
    print_r($req->errorInfo());
    echo '<br>updated!';
}
catch(PDOException $e)
{
  echo 'Erreur : ' . $e->getMessage();
}

?>

Angular Controller code :

// Controller profil
.controller('profilCtrl', function (user, $http, $state) {
  var vm = this;
  vm.user = user.getUserConnected();
  vm.update = update;

  function update(){

    var data = {telephone: this.telephone, adresse: this.adresse, email: this.email}
    $http({
        method: 'POST',
        url: 'http://mytpm.reaco.work/update.php',
        data: data,
        headers: {'Content-Type': 'application/json'}
    })
    .then(function(response){
      vm.data = response.data;
      vm.status = response.statusText;
      console.log('STATUS ' + vm.status);
      console.log('data ' + vm.data);
      $state.go('profil');
    }, function(error) {
        vm.data = response.data;
        vm.status = response.statusText;
        vm.errorMessage = 'ERROR';
      })
  };
  console.log(vm.user.prenom);
})

My form :

  <form name="form" ng-submit="vm.update()" novalidate>

    <label class="item item-input noborder">
      <span class="input-label"><strong>Email:</strong></span>
      <input type="email" name="email" ng-model="vm.email" placeholder="{{vm.user.email}}">
    </label>

    <label class="item item-input noborder">
      <span class="input-label"><strong>Téléphone:</strong></span>
      <input type="number" name="telephone" ng-model="vm.telephone" placeholder="{{vm.user.telephone}}">
    </label>

    <label class="item item-input noborder">
      <span class="input-label"><strong>Adresse:</strong></span>
      <input type="text" name="adresse" ng-model="vm.adresse" placeholder="{{vm.user.adresse}}">
    </label>

  <div class="item noborder">
    <button class="button button-block button-positive" type="submit">Mettre à jour mes informations</button>
  </div>
  </div>
</form>

Any help is welcome ! Im not sure if Im going in the right direction...

  • 写回答

2条回答 默认 最新

  • doulao2128 2017-03-06 14:25
    关注

    Ok I've got it ! It was dead simple actually... I was using for example vm.telephone as my placeholder and ng.model and it was actually displayed as a standard placeholder with grey characters and a smaller typo.
    I actually need to use vm.user.number (since Im using a user.service, can't go too deep into explanations here without it being complicated) as my placeholder to actually display the placeholder as input text !
    Like this : ng-model="vm.user.telephone" placeholder="{{vm.user.telephone}}
    And now my data is displayed and interpreted as real data so when I click on submit only the data I modified is actually sent to my DB. (Also, Im setting all my variables to null by default in my DB for the update to work).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集