2017-10-14 22 views
0

上午使用codeigniter,我正在購物車。使用ajax jquery選擇項目到購物車後。在這裏,項目菜單和購物車表將並排顯示。如何使用codeigniter中的ajax jquery將頁面重定向到另一個頁面?

現在,我的任務是通過點擊按鈕完成購物車項目到另一個表格。現在的問題是點擊按鈕,我需要複製,並在一個時間,我需要路由到另一個頁面..在這裏,我可以複製購物車項目到另一個表,但我不能重定向到another.for這個我用jqueryajax

現在我需要的路線這在視圖文件夾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> 
---------- 
+0

你應該經常檢查你的ajax請求是否成功/失敗,從你的控制器返回一個布爾值並檢查。例如:if(data.status){window.location.href =「your_url_here」} – Shan

回答

0

使用此代碼,您需要重定向的地方: 在這裏您將獲取通過AJAX成功響應(日期)的響應;
之前您將返回意味着控制器的服務器端的值。

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

} 
}); 
+0

location.href =「<?php echo base_url();?> users/basket」;它不工作 –

+0

嗨最初,JS加載。因此,您將在ajax調用中檢索響應,並在此代碼中傳遞值。 –

+0

現在我改變我的代碼到你的觀點,所以請檢查並讓我知道。 –

0

使用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"; 

    } 
}); 
0

後阿賈克斯成功只是寫。

window.location.href="redirect_location_name"; 
相關問題