2016-11-17 32 views
0

.html文件重定向到點擊一個按鈕

<?php 
 
session_start(); 
 
include_once("config.php"); 
 

 

 
//current URL of the Page. cart_update.php redirects back to this URL 
 
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); 
 
?> 
 
<!-- View Cart Box Start --> 
 
<?php 
 
if(isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"])>0) 
 
{ 
 
\t echo '<div class="cart-view-table-front" id="view-cart">'; 
 
\t echo '<h3>Your Shopping Cart</h3>'; 
 
\t echo '<form method="post" action="cart_update.php">'; 
 
\t echo '<table width="100%" cellpadding="6" cellspacing="0">'; 
 
\t echo '<tbody>'; 
 

 
\t $total =0; 
 
\t $b = 0; 
 
\t foreach ($_SESSION["cart_products"] as $cart_itm) 
 
\t { 
 
\t \t $product_name = $cart_itm["product_name"]; 
 
\t \t $product_qty = $cart_itm["product_qty"]; 
 
\t \t $product_price = $cart_itm["product_price"]; 
 
\t \t $product_code = $cart_itm["product_code"];; 
 
\t \t $bg_color = ($b++%2==1) ? 'odd' : 'even'; //zebra stripe 
 
\t \t echo '<tr class="'.$bg_color.'">'; 
 
\t \t echo '<td>Qty <input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>'; 
 
\t \t echo '<td>'.$product_name.'</td>'; 
 
\t \t echo '<td><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /><strong>Remove</strong></td>'; 
 
\t \t echo '</tr>'; 
 
\t \t $subtotal = ($product_price * $product_qty); 
 
\t \t $total = ($total + $subtotal); 
 
\t } 
 
\t echo '<td colspan="4">'; 
 
\t echo '<button type="submit">Update</button><a href="view_cart.html" class="button">Checkout</a>'; 
 
\t echo '</td>'; 
 
\t echo '</tbody>'; 
 
\t echo '</table>'; 
 
\t 
 
\t $current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); 
 
\t echo '<input type="hidden" name="return_url" value="'.$current_url.'" />'; 
 
\t echo '</form>'; 
 
\t echo '</div>'; 
 

 
} 
 
?> 
 
<!-- View Cart Box End --> 
 
<!-- Products List Start --> 
 
<?php 
 
$results = $mysqli->query("SELECT product_code, product_name, product_desc, product_img, price FROM products ORDER BY id ASC"); 
 
if($results){ 
 
$products_item = '<ul class="products">'; 
 
//fetch results set as object and output HTML 
 
while($obj = $results->fetch_object()) 
 
{ 
 
$products_item .= <<<EOT 
 
\t <li class="product"> 
 
\t <form method="post" action="cart_update.php"> 
 
\t <div class="product-content"><h3><strong>{$obj->product_name}</strong></h3> 
 
\t <div class="product_img"><img src="product_images/{$obj->product_img}"class="img-responsive img-thumbnail" width="150px" height="150px"></div> 
 
\t <div class="product-desc">{$obj->product_desc}</div> 
 
\t <div class="product-info"> 
 
\t \t <div class="product-price"><strong> 
 
\t Price {$currency}{$obj->price} 
 
\t </strong> 
 
\t </div> 
 
\t <fieldset> 
 

 
\t <label> 
 
\t \t <span>Quantity</span> 
 
\t \t <input type="text" size="2" maxlength="2" name="product_qty" value="1" /> 
 
\t </label> 
 
\t </fieldset> 
 
\t <input type="hidden" name="product_code" value="{$obj->product_code}" /> 
 
\t <input type="hidden" name="type" value="add" /> 
 
\t <input type="hidden" name="return_url" value="{$current_url}" /> 
 
\t <div align="center"><button type="submit" class="add_to_cart">Add to cart</button></div> 
 
\t </div></div> 
 
\t </form> 
 
\t </li> 
 
EOT; 
 
} 
 
$products_item .= '</ul>'; 
 
echo $products_item; 
 
} 
 
?>

即時通訊開發一個電子商務網站後,PHP文件。我是新來的AJAX。我不知道如何解決這個問題。在index.html我把阿賈克斯代碼加載到html正文中的index.php。當我點擊 「添加到購物車」 「結賬」 「更新」 按鈕將它重定向到的index.php,而不是停留在的index.html

更新*

<script > 
function load_data(){ 
     $.ajax({ 
      url: "index.php", 
      dataType: 'html', 
      cache: false, 
      success: function(data){ 
      $("#mydiv").html(data); 
      } 
     }); 
} 
$(document).ready(function(){ 
load_data(); 
}); 
</script> 

index.html index.php

+0

你不必有2個不同的頁面。 .php文件也可以包含h​​tml。這可能會解決您的問題。 –

+0

但我也轉換我的網站到apk文件。所以我需要做一個HTML文件上傳到phonegap。感謝您的評論:) –

+0

我明白,也不要使用間隔0000 ...你會崩潰客戶端,如果不是服務器。 –

回答

0

jQuery的$就()函數是用來執行一個異步HTTP請求。它很早以前就加入到圖書館,自1.0版以來就已經存在。 $ .ajax()函數是前面提到的文章中討論的每個函數使用預設配置調用場景後的內容。這個函數的簽名如下所示:

$.ajax(url[, options]) 
$.ajax([options]) 

url參數是包含要與Ajax調用訪問的網址字符串,而選項是文字包含Ajax請求的配置對象。

在第一種形式中,此函數使用url參數和選項中指定的選項執行Ajax請求。在第二種形式中,URL是在options參數中指定的,或者可以省略,在這種情況下向當前頁面發出請求。

setInterval()方法以指定的時間間隔(以毫秒爲單位)調用函數或計算表達式。

在你的情況下,你的代碼應該是

$(document).ready(function(){ 
      auto_load(); 
     }); 
    setInterval(auto_load,10000); 
function auto_load(){ 
    $.ajax({ 
     url: 'path/to/file/index.php', 
     data: { 
      format: 'json' 
     }, 
     error: function() { 
      $('#info').html('<p>An error has occurred</p>'); 
     }, 
     dataType: 'jsonp', 
     success: function(data) { 
     alert(data); 
     //auto_load(); your code assignment and result goes here 
     }, 
     type: 'GET' 
    }); 
} 

,也指https://www.sitepoint.com/use-jquerys-ajax-function/

+0

謝謝..但仍然不會工作,頁面是空白:( –

+0

如果你想結果,那麼你應該評論auto_load()函數,那麼你會得到結果 –

相關問題