Page 1 of 1

Module checkbox driving me nuts! [SOLVED]

Posted: Fri Jan 16, 2015 1:07 pm
by karlpers
Im trying to build a module, with some simple settings.

One of the settings is a checkbox that could be checked/unchecked.

The value is stored, and the checkbox stays the way it was saved. However, I cant get rid of the error message that appears when the checkbox is unchecked.

admin/controller:

Code: Select all

<?php if (isset($this->request->post['show_link'])) {
$data['show_link'] = $this->request->post['show_link'];
} elseif (!empty($module_info)) {
$data['show_link'] = $module_info['show_link'];
} else {
$data['show_link'] = 1;
}
admin/view:

Code: Select all

<?php if (isset($show_link)) { ?>
<label><input type="checkbox" name="show_link" value="1" checked="checked" /></label>
<?php } else { ?>
<label><input type="checkbox" name="show_link" value="1" /></label>
<?php } ?>
Result: It works, but when unchecked:

Notice: Undefined index: show_link in D:\Server\htdocs\201\admin\controller\module\simple_module.php on line 202

I have tried both with:
<?php if (isset($show_link)) { ?>

and
<?php if ($show_link) { ?>

But still that error message..

What am I doing wrong??

Re: Module checkbox driving me nuts!

Posted: Fri Jan 16, 2015 5:32 pm
by MarketInSG
try below instead

Code: Select all

} elseif (!empty($module_info['show_link'])) {

Re: Module checkbox driving me nuts!

Posted: Fri Jan 16, 2015 9:51 pm
by karlpers
Thanks a lot for the tip!

It kind of work. The error message is gone, but the checkbox is always checked.

This code seem to work properly though, even though it looks strange to me.

Code: Select all

if (isset($this->request->post['show_link'])) {
  $data['show_link'] = $this->request->post['show_link'];
} elseif (!empty($module_info['show_link'])) {
  $data['show_link'] = $module_info['show_link'];
} else {
			
}
It looks strange to have that blank line after the else statement. It works anyhow. Any better solution how to write this code? :D

Otherwise, I could live with this.

BTW, great works on your extensions!

Re: Module checkbox driving me nuts!

Posted: Fri Jan 16, 2015 11:47 pm
by fido-x
Try this:

Code: Select all

if (isset($this->request->post['show_link'])) {
    $data['show_link'] = $this->request->post['show_link'];
} elseif (!empty($module_info)) {
    $data['show_link'] = $module_info['show_link'];
} else {
    $data['show_link'] = '';
}
in your controller.

Re: Module checkbox driving me nuts!

Posted: Sat Jan 17, 2015 9:37 am
by MarketInSG
Pardon my lazy reply. What I meant was the following

Code: Select all

if (isset($this->request->post['show_link'])) {
$data['show_link'] = $this->request->post['show_link'];
} elseif (!empty($module_info['show_link'])) {
$data['show_link'] = $module_info['show_link'];
} else {
$data['show_link'] = 1;
}
You were getting the error because when you save the settings, and the checkbox is unchecked, there's actually no $module_info['show_link']. That's the thing giving you the errors. So you need to check if there's $module_info['show_link']

Re: Module checkbox driving me nuts!

Posted: Sun Jan 18, 2015 4:37 am
by karlpers
Thank you guys!

Anyway, non of these solutions works. Only code working is this:

Code: Select all

if (isset($this->request->post['show_link'])) {
      $data['show_link'] = $this->request->post['show_link'];
} elseif (!empty($module_info['show_link'])) {
      $data['show_link'] = $module_info['show_link'];
} else {
             
}
I can live with it and will not waist any more time on this :)

Thanks a lot!

Re: Module checkbox driving me nuts! [SOLVED]

Posted: Mon Jan 19, 2015 5:32 am
by Qphoria
The issue you are having is because in html, form checkboxes are only found in the POST when checked. So while you can do things like:

Code: Select all

if ($_POST['mytextbox'] != '') {
That will work if the textbox is empty OR has data in it.

You can't do that with checkboxes since the don't have an "empty" state. So you need to use:

Code: Select all

if (isset($_POST['mycheckbox'])) {
to determine if the checkbox was set.

Normally to keep things more standardized, I replace all my checkboxes with select Yes/No options. This way you can guarantee a definite existence of the form field.

Re: Module checkbox driving me nuts! [SOLVED]

Posted: Mon Jan 19, 2015 10:06 pm
by MarketInSG
karlpers wrote:Thank you guys!

Anyway, non of these solutions works. Only code working is this:

Code: Select all

if (isset($this->request->post['show_link'])) {
      $data['show_link'] = $this->request->post['show_link'];
} elseif (!empty($module_info['show_link'])) {
      $data['show_link'] = $module_info['show_link'];
} else {
             
}
I can live with it and will not waist any more time on this :)

Thanks a lot!
was was there even an empty line? You should either remove it, or set $data['show_link'] = false.

Re: Module checkbox driving me nuts! [SOLVED]

Posted: Tue Jan 20, 2015 9:28 am
by fido-x
Qphoria wrote:Normally to keep things more standardized, I replace all my checkboxes with select Yes/No options. This way you can guarantee a definite existence of the form field.
I agree with Qphoria on this. Using Yes/No radio buttons is a much better solution.

In your controller use:

Code: Select all

if (isset($this->request->post['show_link'])) {
	$data['show_link'] = $this->request->post['show_link'];
} elseif (!empty($module_info)) {
	$data['show_link'] = $module_info['show_link'];
} else {
	$data['show_link'] = '';
}
And in the template file:

Code: Select all

<label class="radio-inline">
  <?php if ($show_link) { ?>
  <input type="radio" name="show_link" value="1" checked="checked" />
  <?php echo $text_yes; ?>
  <?php } else { ?>
  <input type="radio" name="show_link" value="1" />
  <?php echo $text_yes; ?>
  <?php } ?>
</label>
<label class="radio-inline">
  <?php if (!$show_link) { ?>
  <input type="radio" name="show_link" value="0" checked="checked" />
  <?php echo $text_no; ?>
  <?php } else { ?>
  <input type="radio" name="show_link" value="0" />
  <?php echo $text_no; ?>
  <?php } ?>
</label>
Of course, you will need to ensure that "text_yes" and "text_no" have been defined in your controller:

Code: Select all

$data['text_yes'] = $this->language->get('text_yes');
$data['text_no'] = $this->language->get('text_no');
It isn't necessary to define these in your language file, as they are already defined in the top-level language file which is loaded automatically.