Collection attribute not updated immediately

Permalink
I am building a image database. One option is to select several images to download them as a zip or send them via email.

I am using the 'is_featured' attribute for this (since I can easily filter within pagelists).

I am using pageControllers and the function I'm using to toggle the is_featured is:

public function changeimgfav() {
    $cID = $this->post('cID');
    $newValue = $this->post('newValue');
    $c = Page::getByID($cID);
    if ($newValue) {
       $c->setAttribute('is_featured', true);
    } else {
       $c->setAttribute('is_featured', false);
    }
}


Which is called by this form:
echo '<form method="POST" action="'.$this->action('changeimgfav').'" id="FEAT_'.$c->getCollectionID().'">';
echo '<input type="hidden" name="cID" value="'.$c->getCollectionID().'"/>';
echo '<input type="hidden" name="newValue" value="'.($c->getAttribute('is_featured')?0:true).'" />';
if ($c->getAttribute('is_featured')) {
   echo '<a id="favButton" class="btn btn-small" href="#">
        <span id="favButtonSpan" class="btn-icon-phone">Remove</span></a>';
} 
else {
   echo '<a id="favButton" class="btn btn-small" href="#">
       <span id="favButtonSpan" class="btn-icon-phone">Add</span></a>';
}
echo '</form>';


The form is submitted via JavaScript:
<script> 
document.getElementById('favButton').addEventListener('click', function() {     
       document.getElementById('FEAT_<?php echo $c->getCollectionID();?>').submit();
}, false);
</script>


I am experiencing a strange behavior. The is_featured is only toggled every second click... not sure why. It seems like the page reload is faster, than the setAttribute in the PageController. If I use a PageList to display a list of images, the is_featured is always displayed correctly. Any hints?

msglueck
 
msglueck replied on at Permalink Reply
msglueck
I changed the ($c->getAttribute('is_featured')?0:true) from ($c->getAttribute('is_featured')?false:true), since the rendered html for the hidden attribute was value instead of value="0". but no change in behavior.
msglueck replied on at Permalink Reply
msglueck
and it's an 5.6.0.2
msglueck replied on at Permalink Reply
msglueck
found a workaround/hack:

<?php
   if ($_POST['cID']) {
      echo 'window.location.href="'.$c->cPath.'"';
   }?>


If I'm on the page after the form submit I reload the page once.... not good, but working.

Would be interested on insights why my initial solution didn't work...