Reputation: 79
I want to call a codeigniter controller function from ajax jquery function, and on success i am getting the result in array form. I need to output the array values into different text fields. how can i retrieve the array values with jquery ajax. My code,
<?php
echo form_open(base_url().'index.php/tapal'); ?>
<table class="register-table">
<tr>
<td>Tapal No.</td>
<td><input type="text" class="tapal_no" name="Tyear" id="Tyear" placeholder="<?php echo date('Y');?>" />
<input type="text" class="tapal_no" name="Tno" id="Tno" />
<input type="text" class="tapal_no" name="Tdist" id="Tdist" placeholder="7" /></td>
</tr>
<tr>
<td><label for="T_sender">Sender Code<span class="required_field">*</span></label></td>
<td><select id="T_sender" name="T_sender">
<option value=""></option>
<?php if(isset($Tsender)) : foreach($Tsender as $send) : ?>
<?php echo "<option value=".$send->add_id.">".$send->add_name.", ".$send->add_line1."</option>"; ?>
<?php endforeach; ?>
<?php else : ?>
<option value=""></option>
<?php endif; ?>
</select>
</td>
</tr>
<tr>
<td>Sender Name</td>
<td><input type="text" class="text-input" name="send_name" id="send_name" /></td>
</tr>
<tr>
<td rowspan="3">Address</td>
<td rowspan="3"><div><input type="text" class="text-input" name="add_line1" id="add_line1" placeholder="Building Name/ No." /></div>
<div><input type="text" class="text-input" name="add_line2" id="add_line2" placeholder="Street/ Lane" /> </div>
<div><input type="text" class="text-input" name="add_line3" id="add_line3" placeholder="City/ Dist" /> </div></td>
</tr>
</table>
<?php echo form_close(); ?>
And the jquery function ,
<script type="text/javascript">
$(function(){
$("#T_sender").change(function(){
var sender_code = $(this).val();
var out = $("#send_name");
var ajax_data = {
sender_code:sender_code,
action:'findaddress'
};
$.ajax({
url: "<?php echo site_url('tapal/tapalajaxqry'); ?>",
type: 'POST',
data: ajax_data,
success: function(msg) {
$("#send_name").val(msg);
}
});
});
})
</script>
And the controller function,
function tapalajaxqry()
{
if(strncmp($this->input->post('action'),'findaddress',11) == 0){
if($query = $this->tapal_model->get_Taddress($this->input->post('sender_code')))
return $query;
}
}
How can i get the array values,and output them (sender name,address line1,address line2,etc) into corresponding text fields?
Upvotes: 0
Views: 2555
Reputation: 66
Try this: In your controller
function tapalajaxqry()
{
// Your code here
// respond to jQuery
echo json_encode(array('result' => 'success'));
}
In your jQuery code, would retrieve like this:
$.ajax({
url: "<?php echo site_url('tapal/tapalajaxqry'); ?>",
type: 'POST',
data: ajax_data,
success: function(msg) {
$("#send_name").val(msg.result);
}
});
Upvotes: 0
Reputation: 72855
Your best bet would be to encode the output as JSON (json_encode($array);
) so that you can easily retrieve and decode the result with your ajax/jquery with JSON.parse(data);
.
There are countless similar posts on the subject, though since yours is unique to your code, it doesn't need to be flagged as a duplicate.
Try this for starters, respond with a comment if you need further assistance: Retrieving Data from a database in codeigniter using AJAX
Upvotes: 1