簡體   English   中英

如何從yii2中的多選復選框輸入中選擇用戶?

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

最近,我安裝了multiSelect復選框擴展。 我的_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,
],
]);

如何在花葯模型的actionCreate中提取用戶選擇的數據? 如何獲得用戶選擇的選項? (我嘗試了$ _post [],但是沒有用。)

這是整個表格:

   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>

我嘗試的是使用默認數組將其保存到數據庫中的其他表中,並且它起作用了。

它看起來像這樣:

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