Using onChange with CakePHP

barnent1
barnent1 used Ask the Experts™
on
Dear Experts,

I have a good one today. I have a view in cakephp in which has several dropdown list boxes.

FreqType which has H = Hour, D = Day and N = Now

The desired result is when someone selects from the FreqType listbox. It determines if one of the other other listboxes show.

The problem is that the onchange event is not firing. If I code the ddlistbox in regular html with out using the cake functions it works. Here is the current code

Oddly the view source shows the correct HTML.
javascript - if I can get the code to just fire the alert I have defined I would be happy.
 
<script language="javascript">
function showFields(){    
    var selected = document.getElementById('freq_type').value
         alert(selected);
    // Hide all fields we show/hide just to be sure we don't show the wrong ones.
    document.getElementById('div_freq').style.display="block"
    document.getElementById('d').style.display="none"
    document.getElementById('e').style.display="none"
 
    if(selBox.selectedIndex==0){
    // Show the fields we want to show if the first option is selected.
        document.getElementById('c').style.display="block"
    }else{
    // Extend with more else..if-statements or use a switch-statement......
    }
}
</script>
 
CakePHP view  code
 
<div class="rssfeeds_form"> 
<?php echo $form->create('Tweetlater');?>
    <fieldset>
         <legend class="legend_add_rssfeeds"><?php __('Add a Scheduled Tweet');?></legend>
    <?php
        echo $form->input('userid', array('type' => 'hidden', 'value' => $UserID));
        echo $form->input('name', array('label' => 'Description: ', 'size' => '30')); 
        $options_postto=array('T'=>'Twitter','P'=>'PingFM');
        echo "Post To: ".$form->select('postto',$options_postto,'T');
        echo " ";
        $options_freq_type=array('H'=>'Hour','D'=>'Day','N'=>'Now');
        echo "Frequency Type: ".$form->select('freq_type',$options_freq_type,'H', array('onChange'=>'showFields(this)'));        
        echo " ";        
        $options_freq=array('1'=>'Every Hour','2'=>'Every 2 Hours','3'=>'Every 3 Hours','6'=>'Every 6 Hours','8'=>'Every 8 Hours','12'=>'Every 12 Hours');
        echo "Frequency: ".$form->select('freq',$options_freq,'1', array('showEmpty'=>'false'));
        echo " ";
        $options_num_posts = array('0'=>'0','1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5');
        echo "Number of Posts: ".$form->select('numposts',$options_num_posts,'0');
        echo "<br />";
    ?>
    </fieldset>
<center><?php echo $form->end('Submit'); ?></center>
</div>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Dear Experts,

I figured it out. CakePHP creates it's own id's for the elements and so the element id for the freq_type dropdown was named differently. I found this by viewing the source of the page.

After changing the javascript to the correct id's the code now works perfectly.

So All those out there that want to use on change from a form->select the code in this example is correct.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial