drduh44480 2013-06-17 09:54
浏览 80

使用YII框架提交表单后更新会话中的值

I really need big help for this. I have a form, and in that form client can choose country, and document type, and there is add to cart button.

So when the client select everything he need, all parametars are send into the session like this (this is in controller):

$quantity = 1; //this is default quantity for all documents
if(isset($_POST['cartBtn']))  {

        if(!isset(Yii::app()->session['cart_values'])) {
            Yii::app()->session['cart_values'] = array();
        }

        $sessionCart = Yii::app()->session['cart_values'];
        $productInfo = Product::model()->find('id=:id',(array(':id'=>$_POST['documents'])));

            $sessionCart[] = array('product_id' => $_POST['documents'], 'document' => $productInfo->name, 'countries'=>$_POST['countries'], 'quantity'=> $quantity, 'price' => $unitCost);

            Yii::app()->session['cart_values'] = $sessionCart;

        $this->redirect(array($this->id."/cart"));

    }

after submiting the form the client is redirected to cart form, here in this form he can update the quantity of documents. Now i have to determinate which column did the client select to update.

I try to do this in controller (under cart action) :

public function actionCart()
{
    if(isset($_POST['cartBtnUpdate'])) {
        $sessionCart['quantity'] = $_POST['quantity'];
    }
    $this->render('cart');
}

but when i do this nothing happened the value is 1, i enter 2 or 3 submit the form but value is 1.

Also I would like to delete the field if someone click the check box and submit the form, but i have no clue how to select the all session line to delete it.

Thanks.


Form code in cart view:

<?php
    if (is_array(Yii::app()->session['cart_values']))
    {
        $total = 0;
        foreach ( Yii::app()->session['cart_values'] as $value) {

            $total += $value['price'];

    ?>

<tr id="TDcartTable">
    <td class="docName">
      <?php echo $value['document'] ?>
    </td>
    <td>
        £ <?php echo number_format($value['price'], 2); ?>
    </td>
    <td>
       <?php echo CHtml::textField('quantity', $value['quantity']); ?>
    </td>
    <td>
        £ <?php echo number_format(($value['price'] * $value['quantity']), 2);
        ?>
    </td>
          <?php }
}
?>
    <td>

    </td>
</tr>

<tr>
    <td class="column-last" colspan="6">
    </td>
</tr>
<tr>
    <td class="cart-order-total" colspan="6">
        <?php echo CHtml::encode(Yii::t('app', 'Order Total')); ?>: £ <?php echo number_format($total, 2); ?>
    </td>
</tr>

<tr>
    <td colspan="2">
        <input type="submit" class="button" name="cartBtnUpdate" value="<?php echo CHtml::encode(Yii::t('app', 'Update Your Shopping Cart')); ?>">
    </td>
    <td colspan="4">
        <input type="submit" class="button" name="cartBtnContinue" value="<?php echo CHtml::encode(Yii::t('app', 'Continue')); ?>">
    </td>
</tr>
  • 写回答

1条回答 默认 最新

  • douraoyw194498 2013-06-17 09:58
    关注

    The variable $sessionCart is not defined in your controller action before you attempt to use it. You need to define it:

    if (isset($_POST['cartBtnUpdate'])) {
        $sessionCart = Yii::app()->session['cart_values'];
        $sessionCart['quantity'] = $_POST['quantity'];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思