• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Conditionn Checkbox

#1
Hello !
I would like to create a condition with a checkbox :)
I have added this in a template :
HTML:
<input type="checkbox" name="checkBox">
And I would like to do a condition if the checkbox is checked :)
Do i need to add PHP file via FTP or not necessary ?

Thanks
 
#2
EDIT :

I have added the checkbox in the template : test_Template
And I would like to get the value of the checkbox (ticked or not) in a PHP file How can I do this ?

Example :
PHP:
public function actionIndex(){
        $options = XenForo_Application::get('options');
        $visitor = XenForo_Visitor::getInstance();
       
         if (checkboxIsTicked) {
                // Do Stuff
         }
Maybe I need to use this :
PHP:
$_POST['checkBox'];
?
 
#6
Still doesn't work :(

PHP:
$checkBox = $this->_input->filterSingle('userBox', XenForo_Input::BOOLEAN);
           
            if ($checkBox)
            {           
               $messageId = XenForo_Application::get('options')->messageId;
           
               $dw = XenForo_DataWriter::create('Test_DataWriter_Test');
               $dw->set('user_id', 2);
               $dw->set('username', 'hakdi');
               $dw->save();
            }
            else
            {
                $dw = XenForo_DataWriter::create('Test_DataWriter_Test');
               $dw->set('user_id', $visitor['user_id']);
               $dw->set('username', $visitor['user_id'] > 0 ? $visitor['username'] : new XenForo_Phrase('guest'));
               $dw->save();
            }
 
#10
Not work too :(

I tried to edit this :
PHP:
if ($checkBox)
By :

PHP:
if (isset($checkBox))
The results change but when I tick the result still the same :/
 
#12
Yes but still not mork :/

PHP Code :
PHP:
$checkBox = $this->_input->filterSingle('checkBox', XenForo_Input::BOOLEAN);
           
            if ($checkBox)
            {           
               $messageId = XenForo_Application::get('options')->messageId;
           
               $dw = XenForo_DataWriter::create('Test_DataWriter_Test');
               $dw->set('user_id', 2);
               $dw->set('username', 'flo');
               $dw->save();
            }
            else
            {
                $dw = XenForo_DataWriter::create('Test_DataWriter_Test');
               $dw->set('user_id', $visitor['user_id']);
               $dw->set('username', $visitor['user_id'] > 0 ? $visitor['username'] : new XenForo_Phrase('guest'));
               $dw->save();
            }
My template :
HTML:
<input type="checkbox" name="checkBox" value="1" />
 

Lawrence

Well-known member
#16
I'll assume your form action is set to point to where your check box value will be read.
If it is set correctly, then the post made by @Nobita.Kun will work. As it doesn't work you will need to post more of your code (template, and the method) for us to help :)

PHP:
if ($this->_input->filterSingle('nameOfCheckbox', XenForo_Input::BOOLEAN);)
{
       // do something
}
else
{
     // do something else
}
 
#17
I'll assume your form action is set to point to where your check box value will be read.
If it is set correctly, then the post made by @Nobita.Kun will work. As it doesn't work you will need to post more of your code (template, and the method) for us to help :)

PHP:
if ($this->_input->filterSingle('nameOfCheckbox', XenForo_Input::BOOLEAN);)
{
       // do something
}
else
{
     // do something else
}
I'll pm you with the entire code :p