Frage

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.

War es hilfreich?

Lösung

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);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top