未定义索引:itemdetails。当从javascript发送json对象到php时

时间:2015-03-26 14:51:05

标签: javascript php html

输入项目的数量并单击添加到购物车按钮toShoppingCart()方法执行。此方法将获取标签和文本字段的值并插入到json对象并将其传递给Order.php文件

Onearlist.html

  <html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <form action="Order.php">
        <table  border="2">    
            <tr><td><div style="padding: 40px"><label id="id">001</label> <label id="name">SMS Audio street by 50 on-ear wired</label>  &nbsp;<label id="price">179.00$</label>&nbsp;Qty <input type="text" id="qtytxt"><input type="button" value="Add to Cart" onclick="toShoppingCart()" /></div></td></tr>
            <tr><td><div style="padding: 40px"><label id="id">002</label><label id="name">Beats Solo2 on-ear wired</label>  &nbsp;,<label id="price">199.00$</label> &nbsp; Qty<input type="text" id="qtytxt"><input type="button" value="Add to Cart" onclick="toShoppingCart()"/></div> </td></tr>
            <tr><td><div style="padding: 60px"></div></td>tr>
        </table>
        <input type="submit" value="Check Out">
    </form>

    <script type="text/javascript">
        function toShoppingCart(){
            console.log("toShoppingCart");
           var id=document.getElementById("id").innerHTML;
            var name=document.getElementById("name").innerHTML;
            var qty=document.getElementById("qtytxt").value;
            var price=parseFloat(document.getElementById("price").innerHTML);

            console.log(id);
            console.log(name);
            console.log(qty);
            console.log(price);

            var request;

           var itemdata={"id":id,"name":name,"qty":qty,"price":price};
          open("Order.php","window1");
          send({itemdetails:JSON.stringify(itemdata)});

        }

    </script>
      </body>
</html>

这里是Order.php       Order.php

  <?php

   $data=json_decode($_GET['itemdetails']);

    echo $data;


   ?>

1 个答案:

答案 0 :(得分:1)

要发送ajax请求,请执行以下操作:

var request = new XMLHttpRequest();
request.onreadystatechange = function () {
    var DONE = this.DONE || 4;
    if (this.readyState === DONE){
        alert(request.responseText); // get response form the server
    }
};
request.open('GET', 'Order.php?itemdetails='+JSON.stringify(itemdata), true);
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
request.send(null);

或者如果你使用jQuery:

$.get('Order.php?itemdetails='+JSON.stringify(itemdata), function(response) {
});