Jump to content
onjegolders

Unchecked checkboxes issue

Recommended Posts

Hey guys, I'm having an issue where I have a checkbox for deciding whether a link is shown to a website. I have a field to hold the url and the checkbox field.

My code is:

if ($page->project_url AND $page->show_url = 1) { ?>
<a href="<?php echo $page->project_url; ?>" target="_blank" class="project_link flr tar">←  Visit Site</a>
<?php }

I have also tried in parentheses:

if ($page->project_url AND ($page->show_url = 1)) { ?>
<a href="<?php echo $page->project_url; ?>" target="_blank" class="project_link flr tar">←  Visit Site</a>
<?php }

The problem is, the links are displaying regardless.

Any ideas what I may have done wrong? I'm still wearing my dunce's hat by the way...

Share this post


Link to post
Share on other sites

yeah it's "==" not "=".

if you use "value == field" it won't happen as it will throw an error.

Share this post


Link to post
Share on other sites

A handy bit of info on comparison operators: http://php.net/manual/en/language.operators.comparison.php

In your case you're making $page->show_url equal to 1 regardless of it's true value by setting it to 1 using just one '=' whereas you need two '==' to compare the field value against another value.

Granted, PW's selectors only use one = but they're a bit special like that ;)

Share this post


Link to post
Share on other sites

Thanks both of you, it doesn't seem to work though with "==" either.

I'm sure I've seen checkboxes with single "=" too as Pete mentions.

The field is definitely called show_url so not too sure what is happening here.

Is there any way to debug?

Thanks.

Share this post


Link to post
Share on other sites

Just an idea, what do you get doing the following:

var_dump($page->project_url);

Because if $page->project_url is null/false or an empty String '', the if condition will never be true.

Empty strings are converted to false by PHP.

Edit:

Oops just realized that your problem's the opposite. :rolleyes:

You must use the "==" operator.

What do you get when you're doing this on a page where show_url should be false:

var_dump($page->show_url);

Share this post


Link to post
Share on other sites

A single "=" will only work in PW's selectors like $page->find('something=something') and so on - you must have double == to compare values in normal PHP as per the link I posted.

Strange that that's not working either though, but a single "=" is definitely wrong.

Just a thought - what is the expected value of $page->project_url? If it's just a text field then you might want to check it's not empty with if (!empty($page->project_url as it would exist whether empty or not, so simply doing if ($page->project_url would always be true I think. If on the other hand it is a Page fieldtype, then try if ($page->project_url->id instead.

Share this post


Link to post
Share on other sites

Thanks for the reply Wanze, I get the following:

int(0)

Not sure what's going on here...

Share this post


Link to post
Share on other sites

Hi Pete, sorry we must have posted at the same time!

Thanks for clearing up the "=" thing.

The value of project_url is a url (I guess a text string) but my if statement specifies project_url AND show_url so both criteria ought to be met before outputting the url.

I'm guessing the int(0) means that show_url is unchecked?

Which is sort of good news I suppose except that it's still displaying, I will have to go over it again to see if I'm missing something stupid!

Share this post


Link to post
Share on other sites

Does your if statement now look like this?

if ($page->project_url AND $page->show_url == 1) {

This should never be true if show_url is int(0).... hmmm

Share this post


Link to post
Share on other sites

Wanze, I've just copied and pasted your if statement and I don't believe it because it works! So strange as I have tried (or so I thought), every permutation with both single and double "=".

I must have missed something out, though I was sure I tried this exact statement! Strange....

Anyway, a big thank you for all of your help.

Share this post


Link to post
Share on other sites

I guess you got this thing because you used AND operator, not &&. AND has less priority then && and combined with = (assign operstor) instead of == (equality comparison operator) it gave you this strange behaviour.

I guess you could also do this, granted your $page->show_url takes values only of 1 and 0:

if ($page->project_url && $page->show_url) {
//do your thing
}
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By killedfriendz
      I am very sorry for asking this but i totally do not understand how to set values of checbox using API. 
      I have checbox field on my page with name "order_status". 
      So i've tried few ways to make it checked but it still doesn't work:
       
      $userPage->order_status->value = 1; $userPage->order_status->add(1); $userPage->order_status->add(true); Could you please tell me how to do it?
    • By cosmicsafari
      Hi all,
      Before I go potentially wasting time trying to achieve the impossible.
      Can anyone confirm if its possible to have a Page Reference field on a modules config page?
      I'm wanting to essentially just output a list of select able pages based on the a given selector (likely by template at this stage), wherein the select is the pages that the module should apply to etc. I was thinking a simple checkbox list would suffice is asmSelect isn't available.
      Essentially have it display the same way a Page Reference field would display on a template, where you can easily select a bunch of them.
      public function getInputfields() { $inputfields = parent::getInputfields(); $f = $this->modules->get('InputfieldPage'); $f->attr('name', 'testSelect'); $f->setAttribute('multiple', 'checkboxes'); $f->setAttribute('findPagesSelector', 'template=development'); $f->label = 'Test'; $inputfields->add($f); return $inputfields; } Figured something akin to the above would work but can't seem to get rid of this warning on the modules config screen though.

    • By ngrmm
      I have a page with a table. Each table row has a page-reference field and a checkbox.
      The Page sends emails to all users (page-refrence->email-field) and change the value of the checkbox in a row to 1.
      It works with this:
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?> But i have to use a variable in my emailbody.inc which i'm able to get in the table-loop.
      So i do the including of the body inside my loop. But this doesn't work anymore. Page sends out the emails but is unable to change the value of the checkbox.
      I get no errors!
      I'm using ProTable
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?>  
    • By louisstephens
      Is it possible to use count() to return a number of repeater items don't have a checkbox checked? In my current set up, I have a repeater on the page "dev_repeater" with a checkbox called "dev_repeater_exclude". I need to get a count of the current items that do not have it checked so I can pass it to my css grid to alter the column width.
    • By Chris Bennett
      Not sure where I originally saw it while lurking around the forums, but someone, somewhere at some time was asking about styling Uikit checkboxes as toggle-style switches, much like the ones at the bottom of this post asking me if I want to be notified of replies.
      So here is my humble offering, rough and ready,  which can be thrown in at the bottom of your Uikit css as a starting point.
      Everything is based on ems and rems, so you can increase size as you desire by altering the single instance of font-size.
      It only targets single instances of labels within a specific field to a) try to limit unintended consequences and b) because in those cases it often seems more user-friendly to have an on/off binary switch rather than a checkbox. It's still totally a checkbox, just styled differently.
       
      .uk-form-controls-text label:only-of-type input.uk-checkbox { font-size:.8rem; margin-top:0; position:relative; -webkit-appearance:none; outline:none; width:4em; height:2.4em; border:2px solid #D6D6D6; border-radius: 3em; box-shadow:inset 5em 0 0 0 #DDD; flex-shrink: 0; } .uk-form-controls-text label:only-of-type input.uk-checkbox:after { content:""; position:absolute; top:.25em; left:.25em; background:#FFF; width:1.6em; height:1.6em; border-radius:50%; transition:all 250ms ease 20ms; box-shadow:.05em .25em .5em rgba(0,0,0,0.2); } .uk-form-controls-text label:only-of-type input.uk-checkbox:checked { background-color: transparent; box-shadow:inset 5em 0 0 0 #4ed164; border-color:#67bba5; } .uk-form-controls-text label:only-of-type input.uk-checkbox:checked:after { left:1.85em; box-shadow:0 0 1em rgba(0,0,0,0.2); } label:only-of-type input.uk-checkbox:checked + span { color:#008a00; transition:all 250ms ease 20ms; } .InputfieldCheckbox .InputfieldContent label:only-of-type {display:flex;} label:only-of-type input.uk-checkbox + span { color:#c3c3c3; display:flex; align-items:center; line-height:1.1; } /* Below is only necessary if you want the optional "tick" after items when selected */ label:only-of-type input.uk-checkbox + span:after { flex-shrink:0; margin-left:.5em;width:2em; opacity:0; content:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 250 250'%3E%3Ccircle cx='125' cy='125' r='125' fill='%23231F20' opacity='.1'/%3E%3Cpath fill='%230B8B44' d='M95.823 139.432l-32.492-32.56-31.872 31.883-.008-.008 63.72 63.732L218.549 79.116 187.494 47.52z'/%3E%3C/svg%3E"); } label:only-of-type input.uk-checkbox:checked + span:after { opacity:1; transition: opacity 250ms ease 150ms; }  

×
×
  • Create New...