Passing jquery JSON from Codeigniter controller to view
Posted
by
dede
on Stack Overflow
See other posts from Stack Overflow
or by dede
Published on 2011-01-15T20:26:53Z
Indexed on
2011/01/15
21:53 UTC
Read the original article
Hit count: 242
I've been struggling to make it work, but cannot pass the inserted data from the controler to the view in CI using JSON. The input value from the form is successfully inserted into the database, but cannot make it appear in the view.
This is my view file ajax_view.php:
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery-1.4.2.min.js"></script>
$(document).ready(function(){
$("#submit").click(function(){
var inp = $('#inp').val();
$.post("ajax/ajax_input", { 'send' : inp },
function(data){
alert(data.input_text);
}, "json");
});
});
</script>
</head>
<body>
<form id="form1" method="post" action="">
<label for="inp">Text</label>
<input type="text" name="inp" id="inp" />
<label for="submit"></label>
<input type="submit" name="submit" id="submit" value="Submit" />
And this is the ajax_input method of the ajax.php controller:
<?php
// Initializing controller .....
// .............................
//ajax method
function ajax_input(){
$var_1 = trim($this->input->post('send'));
$array = array('input_text' => $var_1);
echo json_encode($array);
$this->db->insert('ajax',$array);
}
Trying to debug it with Firebug, it gives me that data.input_text is empty.
What am I doing wrong?
EDIT: I'm using XAMPP on Win, so is it posible that json configuration is the problem?
© Stack Overflow or respective owner