Using onChange with CakePHP

barnent1 used Ask the Experts™
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
    // Hide all fields we show/hide just to be sure we don't show the wrong ones.
    // Show the fields we want to show if the first option is selected.
    // Extend with more else..if-statements or use a switch-statement......
CakePHP view  code
<div class="rssfeeds_form"> 
<?php echo $form->create('Tweetlater');?>
         <legend class="legend_add_rssfeeds"><?php __('Add a Scheduled Tweet');?></legend>
        echo $form->input('userid', array('type' => 'hidden', 'value' => $UserID));
        echo $form->input('name', array('label' => 'Description: ', 'size' => '30')); 
        echo "Post To: ".$form->select('postto',$options_postto,'T');
        echo " ";
        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 />";
<center><?php echo $form->end('Submit'); ?></center>

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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