doucheng5705 2013-02-02 09:47
浏览 38
已采纳

Codeigniter将多个已检查的参数传递回视图

I new to CI, and in doubt of how to passing params, my scenario is:

1) A form with multiple checkbox, seen as online_booking.php, user would checked multiple options.

2) User click button to call jq ajax post in json, this will get the callback params from booking_process in controller.

3) the returned results will then print on the view in #result div block.

OK, above process is worked fine and the result is print like what user's has checked.

Now is my problem, I want to re-use the callback result in view, even I have the result printed on the view page, but I can't use the result for further processing, how could I make the returned results into a variable in view?

I had try few way, I try the old fashion with online_booking?selected=mutipleCheckedValue&sum=500 passing method with changed of $config['permitted_uri_chars'], but get only the last checked value instead of all checked values, which is means even I checked the checkbox with Type A+B+C, in the URL its only show value for Type C, I know this is not a good practice in CI but may help to solve my doubt if I couldn't find a proper solution.

I even try to store checked values in session, as seen on controller, $this->session->set_userdata('selected', $conf_type.":".$conf_charge);, its still only show the last checked value.

Please give an advise on how to overcome with this issue as I already stucked in this part plenty of time.

online_booking.php View:

<?php
$is_logged = $this->simplelogin->is_logged();
$active = $this->simplelogin->get_data_user('activate');
$user_email = $this->simplelogin->get_data_user('email');

if(!$is_logged) {

    echo "<p>Please login to get access</p>";

}else if($active == 0){
    echo "<p>Your account is inactive to proceed this page</p>";

}else{

?>

<p><h2>Online Booking</h2></p>

<p><div id="result_msg"></div></p>

<p>
<form id="conf_booking_form">
    <input type="checkbox" name="cType[]" value="A,200">&nbsp;Type A - USD200<br>
    <input type="checkbox" name="cType[]" value="B,150">&nbsp;Type B - USD150<br>
    <input type="checkbox" name="cType[]" value="C,100">&nbsp;Type C - USD100<br>
    <input type="checkbox" name="cType[]" value="D,50">&nbsp;Type D - USD50<br>
    <input type="hidden" name="user_email" value="<?php echo $user_email ?>" />
    <input type="button" id="btn_conf" name="" value="Book" />
    <span id="wait"><img src="<?php echo base_url();?>assets/img/loader.gif" /></span><br />
</form>
</p>
<p><div id="result" style="color:#606;"></div></p>

<?php
}
?>

JQuery ajax post:

$('#btn_conf').click(function(){
    var parameters = $('#conf_booking_form').serialize();

    //alert(parameters);
    $.ajax({
        url: baseurl + 'site/booking_process',
        type: 'POST',
        data: parameters,
        dataType: 'json',
        success: function(output_str){
            if(output_str == "false"){
                $('#result_msg').html("Select at least one type to proceed booking");

            }else{
                $('#result').html(output_str);
                //window.location.replace(baseurl + 'site/online_booking?selected='+output_str.selected+'&sum='+output_str.sum);
            }
        }
    });
});

Controller:

class Site extends CI_Controller {

public function online_booking() {
    $this->load->view('header');
    $this->load->view('online_booking');
    $this->load->view('footer');
}

public function booking_process(){
    $confTypes = $this->input->post('cType');
    $user_email = $this->input->post('user_email');
    $output_str = NULL;
    $conf_charge_total = 0;

    if(!empty($confTypes)){

        foreach($confTypes as $confType){
            $explode_value = explode(",", $confType);

            $conf_type = $explode_value[0];
            $conf_charge = $explode_value[1];

            $conf_charge_total += $conf_charge; //sum up checked values

            $output_str .= $conf_type.":".$conf_charge.",";
            /*$output_str = array(
                    'selected' => $conf_type.','.$conf_charge,
                    'email' => $user_email,
                    'sum' => $conf_charge_total,
                    'flag' => 'true'
                );*/

            //$this->session->set_userdata('selected', $conf_type.":".$conf_charge);
        }

        $output_str .= $conf_charge_total;

        //$this->session->set_userdata('sum_conf_charges', $conf_charge_total);

    }else{
        $output_str = "false";
    }

    echo json_encode($output_str);
}
}

Thanks.

  • 写回答

1条回答 默认 最新

  • doutu4335 2013-02-02 15:52
    关注

    When you store set_userdata('selected', ...) inside the loop, selected will be overwritten each time you loop. This results in the last value stored, as you observed. You can move this out of the loop, either before or after, and

    $this->session->set_userdata('selected', $confTypes);
    

    or prepare an array and store that

    foreach ($confTypes as $confType){
        ...
        $selected_conf_types[$conf_type] = $conf_charge;
    }
    
    $this->session->set_userdata('selected', $selected_conf_types);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器