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?
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();'));