简体   繁体   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; } ?> 

Im developing a ecommerce website. 我正在开发一个电子商务网站。 I'm new to AJAX .I don't know how to solve this problem. 我是AJAX的新手,我不知道如何解决这个问题。 In index.html i put ajax code to load the index.php inside the html body. index.html中,我将ajax代码加载到html主体中以加载index.php And when i click " add to cart " " checkout " " update " button its redirecting to index.php instead staying on index.html . 当我单击“ 添加到购物车结帐更新 ”按钮时,它重定向到index.php而不是停留在index.html上

UPDATED* 更新*

<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 index.html index.php

The jQuery $.ajax() function is used to perform an asynchronous HTTP request. jQuery $ .ajax()函数用于执行异步HTTP请求。 It was added to the library a long time ago, existing since version 1.0. 它是很久以前添加到库中的,自1.0版开始存在。 The $.ajax() function is what every function discussed in the previously mentioned article calls behind the scene using a preset configuration. $ .ajax()函数是前面提到的文章中讨论的每个函数使用预设配置在后台调用的功能。 The signatures of this function are shown below: 该函数的签名如下所示:

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

The url parameter is a string containing the URL you want to reach with the Ajax call, while options is an object literal containing the configuration for the Ajax request. url参数是一个字符串,其中包含您要通过Ajax调用访问的URL,而options是一个对象文字,其中包含Ajax请求的配置。

In its first form, this function performs an Ajax request using the url parameter and the options specified in options. 在第一种形式中,此函数使用url参数和options中指定的选项执行Ajax请求。 In the second form, the URL is specified in the options parameter, or can be omitted in which case the request is made to the current page. 在第二种形式中,URL是在options参数中指定的,或者在这种情况下可以向当前页面发出请求,因此可以将其省略。

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds). setInterval()方法以指定的时间间隔(以毫秒为单位)调用函数或计算表达式。

In Your case your code should be 在您的情况下,您的代码应为

    $(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'
    });
}

and also refer https://www.sitepoint.com/use-jquerys-ajax-function/ 并参考https://www.sitepoint.com/use-jquerys-ajax-function/

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

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