I'm trying to pass some values from a Javascript variable to my controller variable in CodeIgniter. The values of this Javascript variable come from the user when he selects from an option. Here's my HTML code:
<div id="etotradersformcont">
<!--some form fields here-->
<!--CHANGE APPROVER MODAL-->
<div id="changeAppTrader" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h4 id="myModalLabel">Change Approver</h4>
</div>
<div class="modal-body">
<form name="change_app_formTrader" id="change_app_formTrader">
<div id="putHere">
<div class="row-fluid" id="appContent">
<div class="span6">
<label>Approver Name</label>
<select name="app_id[]">
<option value="0">select approver</option>
<?php
foreach($user_list as $lst){
echo '<option value="'.$lst['user_id'].'">'.$lst['appname'].'</option>';
}
?>
</select>
</div>
<div class="span5">
<label>Due Date</label>
<input type="text" READONLY name="due_date[]" class="date datepicker" value="<?php echo date('m/d/Y');?>"/>
<label>Expiration Action</label>
<select name="expire_action[]">
<option value="0">select</option>
<option value="2">Reject</option>
<option value="3">Send Notification</option>
<option value="4">Do Nothing</option>
</select>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<input type="button" class="btn" onclick="add_approver()" value="Add"/>
<input type="button" class="btn" data-dismiss="modal" aria-hidden="true" value="Close"/>
<input type="button" class="btn btn-success" onclick="save_appchangeteto()" value="Save Changes"/>
</div>
</div>
<!--END CHANGE APPROVER MODAL-->
</div>
In my javascript code for save_appchangeteto(), what I have is this:
function save_appchangeteto(){
var appid = 0;var duedate='';var eaction=0;
tetoApprover = [];var actionWord = '';var appword = '';
if(validateApproverTrader()==true){
$('form[name="change_app_formTrader"]').find(':input').each(function(){
var elem = $(this).attr('name'); var e = $(this).val();var islast = false;
switch(elem){
case 'app_id[]':
appid = e;
appword = this.options[this.selectedIndex].text;
appword = this.options[this.selectedIndex].text;
break;
case 'due_date[]':
duedate = e;
break;
case 'expire_action[]':
eaction = e;
actionword = this.options[this.selectedIndex].text;
islast = true;
break;
}
if(islast){
push_approverteto(appid,appword,duedate,eaction,actionword);
}
});
populate_approverteto();
$('#changeAppTrader').modal('hide');
}
}
function push_approverteto(appid,appword,duedate,eaction,actionword){
tetoApprover.push({'appid':appid,'appword':appword,
'duedate':duedate,'eaction':eaction,'actionword':actionword});
}
function populate_approverteto(){
var htm = '';
var x = 0;
$.each(tetoApprover,function(){
x+=1;
htm += '<tr>';
htm += '<td>'+x+'</td>';
htm += '<td>'+this.appword+'</td>';
htm += '<td>'+this.duedate+'</td>';
htm += '<td>'+this.actionword+'</td>';
htm +='<td></td>';
htm +='<td></td>';
htm += '</tr>';
});
$('#approverTblTrader').html(htm);
}
Then when the user clicks the submit button, it executes the function submitTradersRequest() below:
$('#submitTradersRequest').click(function(){
$('.rc-loader').html('<img src="'+BASE_URL+'assets/img/ajax-loader.gif"/> Please wait..');
$(this).prop('disabled',true);
tetoApprover = (tetoApprover.length<1)?1:tetoApprover;
$.ajax({
url: BASE_URL+'create/process',
type: "POST",
data: {approvers:tetoApprover},
success: function(data){
var response = $.parseJSON(data);
if(response.status!='Success'){
$('.rc-loader').html('<span class="alert alert-error">'+response.msg+'</span>');
}else{
$('#etotradersformcont').html(response.msg);
}
}
}).fail(function(data) { alert('Error'); });
$(this).prop('disabled',false);
});
But in my controller, the tetoApprover was not passed at all. The code executed was the one inside the else below, which means the variable for approvers has no value at all:
$post = $this->input->post();
if($post['approvers']){
foreach($post['approvers'] as $val){
$count++;
$status = 0;
$token = sha1(rand().date('m/d/y'));
if($count==1){
$status = 1;
}
$params = array('scalar'=>array(
$reqid,
$val['appid']
)
);
$this->create_model->updatestatus($params);
}
}
else { //this is where the process currently goes
$val['appid'] = 4; //test
$params = array('scalar'=>array(
$reqid,
$val['appid']
)
);
$this->create_model->updatestatus($params);
}
I've been working on this for days, but could not find where I went wrong. Please help.
Update:
I have confirmed that my push_approverteto() function is working properly because the populate_approverteto() function is successfully updating the list of approvers on the display.
In other words, the values are successfully saved in the variable tetoApprover.
However, it fails to be passed to the controller function $post['approvers']. That is my main problem because i could not save the user-given values into my database.