繁体   English   中英

如何在Codeigniter中使用Ajax jQuery将页面重定向到另一个页面?

[英]How to redirecting page to another page using ajax jquery in codeigniter?

正在使用codeigniter,正在使用购物车。 使用ajax jquery选择要购物的商品后。 在这里,物品菜单和购物车表格将并排显示。

现在,我的任务是通过单击按钮将购物车项目复制到另一个表中,此操作已完成。 现在的问题是单击按钮,我需要复制,一次我需要路由到另一个页面。.在这里,我可以将购物车项目复制到另一个表,但是我不能重定向到另一个表。为此,我使用了jquery ajax

现在我需要路由到视图文件夹users/basket.php

Home.php

<?php
     defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="row">
     <div class="col-lg-12 text-center">
     <?php if(isset($_SESSION['loggedin'])){?>
         <div class="alert alert-success"><?php echo $_SESSION['loggedin'];?></div>
     <?php } ?>
     Hello, <?php echo $_SESSION['username']?>
     </div> 
</div>
<div class="row">
    <div class="col-lg-3">
        <table class="table table-condensed table-hover">
            <tr>
                <th class="text-center">Item</th>
                <th class="text-center">Quantity</th>
                <th class="text-center">Price</th>
                <th class="text-center">Add</th>
            </tr>
            <?php foreach($items as $item):?>

            <tr class="success">         
                <td><?php echo $item['product_name'];?></td>
                <td class="text-center"><input type="text" name="quantity" id="<?php echo $item['product_id'];?>" class="quantity" maxlength="2" size="2"></td>
                <td><?php echo $item['product_price'];?></td>
                <td><button type="button" name="add_cart" class="add_cart" data-productname="<?php echo $item['product_name'];?>" data-price="<?php echo $item['product_price'];?>" data-productid="<?php echo $item['product_id'];?>"><i class="fa fa-plus-circle"></i></button></td>
             </tr>
             <?php endforeach; ?>
        </table>
     </div>
     <div class="col-lg-6 col-lg-offset-1">
          <div id="cart_details" class="text-center">
          </div>
     </div>
</div>

Product_controller.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Products extends CI_Controller {

public function add(){
    $this->load->library('cart');
    $data = array(
        'id' => $_POST["product_id"],
        'name' => $_POST["product_name"],
        'qty' => $_POST["quantity"],
        'price' => $_POST["product_price"],   
    );
    $this->cart->insert($data); //return rowid
    echo $this->view();     
}
public function load(){
    echo $this->view();
}
public function remove(){
    $this->load->library('cart');
    $row_id = $_POST["row_id"];
    $data = array(
        'rowid' => $row_id,
        'qty' => 0
    );
    $this->cart->update($data); 
    echo $this->view();
}
public function clear(){
    $this->load->library('cart');
    $this->cart->destroy();
    echo $this->view();
}
public function view(){
    $this->load->library('cart');
    $output = '';
    $output.='
    <h3>Shopping cart</h3><br/>
    <div class="table-responsive">
        <div align="right">
            <button type="button" id="clear_cart" class="btn btn-danger"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
        </div>
        <br/>
        <table class="table table-bordered">
            <tr>
                <th class="text-center">Name</th>
                <th class="text-center">Quantity</th>
                <th class="text-center">Price</th>
                <th class="text-center">Total</th>
                <th class="text-center">Action</th>
            </tr>';
            $count = 0;
            $content=$this->cart->contents();
            foreach($content as $items){
                $count++;
                $output .='
            <tr>
                <td>'.$items["name"].'</td>
                <td>'.$items["qty"].'</td>
                <td>'.$items["price"].'</td>
                <td>'.$items["subtotal"].'</td>
                <td><button type="button" name="remove" class="btn btn-danger btn-xs remove_inventory" id="'.$items["rowid"].'"><i class="fa fa-times" aria-hidden="true"></i></button></td>
            </tr>';
            }
            $output .='
            <tr>
                <td colspan="4" align="right">Total</td>
                <td>'.$this->cart->total().'</td>
            </tr>
        </table>
        <button type="submit" name="basket" class="btn btn-danger btn-lg basket" ><i class="fa fa-shopping-cart" aria-hidden="true"></i></button>
    </div>';
    if($count == 0){
        $output = '<h3>Cart is Empty</h3>';
    }
    return $output;
}
public function basket(){
    if ($cart = $this->cart->contents()){
        foreach ($cart as $item){
        $order_detail = array(
            'tblItemsID' => $item['id'],
            'tblLoginID' => $this -> session -> userdata('user_id'),
            'Qty' => $item['qty'],
            'price'    => $item['price'],
            'total'    => $item['subtotal']
         );
         $this->db->insert('tblShoppingCart', $order_detail);
         }
     }  
  }              

}

注意:在product_controller.php中,我编码了将购物车项目复制到另一个表的按钮。 我在下面的jQuery ajax中编写的按钮单击事件

jQuery Ajax:

<script>
$(document).ready(function(){
$('.add_cart').click(function(){
var product_id=$(this).data("productid");
var product_name=$(this).data("productname");
var product_price=$(this).data("price");
var quantity=$('#' + product_id).val();
if(quantity != '' && quantity >0)
{
$.ajax({
url:"<?php echo base_url();?>products/add",
method:"POST",
data:{product_id:product_id,product_name:product_name,product_price:product_price ,quantity :quantity},
success:function(data)
{
alert("Product Added into cart");
$('#cart_details').html(data);
$('#' + product_id).val('');
}
});
}
else
{
alert("Please Enter Quantity");
}
});
$('#cart_details').load("<?php echo base_url();?>products/load");
 $(document).on('click','.remove_inventory',function(){
        var row_id = $(this).attr("id");
        if(confirm("Are you sure you want to delete item")){
            $.ajax({
                url:"<?php echo base_url();?>users/remove",
                method:"POST",
                data:{row_id:row_id},
                success:function(data)
                {
                alert("Product remove fromm cart");
                $('#cart_details').html(data);
                }
            });
        }else{
            return false;
        }
    });
$(document).on('click','#clear_cart',function(){

if(confirm("Are you sure you want to delete item"))
{
$.ajax({
url:"<?php echo base_url();?>products/clear",

success:function(data)
{
alert("Are you sure you want clear cart?");
$('#cart_details').html(data);

}
});
}
else{
return false;
}
});
$(document).on('click','.basket',function(){

if(confirm("Are you sure you want to delete item"))
{
$.ajax({
url:"<?php echo base_url();?>products/basket",
method:"POST",
success:function(data)
{
alert("Are you sure?");
window.location="users/basket";

}
});
}
else{
return false;
}
});

});

</script>
----------

在所需的重定向位置使用此代码:您将在此处通过ajax成功响应(日期)检索响应;
在此之前,您将在服务器端返回表示控制器的值。

$.ajax({
url:"<?php echo base_url();?>products/basket",
method:"POST",
success:function(data)
{
alert("Are you sure?");
location.href = data.url_path;

}
});

使用window.location.href

$.ajax({
  url:"<?php echo base_url();?>products/basket",
  method:"POST",
  success:function(data)
  {
     alert("Are you sure?");
     window.location.href="<?php echo base_url();?>users/basket";

  }
});

ajax成功后,只需写。

window.location.href="redirect_location_name";

暂无
暂无

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

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