ITWitch
ITWitch

Reputation: 1729

Could not pass array value from Javascript to Codeigniter controller

In my HTML, when a user selects from a dropdown option, the selected item is stored in a Javascript variable. I'm trying to pass this value(s) to my controller variable in CodeIgniter, but it fails.

Here's my HTML code:

<form name="change_app_formTrader" id="change_app_formTrader">
    <label>Approver Name</label>
    <select name="app_id[]">
        <option value="0">select approver</option>
        <option value="approver1">Approver One</option>
        <option value="approver2">Approver Two</option>
        <option value="approver3">Approver Three</option>
        }
        ?>
    </select>
</form>
<input type="button" onclick="save_appchangeteto()" value="Save Changes"/>

Here's my javascript code for save_appchangeteto():

var tetoApprover;
var appword;

function save_appchangeteto(){
    tetoApprover = [];
    appword = '';
    var appid   = 0;

    $('form[name="change_app_formTrader"]').find(':input').each(function(){
        appid = $(this).val();
        appword = this.options[this.selectedIndex].text;
        push_approverteto(appid,appword);
    });
    populate_approverteto();

} /*save_appchangeteto()*/

function push_approverteto(appid,appword){
    tetoApprover.push({
        'appid':appid,
        'appword':appword
    });
}

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></td>';
            htm +='<td></td>';
            htm += '</tr>';
    });
    $('#approverTblTrader').html(htm);
} /*populate_approverteto*/

Then when the user clicks the submit button, it executes the below code:

$('#submitTradersRequest').click(function(){
    $('.rc-loader').html('<img src="'+BASE_URL+'assets/img/ajax-loader.gif"/>&nbsp;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>');
            }
        }
    }).fail(function() { 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 push_approverteto() is working properly because populate_approverteto() 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 variable $post['approvers']. That is my main problem because i could not save the user-given values into my database.

Upvotes: 0

Views: 575

Answers (1)

Vitalii
Vitalii

Reputation: 161

I think that you have error in function param.

public function create($process='0')
{
     if($proces='0' or !isset($proces)) return false;
     //do something
}

And update your ajax param:

url: BASE_URL+'create/1'

Look. In this example 1 in ajax method was $process in php. If you trying to send ajax request to *BASE_URL+'create/process'* url, you have php-error, because param not isset or type of param not type of param in request.

P.S.: sorry for my English.

Upvotes: 1

Related Questions