1

I have written the following piece of code in my view page

<script type="text/javascript">
function filldetails()
{
    document.getElementById('FirstName').value  = "hjshjsh"; 
}
</script>
echo $this->Form->select('students',$student_name,array('onchange' =>filldetails()));

but i am getting an error message

call to undefined function filldetails()

How do I solve this error?

1
  • Since the JS function filledetails is being called, it would suggest that you haven't defined it properly. Can you provide more information on where you've defined the filldetails() function and ensure that it's wrapped in <script type="text/javascript"></script>. I don't think it's a cake php error per se Commented Mar 16, 2011 at 12:22

2 Answers 2

5

it should be 'onchange' => 'filldetails()'

Sign up to request clarification or add additional context in comments.

Comments

0

Unless your project has a specific reason to avoid JS frameworks, you will avoid a lot of long-term headaches by using jQuery instead of pure Javascript.

Rewrite your view thus:

<?php
  $selectDomId = $this->Form->domId('students');
  $firstnameDomId = $this->Form->domId('Student.first_name');

  $this->Html->scriptBlock("
      jQuery(function($){
        $('#{$selectDomId}').change(function(event){
          $('#{$firstnameDomId}').val( $(this).val() );
        });
      });
  ",array('inline'=>false));
?>
<?php echo $this->Form->select('students',$student_name,$this->Form->domId(array(),'students'))?>
<?php echo $this->Form->input('Student.first_name')?>

The jQuery takes care of the onChange event handler, so it doesn't clutter your HTML, by hooking onto your dropdown menu's change event.

The use of Helper::domId means you don't have to worry about how CakePHP's helpers generate their id attributes, which is a net win in reliability and maintainability.

2 Comments

But it is not working , do i need to import some jquery library files
hi Daniel, on change the value is not filled up.can you please help me to solve this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.