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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?