Hello Friends,
Am actually trying to get a Foreign key which is a Primary key in Diagnosis table

Controller:

<?php
class AppTreatment extends CI_Controller
{
   function AppTreatment()
   {
      parent::__construct();

      $this->view_data['base_url'] = base_url();

      $this->load->model('Apptreatment_model');
      $this->load->model('Patient_model');

      if(!$this->session->userdata('is_logged_in') ){
      $this->session->set_flashdata('login_error', TRUE);
      redirect(base_url() . 'index.php/homepage');
        }


   }

   function index()
   {

    $data['patient']=$this->Patient_model->getAllpatients();
     $data['AppTreatment']=$this->Apptreatment_model->findAll();




    foreach($data['AppTreatment'] as $key => $item)
    {
      $pname=$this->Patient_model->fetchpatientdata($item->P_ID,"First_Name")." ".$this->Patient_model->fetchpatientdata($item->P_ID,"Last_Name");
      $item->P_ID=$pname;

    }

    $data['head_title'] = "Member Area";
      $data['styles'] = array(
        'css/krylov.css', 
        'css/trial.css',
        'css/mws-theme.css',
        'css/button.css',
        'css/mws-style.css',
        'bootstrap/css/bootstrap.min.css',
        'css/fonts/ptsans/stylesheet.css',
        'css/fonts/icomoon/style.css',
        'css/icons/icol32.css',
        'css/fonts/icomoon/style.css',
        'jui/css/jquery.ui.all.css',
        'jui/jquery-ui.custom.css',
        'css/themer.css'
      );

      $data['scripts']= array(
          'js/AppTreatment.js'
      );

        $this->load->library('form_validation');

        $this->form_validation->set_rules('Date' , 'date', 'trim|required|xss_clean');

        if ($this->form_validation->run() == FALSE)
        {
          //hasn't been run or there are validation errors

          //$this->render->view($this->view_data, 'diagnosis_view');
          $this->render->view($data, 'AppTreatment_view');
        }

        else
        {
          // everything is good - process the form - write the data into the database


        $Date = $this->input->post('Date'); 
        $Time = $this->input->post('Time'); 
        $Diagnosis_ID = $this->input->post('diagnosis');

        $this->Apptreatment_model->register_AppTreatment($Date, $Time, $Diagnosis_ID);



         echo "Appointment for Treatment is added succesfully in the database";

      }      
      //$this->load->view('view_appointmentForm',$data);

  }

    //$this->view_data['patient'] = $data['patient'];

    //$this->register();


  function getdiagnosis()
  {

    $this->load->model('Apptreatment_model');
    $P_ID = $this->input->post('patient_selected');

    $AppTreatments = $this->Apptreatment_model->patientAppTreatment($P_ID);

    if($AppTreatments != FALSE){
      $options = array(
        0 =>  'Please Select'
      );
      foreach ($AppTreatments as $key => $value) {
        $options[$value->Diagnosis_ID] = $value->Diagnosis_Name;
      }

      //$string=$this->load->view('getappointment', $data['appointment'], true);


      echo form_dropdown('diagnosis', $options, 0);
    }
    else{
      //no diagnosis for that patient
      echo FALSE;
    }
   }

 }


The model:
<?php

class Apptreatment_model extends CI_Model {


function Apptreatment_model()
{

    parent::__construct();

}   

function register_Apptreatment($Date, $Time, $Diagnosis_ID)
{

    $query_str = "INSERT INTO treatment(Date,Time,Diagnosis_ID) VALUES (?, ?, ?)" ; 

    $this->db->query($query_str,array($Date, $Time, $Diagnosis_ID));
}

public function findAll()
    {
        $query=$this->db->get('treatment');

        return $query->result();
    }



function getdiagnosis()
{
    $query = $this->db->get_where('patient', array('id' => $P_ID), $limit, $offset);

}




function patientAppTreatment($P_ID)
{
$this->db->select('*');
        $this->db->from('appointment');
        $this->db->join('diagnosis', 'diagnosis.App_ID = appointment.Appointment_ID');
        $this->db->where('appointment.P_ID', '$P_ID');
        $q = $this->db->get();

    if($q->num_rows() > 0){
        return $q->result();
    }
    else{
        return FALSE;
    }

}



}


The ajax part:

(function($){



    function get_diagnosis() {
        var patient_selected = $('#P_ID').val();
        $.ajax({
            data: {
                patient_selected: patient_selected,
            },
            type: 'POST',
            url: 'http://localhost/Clinic/index.php/AppTreatment/getdiagnosis',
            success: function(data){
                //console.log(data);
                $('#ApptreatmentsLI').show();

                if(data != false){
                    $('#AppTreatments').html(data);
                }
                else{
                    $('#AppTreatments').html('No Diagnosis');   
                }
            }
        })
    }

    $(function(){
        $('#P_ID').change(function(){
            get_diagnosis();
        });    
    });


})(jQuery);



The view:

<?php



echo  form_open($base_url . 'index.php/AppTreatment');


$Date=array(
    'name'      =>  'Date',
    'id'        =>  'Date',
    'type'      =>   'date'

);


$Time=array(
    'name'      =>  'Time',
    'id'        =>  'Time',
    'type'      =>   'time'


);



?>


<div class="mws-panel grid_8">

    <div class="mws-panel-header">
        <span><i class="icon-magic"></i>Treatment Appointment  Form</span>
    </div>



    <div class="mws-panel-body no-padding">

            <fieldset class="wizard-step mws-form-inline">

                <ul>
<li>
        <label class="adjust">Patient</label>
        <div>
        <select name="P_ID" id="P_ID">
        <option>--Select Patient--</option>
        <?php

            foreach($patient as $item)
            {
            ?>
                <option value="<?php echo $item->Patient_ID?>"><?php echo $item->First_Name." ".$item->Last_Name?></option>
            <?php
            }
        ?>
        </select>
        </div>
        </li>
        <li id="AppTreatmentsLI" style="display:none;">
            <label class="adjust">Diagnosis(s)</label>
            <div id="AppTreatments">

            </div>
        </li>      
                    <li>
                   <div>
                <label class="adjust">Treatment Date</label>
                 <?php echo form_input($Date); ?>
                 <i class="icol32-calendar-view-week" style="vertical-align:middle;"></i>
                 </div>
                 </li> 


                  <li>
                  <div>
                   <label class="adjust">Treatment Time</label>
                   <?php echo form_input($Time); ?>
                 </div>
                 </li>





                    <li>
                        <?php echo validation_errors(); ?>
                    </li>   


                    <div>
                        <?php echo form_submit(array('name' => 'register'), 'Create');  ?>
                    </div>
                    </li>

            </ul>
        </fieldset>

    </div>
</div>
</form>

I dont know whats the problem is. When i select a patient name , am supossed to get the diagnosis ID (as display name)
But guess the ajax part is not working..
Any help would be appreciated ...
thanks...

You have made the script more complecated by adding ajax. Intially try to fetch the foriegn key using only php. Then pass that value to ajax.

Member Avatar for diafol

You've included the kitchen sink in this post. Could you not just post the relevant bits?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.