I'm using cakephp 2x. there is a model of doctor and appointment. in appointment controller receptionist can create appointment. When he select a doctor dynamically it will show the doctor's visit_start and visit_end in the appointment form from the doctors table through ajax. But my code is not working ajax function. Please help me to sort out the problem. Thank you
This is admin_add of appointments view.
<div class="appointments form">
<?php echo $this->Form->create('Appointment'); ?>
<fieldset>
<h2><?php echo __('Create Appointment'); ?></h2>
<?php
echo $this->Form->input('doctor_id', array(
'id' => 'doctor_id',
'empty' => '-----Select Doctor-----' ));
?>
<div id="visit">
</div>
<?php
echo $this->Form->input('patient_id');
echo $this->Form->input('appointed_time');
?>
</fieldset>
<?php echo $this->Form->end(__('Create')); ?>
</div>
<!-- document javascripts -->
<script type="text/javascript">
$(document).ready(function () {
$('#doctor_id').change(function(){
$doctor_id= $('#doctor_id :selected').val();
alert($doctor_id);
$.ajax({
type: 'POST',
url: "<?php echo Router::url(array('controller' => 'doctors',
'action' =>'get_by_doctor'), true); ?>",
cache: false,
data: doctor_id,
success: function(data) {
document.getElementById("#visit").innerHTML = response;
},
error:function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
return false;
});
});
</script>
<?php
DoctorsController
public $components = array(
'Acl',
'Auth',
'Session',
'Security',
'Cookie',
'Paginator',
);
public $helpers = array('Html', 'Form', 'Js');
// AJAX
public function admin_get_by_doctor($id=null) {
$doctor_id = $this->request->data['Doctor']['doctor_id'];
$doctor = $this->Doctor->find('first', array(
'conditions' => array('Doctor.doctor_id' => $doctor_id),
));
$this->set('doctor',$doctor);
$this->layout = 'ajax';
}
ajax view
<dt><?php echo __('Visitting Starts'); ?></dt>
<dd id="visit_start"><?php echo h($doctor['Doctor']['visit_start']); ?
> </dd>
<dt><?php echo __('Visitting Ends'); ?></dt>
<dd id="visit_end"><?php echo h($doctor['Doctor']['visit_end']); ?> </dd>
Appointment has been created but the add page did not show the doctors schedule. I want to fix the appointment by knowing the time when doctor visit_start and doctor visit_end.