Abin Jacob
Abin Jacob

Reputation: 217

How to post data as json in codeigniter?

I have to post data as json object format in codeigniter. how can it make possible. My model function looks like:

public function signAction()
{
        $name=$this->input->post('name',TRUE);
        $dob=$this->input->post('dob',TRUE);
        $gender=$this->input->post('gender',TRUE);
        $country=$this->input->post('country',TRUE);
        $city=$this->input->post('city',TRUE);
        $mobile=$this->input->post('mobile',TRUE);
        $email=$this->input->post('email',TRUE);
        $password=$this->input->post('password',TRUE);

        $this->db->select("*");
        $this->db->where('email',$email);
        $this->db->or_where('mobile',$mobile);

        $query = $this->db->get('dr_signup');

        $value=$query->num_rows();
        if($value==0)
        {
            $sql = "INSERT INTO dr_signup (name,dob,gender,country,city,mobile,email,password) 
                VALUES(".$this->db->escape($name).",".$this->db->escape($dob).",".$this->db->escape($gender).",".$this->db->escape($country).",
                       ".$this->db->escape($city).",".$this->db->escape($mobile).",".$this->db->escape($email).",".$this->db->escape($password).")";
                $value1=$this->db->query($sql);
                $insert_id = $this->db->insert_id();

                $details = array(
              'status'=>'success', 
              'message'=>'registered sucessfully',
              'id' => $insert_id ,
              'name' => $name,
              'dob'=>$dob,
              'gender'=>$gender,
              'country'=>$country,
              'city'=>$city,
              'mobile'=>$mobile,
              'email'=>$email,

              );

                echo  json_encode($details);
        }
        else
        {

            $detail = array(
            'status'=>'unsuccess', 
            'message'=>'already registered',

    );

    echo  json_encode($detail); 
        }

}

I have to post the data as json format. My json objects looks like:

{"name":"jon","dob":"1jan2015","gender":"male","country":"India","city":"Mumbai","mobile":"86064 70000","email":"[email protected]","password":"1234"}

I am new to this. How can it make possible. what all modification should I have to do in code. thanking in advance.

Upvotes: 0

Views: 1676

Answers (2)

kumaravel
kumaravel

Reputation: 74

You try just like this

return json_encode($details); 

delete your code "echo  json_encode($details)"  

I think it's working

Upvotes: 0

Sandeep J Patel
Sandeep J Patel

Reputation: 950

say for example you get the json in $this->input->post('json_string',true); you can use the code like this..

$jsonstring = $this->input->post('json_string');
$json_data = json_decode($jsonstring,true); //remove true if you need object or keep it for array

The $json_data will give you the key value of the posted json string like $json_data['name'] ...

Upvotes: 1

Related Questions