结帐页面未接收到AJAX

时间:2018-11-24 21:35:55

标签: javascript php ajax session cart

我创建了一个购物车页面,该页面在提交表单时填充了$_SESSION['cart']。然后,我创建了一个按钮,该按钮重定向到结帐页面

我遇到麻烦的部分是结帐页面包含一个实际上填充了“表格页面”的表格,该表格会发回响应文本。

结帐页面将AJAX请求发送到“表页面”,该表检查$_SESSION['cart'],并使用一个函数遍历$_SESSION['cart'],并创建文本到发送回调用“结帐”页面。鉴于我是新手,任何帮助将不胜感激。谢谢!

我的错误是-

  

解析错误:语法错误,意外的''(T_ENCAPSED_AND_WHITESPACE),期望为“-”或标识符(T_STRING)或变量(T_VARIABLE)或数字(T_NUM_STRING)

和代码-cart.php

<?php

session_start();

// Find the $item_price

function findItemHeight()
{
if ($_POST['item_price'] == '10.00') {
    return 1;
} else {
    if ($_POST['item_price'] == '35.00') {
        return 3.5;
    } else {
        if ($_POST['item_price'] == '70.00') {
            return 7;
        } else {
            if ($_POST['item_price'] == '140.00') {
                return 14;
            } else {
                if ($_POST['item_price'] == '280.00') {
                    return 28;
                }
            }
        }
    }
}
}



function addToCart($itemArray)
{
    if (isset($_SESSION['cart'])) {

    array_push($_SESSION['cart'], $itemArray);
} else {
    $_SESSION['cart'] = array();
    array_push($_SESSION['cart'], $itemArray);
}
}




if (isset($_POST['add_to_cart'])) {



$item_name = $_POST['item_name'];
$item_price = $_POST['item_price'];
$item_height = findItemHeight();
$item_array = array('item_name' => $_POST['item_name'], 'item_price' => $_POST['item_price'], 'item_height' => $item_height,);

addToCart($item_array);

}





echo sizeOf($_SESSION['cart']);
print_r($_SESSION['cart']);

echo <<<_END

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<style>

.button{
    font-size:25px;
    display:inline;
    background-color:red;
    border-style:solid;
    border-color:red;   
}


</style>

<script type="text/javascript">

function checkout(){
    window.location = "checkout_page.php";


}

</script>

</head>
<body>


<form method="post" action="/cart.php">
<input type="hidden" name="item_name" value="Girl Scout Cookie">
<select name="item_price" size="1" style="font-size:22px;">
  <option value="10.00" style="margin-right:10px">Small: $10</option>
  <option value="35.00" style="margin-right:10px">1/8: $35</option>
  <option value="70.00" style="margin-right:10px">1/4: $70</option>
  <option value="140.00" style="margin-right:10px">1/2: $140</option>
  <option value="280.00" style="margin-right:10px">Full: $280</option>
</select>
<input type="submit" value="Calculate" style="font-size:22px;text- 
align:center; margin-top:10px;line-height:25px" name="add_to_cart">
</form>
_END;
            //For some reason the button doesnt change its physical 
characteristics

echo <<<_END

<button value="Checkout" onclick="checkout()" style="font-size:22px;text- 
align:center; margin-top:10px;line-height:25px;display:inline" ></button>
</body>
</html>


_END;

?>

checkout_page.php

<!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">




<script>

var http = new XMLHttpRequest();
http.onreadystatechange = function(){
    var gallery = document.getElementById('cartContainer');
    gallery.innerHTML = this.responseText;
};
http.open('POST', 'table_page.php', true);
http.send();



</script>


</head>
<body>







<div class="cart" id="cartContainer">


</div>
</div>







</body>
</html>

table_page.php

<?php
session_start();
function displayTable(){
        if(!isset($_SESSION['cart'])){

            //DISPLAY THE DEFAULT MESSAGE IF NO ITEMS IN CART

            echo" <table><th>CART TEST</th><tr><td>'Nothing Selected'</td></tr></table>";




        }
        else{

            //DISPLAY THE CONTENTS OF $_SESSION['cart']

            echo "<table><th>Name</th><th>Height</th><th>Price</th>";

            foreach ($_SESSION['cart'] as $cartItem){

                echo"<tr><td><$cartItem['item_name']</td><td>$cartItem['item_height']</td><td>$cartItem['item_price']</td></tr>";

                    }

            }
            echo "</table>";


        }


    }

displayTable();

?>

我尝试过在$ _SESSION ['cart']和单/双引号中使用转义斜杠的不同组合,但是我遇到了相同的错误。我还检查了其他问题,但它们当然不会具体回答我的问题。请提供任何反馈意见

0 个答案:

没有答案