douque9815 2018-04-05 10:22
浏览 226

将存储在HTML会话存储中的数据显示到页面中

I am trying to create a checkout page to send a confirmation to the customer of the products they want to buy. However, I am having trouble to display the data stored in the session storage into my checkout page. Whenever I click on the checkout button, the page only displays the title, and it doesn't display the data. Can somebody help me figure out this problem. I would really appreciate it.

HTML

<div id="show_checkout">
    <h1 id="basket_header">- My Basket -</h1>
    <!-- The items the user wants to buy, will be displayed on this table -->
    <table id="basket_list"></table>
    <!-- Displays the checkout and empty buttons -->
    <div id="basketDiv"></div>
</div>

JavaScript

window.onload = loadBasket;

//Displays basket in page.
function loadBasket() {
    //Get basket from local storage or create one if it does not exist
    var basketArray;
    if (sessionStorage.basket === undefined || sessionStorage.basket === "") {
        basketArray = [];
    } else {
        basketArray = JSON.parse(sessionStorage.basket);
    }
    var tableBody;
    var tableHeader = "<tr><th>Product Image</th><th>Product Name</th><th>Price</th></tr>
";
    //Build string with basket HTML
    var htmlStr = "<p class='basket_items'>Number of items in basket: " + "<span style='color:red'>" + basketArray.length + "</span>" + "</p>";
    var prodIDs = [];

    for (var i = 0; i < basketArray.length; ++i) {
        tableBody += "<tr><td>" + "<img class='basket_img' src='" + basketArray[i].image + "'>" + "</td><td>" + basketArray[i].name + "</td><td>£" + basketArray[i].price + "</td></tr>";
        prodIDs.push({
            id: basketArray[i].id,
            count: 1
        }); //Add to product array
    }
    //Add hidden field to form that contains stringified version of product ids.
    htmlStr += "<input type='hidden' name='prodIDs' value='" + JSON.stringify(prodIDs) + "'>";

    //Add checkout and empty basket buttons
    htmlStr += "<input class='checkout_button' onclick='checkoutBasket()' type='submit' value='Checkout'>";
    htmlStr += "<button class='empty_basket' onclick='emptyBasket()'>Empty Basket</button>";

    //Display number of products in basket
    document.getElementById("basketDiv").innerHTML = htmlStr;

    document.getElementById("basket_list").innerHTML = tableHeader + tableBody;
}

//Deletes all products from basket
function emptyBasket() {
    sessionStorage.clear();
    loadBasket();
}

function checkoutBasket() {
    // Create request object 
    var request = new XMLHttpRequest();
    // Create event handler that specifies what should happen when server responds
    request.onload = function() {
        // Check HTTP status code
        if (request.status == 200) {
            var responseData = request.responseText;
            document.getElementById("show_checkout").innerHTML = responseData;
    } else
        alert("Error communicating with server: " + request.status);
  }
  // Set up request with HTTP method and URL 
  request.open("POST", "php/checkout.php");
  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  //Send request
  request.send();
}

PHP

<?php
//Extract the product IDs that were sent to the server
$prodIDs = filter_input(INPUT_POST, 'prodIDs', FILTER_SANITIZE_STRING);

//Convert JSON string to PHP array 
$productArray = json_decode($prodIDs, true);

//Output the IDs of the products that the customer has ordered
echo '<h1 id="basket_header">- My Order -</h1>';
for($i=0; $i<count($productArray); $i++) {
    echo '<p>Product ID: ' . $productArray[$i]['id'] . '. Count: ' . 
$productArray[$i]['count'] . '</p>';
}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答