简体   繁体   English

如何从yii2中的多选复选框输入中选择用户?

[英]how to get user selected from multiselect checkbox input in yii2?

Recently I installed the multiSelect check box extension. 最近,我安装了multiSelect复选框扩展。 my _form.php looks like this: 我的_form.php看起来像这样:

<? = $ Form-> field ($ model_detail, 'product_id') ->
widget (MultiSelect :: className (),
['id' => "multiXX", 
"options" => ['multiple' => "multiple"],
'data' => $ rows,
'attribute' => 'product_id', 
'model' => $ models , 
"clientOptions" =>
[
"includeSelectAllOption" => true, 'numberDisplayed' => 2,
],
]);

How can I extract the data chosen by the user in actionCreate of anther model? 如何在花药模型的actionCreate中提取用户选择的数据? how can I get the options that the user selected? 如何获得用户选择的选项? (I tried the $_post[] and it didn't worked.) (我尝试了$ _post [],但是没有用。)

this is the whole form: 这是整个表格:

   div class="customer-order2-form">


    <?php $form = ActiveForm::begin(['action' => ['create']]); ?>

    <?= $form->field($model, 'customer_name')->textInput(['maxlength' => 255]) ?>
    <?= $form->field($model, 'customer_phone')->widget(\yii\widgets\MaskedInput::className(), 
    [
    'mask' => '999-9999999',
     'clientOptions' => ['alias' =>  '972']
]) ?>

    <?php /* $form->field($model, 'customer_phone')->textInput(['maxlength' => 255]) **/?> 

    <?= $form->field($model, 'order_date')->widget(
    DatePicker::className(), [
         'inline' => false, 
      // 'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
        'clientOptions' => [
            'autoclose' => true,
            'format' => 'yyyy-m-d'
        ]
]);?>


<?php  $model_detail = new OrderDetails2(); /* -------- כאן הוספתי נתונים ממודל 
$rows=ArrayHelper::map(ProductFamily::find()->all(), 'id', 'name');  //--- אין צורך בשאילתא, עכשיו גם מציג את מה שנבחר למעלה
?>

 <?= $form->field($model_detail, 'product_id')->widget( 
 MultiSelect::className(), [
    'id'=>"multiXX",
    "options" => ['multiple'=>"multiple"], // for the actual multiselect
    'name' =>'multicheck',
    'data' =>$rows,
    'attribute' => 'product_id', // if preselected
    'model' => $models, 
    "clientOptions" => 
        [
            "includeSelectAllOption" => true,
            'numberDisplayed' => 2,
        ], 
]);
?>

<?php

echo $form->field($model, 'description')->textarea(['rows' => 6]);
echo /*$form->field($model, 'totalprice')->textInput();*/
 $form->field($model, 'totalprice')->widget(MaskMoney::classname(), [
    'pluginOptions' => [
        'prefix' => '₪ ',
        'allowNegative' => false
    ]
]);

echo $form->field($model, 'status_id')->dropDownList(ArrayHelper::map(Status::find()->select('*')->all(), 'id', 'name'));

?>

    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'יצירה' : 'עדכון', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>

</div>

what i tried is saving to different table in DB with a default Array, and it worked. 我尝试的是使用默认数组将其保存到数据库中的其他表中,并且它起作用了。

it looks like this: 它看起来像这样:

public function actionCreate()
    {
        $model = new CustomerOrder2();
        $model_details= new OrderDetails2();

    if ($model->load(Yii::$app->request->post()) && $model->save()) 
    {

if (isset($_POST['product_id'])){
     $model_details->attributes = $_POST['product_id'];
  $model_details->attributes=array();
echo 
$model_details->attributes;
}

echo $_POST['description'];
    $values =  Array('1'=>'1','2'=>'2');
    $model_details->attributes = $_POST['product_id'];
    $array=$model_details->attributes;

    //$data=OrderDetails2::->request->post();
    //$values= Array(Yii::$app->request->post()['multiXX']);
    //$values=Array($model_details->attributes);

    foreach($values as $value)
    {
        $command = Yii::$app->db->createCommand();
        $command->insert('order_details',array('Order_id'=>$model->id,
                            'product_id'=> $value,
                            'quantityOrdered'=> '1',
                            ));
        $command->execute();                    
       }

        return $this->redirect(['view', 'id' => $model->id]);
        } 

        else {
            return $this->render('create', [
                'model' => $model,

            ]);
        }
    }

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

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