0

I am trying to create dynamic html created from jquery and save it as json and post to a database using codeigniter. Currently the data is not showing correctly in the database. its just 0. A few issues i am confused with. What should the values of the table be if its storing json data? varchar or what?

I using ajax to post from jquery to a controller in codeigniter

my code from jquery as below

 $.ajax({
    type: "POST",
    dataType: "JSON",
    //url: "<?php echo site_url('application/models/question_model/jsonAddData');?>",
    url:'jsonAddData',
    //data: cssObject,
    data: item,
    success: function(data) {
            //alert('ok');
        }
});

the controller code as below

  if($this->input->is_ajax_request()) {
        try{
            $this->load->database();
            $title = $this->input->post('cssObject');
            print_r($title);
            header('Content-Type: application/json',true);
            $query1 = "INSERT INTO testjson (data) VALUES (".$this->db->escape($title).")";
            $query = $this->db->query($query1);
            log_message('debug',$title);
        }
        catch(Exception $e)
        {
            log_message('debug',$e->getMessage());
            show_error($e->getMessage());
        }
    }       

When i use chrome developer tools the post is ok. but in the database there is nothing. Thanks in advance for the help!

1 Answer 1

1

If you want to receive the data like

$title = $this->input->post('cssObject');

then you have to use

data: {cssObject: cssObject} ,

in your ajax call.

Sign up to request clarification or add additional context in comments.

2 Comments

hmm what about the database? currently what i see from phpmyadmin is showing nothing.
what does this line prints print_r($title);?

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.