duandiaoqian5795 2016-06-28 05:55
浏览 207
已采纳

如何使用$ .post()发送请求并在yii2中向数据库添加数据

I would like to achieve something like these below code. just that the code is making a request from the database, i want to send request to the database

$.post( "/trobay/categories/default/lists?parent_id="+$(this).val(), function( data ) {

how to send request adding attribute name and attribute value to be save in DB below is my code i have already

    <?php
  $script = <<< JS
  $(document).ready(function(){
    //setup before functions
         var typingTimer;                
         var doneTypingInterval = 3000;  
         var \$TitleInput = $('#product-product_title');

         //on keyup, start the countdown

        \$TitleInput.on('keyup input change paste',function(){
            clearTimeout(typingTimer);
            if (\$TitleInput.val()) {
                typingTimer = setTimeout(doneTyping, doneTypingInterval);
            }
        });



         //user is "finished typing," do something
         function doneTyping () {

             data = \$TitleInput.val();

                 $.ajax({
        url: '/trobay/draft/create',
        type: 'POST',
        data: data,
        success: function (data) {
                      alert(data)
            },
            error: function(jqXHR, errMsg) {
             // handle error
                alert(errMsg);
            }
        });


         }

  });
 JS;
 $this->registerJs($script);
 ?>

in my controller i have this

public function actionCreate()
{
    $model = new Draft();

    if ($model->load(Yii::$app->request->post())) {
        $model->created_at = \time();
        if($model->save()){
            return draftId;
        }else{
            return '0';
        }

    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

and in my view i have this

<?php $form = ActiveForm::begin(['id'=>$model->formName(),
                                          'enableClientValidation'=> true,
                                          'fieldConfig' => ['template' => '{label}{input}{hint}']]); ?>
        <div class="row">
           <div class="col-md-12">
              <?= $form->field($model, 'product_title')->textInput([
                'class'=>'title-input',
                'placeholder' => 'Give us a title for your items(include size,brand,color,material. e.t.c)',
                ])->label(false) ?>
    </div>
            <div class="col-md-12 text-muted">
      E.g Men's blue addidas glide running shoes size 11 
    </div>
        </div>
    <?= $form->field($model, 'user_id')->textInput() ?>

<?= $form->field($model, 'product_title')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'product_name')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'product_description')->textarea(['rows' => 6]) ?>

<?= $form->field($model, 'category_id')->textInput() ?>

Now my question is I just want to grab the value of each field input and send it to server side for save in the DB without having to submit the whole form The about JS code works enough to get the value but how do i send this to the server for save up

  • 写回答

1条回答 默认 最新

  • dongxing8009 2016-06-28 06:23
    关注
        //user is "finished typing," do something
        function doneTyping () {
            data = \$TitleInput.val();
            $.ajax({
                  url: '/trobay/draft/create',
                  type: 'POST',
                  data: {title: data}
                  success: function (data) {
                           alert(data)
                  },
                  error: function(jqXHR, errMsg) {
                        // handle error
                        alert(errMsg);
                  }
            });
        });
    

    If you need to send more key value just add those as comma seperated value like

    data: {title: data, attr:"value"} 
    

    and so on...and you can receive this value as post parameter in your controller.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器