How To Insert Multi Records In Yii Session Using Ajaxsubmitbutton

Hi Everybody,

Hope you are doing well. I fall in an issue that is, I have an Order form where I select a registrant (all registrants come from database) from a dropdown, an item (all product items come from database) from dropdown and a input textbox where I type the quantity. There is a "ajaxSubmitButton" button to send all these values to the controller "actionCart" using Ajax. After receiving all values in the controller, I want to put all values in session variable. When I add another new item, the session values are being replaced what I want to hold all newly added items into the session variable. In this circumstance, what should I do. Please help me. I am giving my code snippets below:

In form:




<div class="form">

<?php

$form=$this->beginWidget('CActiveForm', array(

'id'=>'order-form',

'enableAjaxValidation'=>false,

    'htmlOptions'=>array('class'=>'form-horizontal' , 'enctype'=>'multipart/form-data', ),


)); ?>


<div class="alert alert-info" xmlns="http://www.w3.org/1999/html">

    <p class="note">Fields with <strong><span class="required">*</span></strong> are required.</p>

</div>


<?php echo $form->errorSummary($model); ?>


<div class="form-group">

    <?php echo $form->labelEx($model,'registration_id', array('class' => 'control-label col-lg-4')); ?>

    <div class="col-lg-8">

        <?php

        $data = CHtml::listData(Registration::model()->findAll(),'id', 'name');

        echo $form->dropDownList($model,'registration_id',$data,array('class' => 'form-control chzn-select','prompt'=>'Select a Registrant'));

        ?>

    </div>

    <?php echo $form->error($model,'registration_id'); ?>

</div>


<div class="form-group">

    <?php echo $form->labelEx($model,'item', array('class' => 'control-label col-lg-4')); ?>

    <div class="col-lg-8">

        <?php

        $data = CHtml::listData(Products::model()->findAll(),'id', 'name');

        echo $form->dropDownList($model,'item',$data, array('class'=>'form-control chzn-select' , 'id'=>'item', 'prompt'=>'Select an Item')); ?>

        <?php

    ?>

    </div>

    <?php echo $form->error($model,'item'); ?>

</div>


<div class="form-group">

    <?php echo $form->labelEx($model,'quantity', array('class' => 'control-label col-lg-4')); ?>

    <div class="col-lg-2">

        <?php

        echo $form->textField($model,'quantity',array('class' => 'form-control','size'=>60,'maxlength'=>11));

        ?>

    </div>

    <?php echo $form->error($model,'quantity'); ?>

</div>


<div class="form-group">

    <div class="col-lg-8 pull-right">

    <?php

    echo CHtml::ajaxSubmitButton('Add to Cart',Yii::app()->createUrl('admin/order/cart'),

        array(

            'type'=>'POST',

            'update'=>'#cartResult',

        ),

        array('class'=>'btn btn-primary btn-sm',));

    ?>

    </div>

</div>

<div class="form-group">

    <div id="cartResult" class="col-lg-12">

    </div>

</div>

<?php $this->endWidget(); ?>

</div>



In Controller:




public function actionCart()

{

    if(isset($_POST["Order"])){

        $item = $_POST["Order"];

        $registration_id = $item["registration_id"];

        $productId = $item["item"];

        $quantity = $item["quantity"];

        $quantity = $item["quantity"]=='' ? 1 : $item["quantity"];


        $productInfo = Products::model()->findByPk(array('id'=>$productId));

        $totalPrice = $productInfo->price * $quantity;


        $session = Yii::app()->session;

        $session['cart'] = array("product_id" => "$productId" , "product_name" => "$productInfo->name", "quantity" => "$quantity","price" => "$productInfo->price");


    }


}



Thanks in advance,

Shimul

I think the better way is to store in a temporary cart table. All the shopping cart application do like that.

Thanks for help. I’ll do.

Very good…