简体   繁体   English

无法将数组值从Javascript传递到Codeigniter控制器

[英]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. 在我的HTML中,当用户从下拉选项中选择时,所选项目存储在Javascript变量中。 I'm trying to pass this value(s) to my controller variable in CodeIgniter, but it fails. 我试图将此值传递给CodeIgniter中的控制器变量,但失败。

Here's my HTML code: 这是我的HTML代码:

<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(): 这是我的save_appchangeteto()的javascript代码:

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. 但是在我的控制器中, tetoApprover没有通过tetoApprover The code executed was the one inside the else below, which means the variable for approvers has no value at all: 执行的代码是下面else中的代码,这意味着批准者的变量根本没有值:

$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. 我已经确认push_approverteto()正常运行,因为populate_approverteto()成功更新了显示屏上的批准者列表。

In other words, the values are successfully saved in the variable tetoApprover . 换句话说,这些值已成功保存在变量tetoApprover

However, it fails to be passed to the controller variable $post['approvers'] . 但是,它无法传递给控制器​​变量$post['approvers'] That is my main problem because i could not save the user-given values into my database. 那是我的主要问题,因为我无法将用户提供的值保存到数据库中。

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: 并更新您的ajax参数:

url: BASE_URL+'create/1'

Look. 看。 In this example 1 in ajax method was $process in php. 在AJAX方法本实施例1是在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. 如果您尝试将ajax请求发送到* BASE_URL +'create / process'* url,则将发生php错误,因为没有设置param或请求中的param类型。

PS: sorry for my English. PS:对不起,我的英文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM