gpt4 book ai didi

javascript - 在 php 页面之间传递 javascript 值

转载 作者:行者123 更新时间:2023-11-30 11:04:13 24 4
gpt4 key购买 nike

我正在做我的学期项目,我想在下一页 (order.php) 上传递变量“值”。它是从一个下拉框中检索到的值,该下拉框基于另一个由数据库填充的下拉框。我已确认该值已存储,但我无法在下一页检索它。

我尝试了本地存储(变为 NULL)、 session 变量( undefined index “值”)并将其放入 url,但似乎没有任何效果,而且非常令人沮丧。有人可以帮我举个例子吗?谢谢

session_start();

$conn = new mysqli("localhost", "root", "", "restaurant_ordering");
mysqli_set_charset($conn,"utf8mb4");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from sector ";
$result = $conn->query($sql);



while($row = $result->fetch_assoc()){
$categories[] = array("id" => $row['id'], "val" => $row['name']);
}

$sql = "SELECT * FROM booth";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()){
$subcats[$row['sector_id']][] = array("id" => $row['id'], "val" => $row['name']);
}

$jsonCats = json_encode($categories);
$jsonSubCats = json_encode($subcats);


?>

<!doctype html>
<html>

<head>
<script type='text/javascript'>
<?php
echo "var categories = $jsonCats; \n";
echo "var subcats = $jsonSubCats; \n";
?>
function loadCategories(){
var select = document.getElementById("categoriesSelect");
select.onchange = updateSubCats;
for(var i = 0; i < categories.length; i++){
select.options[i] = new Option(categories[i].val,categories[i].id);
}
}
function updateSubCats(){
var catSelect = this;
var catid = this.value;
var subcatSelect = document.getElementById("subcatsSelect");
subcatSelect.options.length = 0;
for(var i = 0; i < subcats[catid].length; i++){
subcatSelect.options[i] = new Option(subcats[catid][i].val,subcats[catid][i].id);
}
}



function handleSelectChange(event) {

var selectElement = event.target;
var value = selectElement.value;

}



</script>
<meta charset="utf-8">
<title>New Order</title>
<link rel="stylesheet" href="server.css" />
</head>

<body onload='loadCategories()'>

<p><a href="server.html"style="color:#ffcc00">Home</a>


| <a href="../login.html"style="color:#ffcc00">Logout</a></p>
<h2>New Order</h2>
<strong>Sector:</strong>

<form name="form" method="post" action="order.php" >
<select id='categoriesSelect'>
</select>

<select id='subcatsSelect' onchange="handleSelectChange(event)">
</select>



<input type="hidden" name="new" value="1" />
<input type="hidden" name="select" id="total" value="">




</div>
</div>




<div id="button">
<p><input name="submit" type="submit" value="Submit" onclick= </p>
</div>

</form>
</body>
</html>```

最佳答案

提交表单中的表单值将在'order.php' 的$_POST 变量中可用。通过使用您想要其值的表单输入的 HTML“名称”属性来查找值。因此,如果您想要隐藏的"new"表单元素的值,请使用 $_POST['new'] 获取 'order.php' 中的值。

关于javascript - 在 php 页面之间传递 javascript 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56370110/

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