Welcome Guest, Not a member yet? Register   Sign In
unable to send javascript variable to controller
#1

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"

The attached form image will clear what i want.

Please help
Reply
#2

(This post was last modified: 05-02-2017, 08:28 AM by xenomorph1030.)

Your Ajax call is a bit extraneous when passing zplan as you have it in the URL and as a query parameter. I suggest picking one.

My legitimate suggestion is to study up on Javascript and PHP. You are clearly mixing the two in get_plan_value_by_id(). Once you remove the Javascript from your PHP function and assuming your query works correctly, you will have to return (echo/print in this case) a JSON object (do not load any views). An easy way is to use the built in PHP function json_encode().
Reply
#3

Your problem is here:
url: "<?php echo base_url();?>super_admin/get_plan_value_by_id/+zplan"
Should be something like :
url: "<?php echo base_url();?>super_admin/get_plan_value_by_id/" + zplan
Reply
#4

[attachment=873]Hi

I have got this json array from controller

{"plan_id":"7","plan_title":"GOLD","plan_price":"999","resume_limit":"12000","day_limit":"364","job_limit":"20","user":"1","day_break":"0"}


Now i want to display the values in the forms input 

I try this but it does not display the plan_price (999) in the input field

document.getElementById("plan_price").value = data.plan_price; 

Please help

Attached Files Thumbnail(s)
   
Reply




Theme © iAndrew 2016 - Forum software by © MyBB