These are the options available in t_plan table
        id    plan_name
        --    ---------
        4  -  free
        5  -  Gold
        6  -  Silver
        7  -  Platinum
        etc

        My object is to as user click on an option in the form it will populate the
        following input boxes next to select box from the data of t_plan table.

        So i store the id of plan in a hidden inputbox 
        and send that value to controller so that it can
        fetch the corrosponing data from the t_plan table and 
        populate the following.

        PLAN-PRICE
        RESUME-LIMIT
        DAY-LIMIT
        JOB-LIMIT
        NO-OF-USER-ALLOW

        Here is my code(in view)
        ------------------------
        <script>
$(document).ready(function(){
//PLAN GOLD, BRONZE, SILVER etc 
$('#plan2_list').change(function(){
   $('#plan2_id').val($("#plan2_list option:selected").val());  
   var zplan = $('#plan2_id').val();
            $.ajax({
                type: "POST",
                url: "<?php echo base_url();?>super_admin/get_plan_value_by_id/+zplan",
                data: {"zplan": zplan},
                       success: function(data) {
                       //alert("SUCCESS");
                       //print_r($result);
                       alert(data.zplan);
                      },
                       error: function(e) {
                       alert("ERROR");
                      }
            });
});     
});
</script>

zplan display the correct data which i am sending to my controller:

My Controller
=============

    public function get_plan_value_by_id($zplan){
        $data=array();
        alert(data.email);   = $this->sa_model->select_plan_by_id(zplan);
        echo  alert(data.email);
        $data['admin_maincontent'] = $this->load->view('admin/add_employer_form',$data,TRUE);
        $this->load->view('admin/admin_master',$data);
    }

My Modal
=============

public function select_plan_by_id($zplan){
    $this->db->select('*');
    $this->db->from('t_plan');
    $this->db->where('plan_id',$zplan);
    $query_result=$this->db->get();
    $result = $query_result->row();
    //echo "<pre>";
    //print_r($result);
    //exit();
    return $result;
}       

But it say "undefined"
or give "error"

I am new in ajax implementaion in codeigniter

Please help

Dani AI

Generated

Brief diagnosis: ’s client code is sending the plan id incorrectly in the URL (the literal "+zplan" is being appended) and the controller is rendering views / using client-side constructs instead of returning plain JSON. For an AJAX endpoint the controller should return JSON (and not load the full page view), and the client should tell jQuery to expect JSON so the response is parsed automatically.

Corrected approach (send an id, return JSON):

// send the selected id as POST, expect JSON back
$.ajax({
  type: 'POST',
  url: '<?php echo base_url("super_admin/get_plan_value_by_id"); ?>',
  dataType: 'json',
  data: { zplan: zplan }
})
.done(function(resp){
  if (resp) {
    $('#plan_price').val(resp.plan_price || '');
    $('#resume_limit').val(resp.resume_limit || '');
    // populate other inputs similarly
  } else {
    console.warn('No plan data returned');
  }
})
.fail(function(xhr, status, err){
  console.error('AJAX error', status, err, xhr.responseText);
});
public function get_plan_value_by_id()
{
    $zplan = $this->input->post('zplan');
    $plan  = $this->sa_model->select_plan_by_id($zplan); // return array or object
    header('Content-Type: application/json');
    echo json_encode($plan);
    exit;
}

Model tip: return a simple array or object (use row_array() or row()), not a view. Debugging tips: use the browser Network tab to inspect the request/response, check response content-type and body, and watch the console for parse errors; return a clear JSON error when a plan is missing. Also useful: ’s preview tip for cleaner posts.

Just a tip. Try the PREVIEW in the forum editor to avoid messes like your post here.

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.