dsk88199 2014-06-23 12:26
浏览 15
已采纳

如何在Yii中发布复选框以执行操作?

I want to post a checkbox to action and print alert if the checkbox is checked, here is my code:

view:

echo CHtml::checkBox('hi');
echo CHtml::button('Search', array('onclick' => 'js:document.location.href="index"'));

controller:

public function actionIndex()
    {

            $model = Jobs::model()->findAll();
            $model2 = Tags::model()->findAll();


            if(isset($_POST['hi']))
                        echo "<script>alert('hello');</script>";


            $this->render('index', array('model'=>$model, 'model2'=>$model2));
    }

when I check the checkbox and click the button nothing is happened, where is the error in my code?

  • 写回答

1条回答 默认 最新

  • dongzan7016 2014-06-23 12:31
    关注

    You just redirect your page to actionIndex. You must SUBMIT your form instead of redirecting it. Take a look:

        echo CHtml::beginForm(Yii::app()->createUrl('index'), 'POST');
        echo CHtml::checkBox('hi');
        echo CHtml::submitButton('Search');
        echo CHtml::endForm();
    

    If you try:

    CVarDumper::dump($_POST,56789,true);
    

    You can see the POST value after submitting the form.

    Or you can do it via java-script in your button:

    //if you have created a form
    echo CHtml::button('Search', array('onclick' => 'this.submit();'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口