繁体   English   中英

单击一个按钮后,.html文件重定向到.php文件

[英].html file redirect to .php file after clicking a button

 <?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) { echo '<div class="cart-view-table-front" id="view-cart">'; echo '<h3>Your Shopping Cart</h3>'; echo '<form method="post" action="cart_update.php">'; echo '<table width="100%" cellpadding="6" cellspacing="0">'; echo '<tbody>'; $total =0; $b = 0; foreach ($_SESSION["cart_products"] as $cart_itm) { $product_name = $cart_itm["product_name"]; $product_qty = $cart_itm["product_qty"]; $product_price = $cart_itm["product_price"]; $product_code = $cart_itm["product_code"];; $bg_color = ($b++%2==1) ? 'odd' : 'even'; //zebra stripe echo '<tr class="'.$bg_color.'">'; echo '<td>Qty <input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>'; echo '<td>'.$product_name.'</td>'; echo '<td><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /><strong>Remove</strong></td>'; echo '</tr>'; $subtotal = ($product_price * $product_qty); $total = ($total + $subtotal); } echo '<td colspan="4">'; echo '<button type="submit">Update</button><a href="view_cart.html" class="button">Checkout</a>'; echo '</td>'; echo '</tbody>'; echo '</table>'; $current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); echo '<input type="hidden" name="return_url" value="'.$current_url.'" />'; echo '</form>'; 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 <li class="product"> <form method="post" action="cart_update.php"> <div class="product-content"><h3><strong>{$obj->product_name}</strong></h3> <div class="product_img"><img src="product_images/{$obj->product_img}"class="img-responsive img-thumbnail" width="150px" height="150px"></div> <div class="product-desc">{$obj->product_desc}</div> <div class="product-info"> <div class="product-price"><strong> Price {$currency}{$obj->price} </strong> </div> <fieldset> <label> <span>Quantity</span> <input type="text" size="2" maxlength="2" name="product_qty" value="1" /> </label> </fieldset> <input type="hidden" name="product_code" value="{$obj->product_code}" /> <input type="hidden" name="type" value="add" /> <input type="hidden" name="return_url" value="{$current_url}" /> <div align="center"><button type="submit" class="add_to_cart">Add to cart</button></div> </div></div> </form> </li> EOT; } $products_item .= '</ul>'; echo $products_item; } ?> 

我正在开发一个电子商务网站。 我是AJAX的新手,我不知道如何解决这个问题。 index.html中,我将ajax代码加载到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

jQuery $ .ajax()函数用于执行异步HTTP请求。 它是很久以前添加到库中的,自1.0版开始存在。 $ .ajax()函数是前面提到的文章中讨论的每个函数使用预设配置在后台调用的功能。 该函数的签名如下所示:

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

url参数是一个字符串,其中包含您要通过Ajax调用访问的URL,而options是一个对象文字,其中包含Ajax请求的配置。

在第一种形式中,此函数使用url参数和options中指定的选项执行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/

暂无
暂无

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

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