1

'i have JavaScript associative array name 'options'

<script>
options[dynamickey] = Number; 
</script>

and i just want to send this array to codeigniter model using jQuery post

<script>
$.('link',options);
</script>

But the problem is I don't know how to extract each of the keys and values of this array (options). My JavaScript array with data looks like this

 <script>
  options { 
     id => 135,
     'Chestnut' => 11,
     'Cinamon' => 1
   }
</script>

in codeigniter (PHP) model i just wanna extract this array like this

<?php
 $id = $this->input->post('id');
//below variable names and data should be dynamic from that javascript array
$chesnut= $this->input->post('dynamic value');
?>

Please help me to solve this.

3
  • how can you post to your model!! You need a controller method to handle this Commented May 14, 2012 at 12:52
  • Yes i have controller but in my post i just ignore that step Commented May 14, 2012 at 12:55
  • Be aware, JavaScript doesn't have associative arrays. You have a JavaScript object with properties. This will become important if you ever try to loop over the items or json encode the 'array' as depending on how you create it, it might fail or have extra mystery values. Commented May 14, 2012 at 13:03

2 Answers 2

2

in your jQuery, you do a post:

var options = { 
    'id' : 135,
    'Chestnut' : 11,
    'Cinamon' : 1
}

$.post('example.com/index.php/firstsegment/secondsegment',options,function(data){...});

In a CodeIgniter Controller that receives this post:

public firstsegment extends CI_Controller {

    public function secondsegment(){
        $data = $this->input->post();

        if($data){
            /*
            $data will contain this:
            $data = array(
                'id' => '135',
                'Chestnit' => '11',
                'Cinamon' => '1'
            );
            */
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Kinda rare request to be frank. You want something that's probably rendered by your system, to be sent to your system?

Nonetheless. Add a { data : options } along with existing common values in jQuery ajax.

Example:

var options = { 
    'id' : 135,
    'Chestnut' : 11,
    'Cinamon' : 1
}

$.ajax({
  type: "POST",
  url: "some.php",
  data: options
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

Comments

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.