如何在jquery mobile上使用HMVC方法在CI控制器中重定向页面

时间:2017-11-19 02:34:49

标签: php codeigniter jquery-mobile

我目前正在使用CI HMVC方法构建电子商务网站。我完成了它的计算机设计和所有后端。它工作得很好。但我的dessign对移动设备来说很糟糕,所以我决定编写jquery移动代码。我几乎完成了jquery mobile中的代码,但是我遇到了无法解决的问题。

我有用于将商品添加到购物车中的表单。为此,我已添加到购物车功能,其中有视图文件add_to_Cart.php whis具有实际形式。提交表单后转到store_basket / add_to_basket函数。在这里,我编写代码来处理store_basket表的提交信息。然后它将页面重定向到另一个控制器购物车,在那里我有购物车中物品清单的查看文件。

当我提交表格时,它存储在store_basketm表中的项目。然后它显示了购物车控制器的视图,但网址是localhost / shop / store_basket / add_to_basket,应该是localhost / shop / cart。但是尽管url localhost / shop / store_basket / add_to_basket它显示了localhost / shop / cart的内容

<?php $form_location = base_url().'store_basket/add_to_basket'; ?>
    <div id="cart" style="background-color: #ddd; border-spacing: 7px; padding: 7px; color: black;">
        <form ajax-date="false" data-url="true" action="<?= $form_location ?>" method="POST">
            <table class="table">
                <tr>
                    <td>ID: </td>
                    <td><?php echo $item_id ?></td>
                </tr>
                <input type="hidden" name="item_id" value="<?php echo $item_id ?>">
                <input type="hidden" name="item_title" value="<?php echo $item_title ?>">
                <?php if($num_colour > 0): ?>
                <tr>
                    <td style="padding-top: 20px;">Colour: </td>
                    <td style="margin-left: 50px;">
                        <?php  
                            $status = '';
                            $additional_dd_code = 'name="select-choice-mini" id="select-choice-mini" data-mini="true" data-inline="true"';
                            echo form_dropdown('item_colour', $colour_options, $submitted_colour, $additional_dd_code);
                          ?>
                    </td>
                </tr>
                <?php endif; ?>
                <?php if($num_size > 0): ?>
                <tr>
                    <td style="padding-top: 20px;">Size: </td>
                    <td style="margin-left: 50px;">
                        <?php  
                            $status = '';
                            $additional_dd_code = 'name="select-choice-mini" id="select-choice-mini" data-mini="true" data-inline="true" style="background-color: white;"';
                            echo form_dropdown('item_size', $size_options, $submitted_size, $additional_dd_code);
                          ?>      
                    </td>
                </tr>
                <?php endif; ?>
                <tr>
                    <td>Price: </td>
                    <td><?= $item_price ?></td>
                </tr>
                <input type="hidden" name="price" value="<?= $item_price ?>">
                <tr>
                    <td style="padding-top: 20px;">Qty: </td>
                    <td>
                        <input type="number" name="item_qty" value="1" min="1" required>
                    </td>
                </tr>
            </table>
            <!-- <a rel="external" href=""><input name="submit" value="Submit" class="btn"></a> -->
            <button type="submit" name="submit" value="Submit" id="cart-btn" class="ui-shadow ui-btn ui-corner-all" style="margin-top: 20px; width: 75%; margin-left: 30px;"><span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></span> Add To Basket</button>
        <?php echo form_close() ?>

store_basket / add_to_basket

function add_to_basket() {
// var_dump($_POST); exit;
$this->load->module('site_settings');
$this->load->library('session');
$this->load->module('site_security');
$is_mobile = $this->site_settings->is_mobile_device();

$submit = $this->input->post('submit', TRUE);

if($submit == "Submit"){
    //library for form validation
    $this->load->library('form_validation');
    //validation rules
    $this->form_validation->set_rules('item_colour', 'Item Color', 'numeric');
    $this->form_validation->set_rules('item_size', 'Item Size', 'numeric');
    $this->form_validation->set_rules('item_qty', 'quantity', 'required|numeric');
    $this->form_validation->set_rules('item_id', 'Item_id', 'required|numeric');

    if($this->form_validation->run($this)) {
        // echo "Success"; exit;

        $data = $this->_fetch_the_data();

        $this->_insert($data);


        redirect('cart');



    } else {
        // echo "Fail"; exit;

        $refer_url = $_SERVER['HTTP_REFERER'];
        $error_msg = validation_errors("<p style='color: red;'>","</p>");

        $this->session->set_flashdata('item', $error_msg);
        redirect('cart');
    }
}

}

车/ index.php的

function index() {
// echo "here"; exit;
$this->load->module('site_settings');
$is_mobile = $this->site_settings->is_mobile_device();
$data['flash'] = $this->session->flashdata('item');
$data['view_files'] = "cart";

if($is_mobile==TRUE) {
    $data['view_files'] .= "_jqm";
    $template = 'public_jqm';
} else {
    $template = 'public_boostrap';
}

$token = $this->uri->segment(3);
if($token!='') {
    $session_id = $this->_check_and_get_session_id($token);
} else {
    $session_id = $this->session->session_id;
}

$this->load->module('site_security');
$shopper_id = $this->site_security->_get_user_id();

if(!is_numeric($shopper_id)) {
    $shopper_id = 0;
}

$table = 'store_basket';
$data['query'] = $this->_fetch_cart_contents($session_id, $shopper_id, $table);

//count the items in basket or cart
$data['num_rows'] = $data['query']->num_rows();
$data['showing_statement'] = $this->_get_showing_statement($data['num_rows']);

$this->load->module('templates');
$this->templates->$template($data);

}

0 个答案:

没有答案