gpt4 book ai didi

javascript - 将 JSON 对象从 JavaScript 发送到 PHP 时为 “Undefined index: itemdetails”

转载 作者:行者123 更新时间:2023-12-03 10:29:25 25 4
gpt4 key购买 nike

输入商品数量并单击“添加到购物车”按钮后,将执行 toShoppingCart() 方法。此方法将获取标签和文本字段的值,并将它们插入到 JSON 对象中,并将其传递到 Order.php 文件。

将 JSON 对象从 JavaScript 发送到 PHP 时,会返回错误:

Undefined index: itemdetails
<小时/>

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:

<?php

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

echo $data;

?>

最佳答案

要发送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) {
});

关于javascript - 将 JSON 对象从 JavaScript 发送到 PHP 时为 “Undefined index: itemdetails”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29281591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com