YII 前台后台快速接收和保存新旧数据

1.在view层

<form method="post" class="form-horizontal" id="volun" >
    <div class="form-group">
        <label class="col-sm-2 control-label" for="field_order_connection">其他联系方式</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="VolunteerinfoMore[field_order_connection]" id="field_order_connection"
                   value="<?php echo isset($volun->more) ? $volun->more['field_order_connection'] : ''; ?>">
        </div>
    </div>
</form>

<script>
$.post('<?php echo SITE_URL;?>' + 'adminPeople/addPeople',$('#volun').serialize(), function (data) {
    var res = $.parseJSON(data);
    if (res == 'true') {
        alert("更新成功!");
        $('#reg1').attr('disabled', 'disabled');
        location.href = '<?php echo SITE_URL;?>adminPeople/listVolunteer';
    }else {
        alert("更新失败!");
    }
});
</script>

在action层:
$flag=VolunteerinfoMore::insertVolun($volunteer->volun_id);
if(!$flag){
    echo CJSON::encode('false');
    Yii::app()->end();
}
在model层:
    public static function insertVolun($volun_id)
   {
      $volun_more=VolunteerinfoMore::model()->findByAttributes(['volun_id'=>$volun_id]);
      if (!empty($volun_more)) {
         $volun_more->attributes=$_POST['VolunteerinfoMore'];
         if ($volun_more->save()) {
            return true;
         };
      }else{
         $model=new VolunteerinfoMore();
         $model->attributes=$_POST['VolunteerinfoMore'];
         $model->volun_id=$volun_id;
         if($model->save())
            return true;
      }
      return false;
   }
}

您可以选择一种方式赞助本站

支付宝转账赞助

支付宝扫一扫赞助

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

图片 表情