简体   繁体   English

从文本框中获取数据并传递给 yii2 中的控制器

[英]Get data from textbox and pass to Controller in yii2

Itis the same question as Pass data from a form to a controller in yii2 .这与将数据从表单传递到 yii2 中的控制器的问题相同 I want to get data from user in 3fields and pass it to a controller action.我想从 3fields 中的用户获取数据并将其传递给控制器​​操作。 I'm trying to do this by javascript but it seems that the javascript function is not being called.我正在尝试通过 javascript 执行此操作,但似乎没有调用 javascript 函数。

Nothing happens when I click on the button.当我点击按钮时什么也没有发生。 No error in the console as well.控制台中也没有错误。

I'm attaching the code of 3 fields,button and javascript below.我在下面附上 3 个字段、按钮和 javascript 的代码。

index2.php索引2.php

<?php

use yii\helpers\Html;
//use yii\grid\GridView;
use kartik\grid\GridView;
use kartik\export\ExportMenu;
use frontend\modules\stock\models\Sellitem;
use dosamigos\datepicker\DatePicker;
use dosamigos\datepicker\DateRangePicker;
use kartik\form\ActiveForm;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\stock\models\SellitemSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Stock';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="sellitem-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <div class="row">
        <div class="form-group">
            <div class="col-xs-4 col-sm-4 col-lg-4">
                <label for="upc" class="control-label"><p class="text-info">Product Code&nbsp;<i class="icon-star"></i></p></label>
                <input type="text" class="form-control" id="upc" class="span3">
            </div>
            <div class="col-xs-4 col-sm-4 col-lg-4">
                <label for="upc" class="control-label"><p class="text-info">Start Date&nbsp;<i class="icon-star"></i></p></label>
                <?= DatePicker::widget([
                //'label' => 'Startdate',
                'name' => 'startdate',
                'id' => 'startdate',
                //'value' => '02-16-2012',
                'template' => '{addon}{input}',
                    'clientOptions' => [
                        'autoclose' => true,
                        'todayHighlight' => true,
                        'format' => 'yyyy-mm-dd'
                    ]
                ]);?>
            </div>
            <div class="col-xs-4 col-sm-4 col-lg-4">
                <label for="upc" class="control-label"><p class="text-info">End Date&nbsp;<i class="icon-star"></i></p></label>
                <?= DatePicker::widget([
                //'label' => 'Startdate',
                'name' => 'enddate',
                'id' => 'enddate',
                //'value' => '02-16-2012',
                'template' => '{addon}{input}',
                    'clientOptions' => [
                        'autoclose' => true,
                        'todayHighlight' => true,
                        'format' => 'yyyy-mm-dd'
                    ]
                ]);?>
            </div>
        </div>

    </div>
    <p>
        <div class="form-group pull-right">
            <button class="btn btn-primary" onclick="getValue()">Seach</button>           
        </div>
    </p>
</div>
<?php
/* start getting the itemid */
$this->registerJs('
function getValue()
    {
        var uPc = $(".upc").val();
        var startDate = $(".startdate").val();
        var endDate = $(".enddate").val();

        alert(uPc);

    }
');
/* end getting the itemid */
?>

在此处输入图片说明

The registerJs use some jQuery assigning so seems that the function getValue is not visible by the button call but you could use jQuery for assign the onclick and code. registerJs 使用一些 jQuery 分配,因此按钮调用似乎看不到getValue函数,但您可以使用 jQuery 分配 onclick 和代码。

Assuming your button has a id named yourButton you could do this way假设您的按钮有一个名为yourButton的 ID,您可以这样做

<div class="form-group pull-right">
    <button id="yourButton" class="btn btn-primary" >Seach</button>           
</div>

$this->registerJs (
      "$('#yourButton').on('click', function() { 
          var uPc = $('#upc').val();
          var startDate = $('#startdate').val();
          var endDate = $('#enddate').val();

          alert(uPc);
      });"
  );

In your javascript code you have '.upc' '.startdate' '.enddate' this mean that you are looking for class upc, startdate endate ... but in your html there is not this class associated to the input field .. you have id then you should search in jQuery using #udc #startdate ... an so在您的 javascript 代码中,您有 '.upc' '.startdate' '.enddate' 这意味着您正在寻找类 upc,startdate endate ...但在您的 html 中,没有此类与输入字段相关联..你有 id 那么你应该使用 #udc #startdate 在 jQuery 中搜索......

you can do it using ajax by serializing your data and send it to controll action like您可以使用 ajax 通过序列化您的数据并将其发送到控制操作来做到这一点,例如

    $('body').on('click','#btn_id',function(){
      var data = $('form#id').serialize();
      $.ajax({
          'type' : 'get/post',
           'dataType' : 'html/json',
           'url' : 'controller/action',
           'data' : {variable:data},
            success : function(data){
               console.log('Data Pass Successfully');
},
            error : function(){
              console.log('There is an Error....!!!');
}
});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM