I have 2 selecttag, the first with the id, ProjectId and the second
with the id, SubjectId

When the user make changes in ProjectId, the observeField will be
triggered and a controller function called ajax_calls will be
triggered. However, I failed to make the SubjectId updated with the
result from the controller.

I found no lead from 
http://www.devmoz.com/blog/2007/04/04/cakephp-update-a-select-box-using-ajax/
of which I based my work on and little help across google.

the following is my code.

View:

<?php

echo $html->selectTag('Project/id',$projectNames, null, array
('onchange' => 'setSubjects()', 'id' => 'ProjectId', 'value' => ''));

echo $html->selectTag('Subject/id', array(), null, array('id' =>
'SubjectId'));

$options = array('url' => '/radar_verbatims/ajax_calls', 'update' =>
'SubjectId', 'frequency' => '0.2');

echo $ajax->observeField('ProjectId', $options);

?>


Controller:

function ajax_calls()
        {
                $this->layout = 'ajax';

                $project_id = $this->data['Project']['id'];
                $this->set('project', $project_id);

                $sql = "SELECT ProjectsSubjects.name
                                FROM projects_subjects AS ProjectsSubjects
                                WHERE ProjectsSubjects.project_id = 
$project_id";
                $q = mysql_query($sql);

                $subjectNames = array();

                while($row = mysql_fetch_array($q))
                {
                        //$subjectNames[$row['name']] = $row['name'];
                        $subjectNames[] .= $row['name'];
                }

                $this->set('subjectNames', $subjectNames);
}

and another view, named ajax_calls.thtml

<script type = "text/javascript">
        document.getElementById('test').innerHTML = <?php print var_dump
($subjectNames); ?>;
</script>

Note: in devmoz guide, he have this code:

<?php
// update_select.(ctp|thtml)
if(!empty($options)) {
  foreach($options as $k => $v) {
     echo "<option value=’$k’>$v</option>";
  }
}
?>

I tried it but, it didn't send the result into the selecttag.

Grateful if anyone could point me my mistake. Thank you.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to