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 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)