Post by karlpers » Fri Jan 16, 2015 1:07 pm

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??
Last edited by karlpers on Sun Jan 18, 2015 4:37 am, edited 1 time in total.

Active Member

Posts

Joined
Tue Jun 09, 2009 10:35 pm

User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by karlpers » Fri Jan 16, 2015 9:51 pm

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!

Active Member

Posts

Joined
Tue Jun 09, 2009 10:35 pm

Post by fido-x » Fri Jan 16, 2015 11:47 pm

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.

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia

Post by MarketInSG » Sat Jan 17, 2015 9:37 am

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']


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by karlpers » Sun Jan 18, 2015 4:37 am

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!

Active Member

Posts

Joined
Tue Jun 09, 2009 10:35 pm

Post by Qphoria » Mon Jan 19, 2015 5:32 am

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.

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by MarketInSG » Mon Jan 19, 2015 10:06 pm

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.


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by fido-x » Tue Jan 20, 2015 9:28 am

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.

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia
Who is online

Users browsing this forum: No registered users and 5 guests