2

i get the json data from the php to ajax how can i separate the value of the console data just like that

i have to set in my id only the agent name not the hole "name" : "Testing"

This is my console value

[{"agent_module_id":"1","agent_module_number":"101","name":"Testing","description":";;;","agent_mobile":"0123456789","email":"[email protected]","category":"","unit_price":null,"cost_price":null,"deleted":"0"}]

This is My PHP Code

 $this->load->model("member");
            $agent_value = $_POST['agent_value']; 
            $data["results"] = $this->member->get_agent_data($agent_value);
            echo json_encode($data["results"]);

This is my JavaScript Code

  function agentForm()
{
    var agent_id = document.getElementById("agent_code").value;
        if(agent_id !='')
        {
            document.getElementById("agent_operation_form").style.display ="block";

        $.ajax({
        url: '<?php echo site_url("members/get_agent_data");?>',
        data: { 'agent_value': agent_id},
        type: "post",
      /*  success: function(data){
             // document.write(data); //just do not use document.write
             var fd_values = data.split(/,/);
             document.getElementById("agent_name").value=fd_values[2]; // unique _id
            document.getElementById("agent_mobile_number").value=fd_values[1];

            console.log(data);*/
            success: function(data){
             // document.write(data); //just do not use document.write
             var fd_values = $.parseJSON(data);
             document.getElementById("agent_name").value = fd_values[0].name; // unique _id
            document.getElementById("agent_mobile_number").value = fd_values[0].agent_module_number;

            console.log(fd_values);
        }
        }
      });
        }else
        {
        document.getElementById("agent_operation_form").style.display ="none";
        }

}

Thanx in advance

1
  • You can use json parse in js Commented Jul 6, 2015 at 6:35

1 Answer 1

2

Try as (Not Tested). You can parse your JSON data using $.parseJSON

success: function(data){
             // document.write(data); //just do not use document.write
             var fd_values = $.parseJSON(data);
             document.getElementById("agent_name").value = fd_values[0].name; // unique _id
            document.getElementById("agent_mobile_number").value = fd_values[0].agent_module_number;

            console.log(fd_values);
        }
Sign up to request clarification or add additional context in comments.

4 Comments

@Uchila please check the below the edits code... i have error in again
Remove one brace after console.log(fd_values);
You've added extra brace after your console.log(fd_values); } <------- remove this one and it'll work
very very thanx a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.