gpt4 book ai didi

javascript - 使用隐藏输入将 Javascript 变量传递给 PHP

转载 作者:行者123 更新时间:2023-11-29 20:51:50 25 4
gpt4 key购买 nike

在下面的代码中,我想做的是发送票的总值(value),然后从 Javascript 到 PHP action_page.php 是接收者,但问题是每次 Reserve 按钮是单击它会发送 0,同时当用户选择座位时,如果我使用 window.alert(b); 来显示当前选择的值,它会显示没有任何问题。只有在使用按钮将值从 Javascript 发送到 PHP 时才会出现此问题。

我哪里错了?下面是代码

    ss.php  //main page

<!DOCTYPE html>
<html lang="en">
<head>
<title>Seat(s)</title>
</head>
<body>

<?php
$b=0;


if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($_POST['submit'])) { //Seat Reserve

require 'action_page.php';

}

elseif (isset($_POST[''])) { //Cancel

require 'mypage.php';

}
}



?>

<h2>Please choose a seat to book</h2>
<form action="ss.php" method="post">
<input type="checkbox" name="vehicle" id="A1" value="100">$100<br>
<input type="checkbox" name="vehicle" id="A2" value="65"> $65<br>
<input type="checkbox" name="vehicle" id="A3" value="55"> $55<br>
<input type="checkbox" name="vehicle" id="A4" value="50"> $50<br>

<p id="demo">
Selected Seat(s)
<br>
<span id="selected-seats"></span> <!-- container for selected seats -->
<br>
Total: <span id="total-container"></span> USD

<button type="submit" class="btn btn-primary" name="submit" onClick="displayCheck()">Reserve Now</button>
<input type="hidden" name="value" value="0">
</p>
</form>






<script>
const selections = {};
const inputElems = document.getElementsByTagName("input");
const totalElem = document.getElementById("total-container");
const seatsElem = document.getElementById("selected-seats");

for (let i = 0; i < inputElems.length; i++) {
if (inputElems[i].type === "checkbox") {
inputElems[i].addEventListener("click", displayCheck);
}
}

function displayCheck(e) {
if (e.target.checked) {
selections[e.target.id] = {
id: e.target.id,
value: e.target.value
};
}
else {
delete selections[e.target.id];
}

const result = [];
let total = 0;

for (const key in selections) {
result.push(selections[key].id);
total += parseInt(selections[key].value);
}

totalElem.innerText = total;
seatsElem.innerHTML = result.join(",");
//window.alert(result); //Hold Number of Seats Selected.
//window.alert(total); //Hold Total Cost of Selected Seats.

var b = <?php echo $b; ?> + total ;
document.getElementsByName("value").value = b;


window.alert(b); //Hold Number of Seats Selected.



}

</script>





</body>
</html>

action_page.php //For receiving value from ss.php

<html>
<head>
<title>Seats Feedback</title>
</head>

<body>


<?php

echo "<br>";

$myval = $_POST['value'];

print_r($myval);

?>

最佳答案

解决方案:

...
// Move the `onClick` function all to `onsubmit` in the form tag.
<form onsubmit="displayCheck();" action="ss.php" method="post">
...
var b = <?php echo $b; ?> + total ;
document.getElementsByName("value")[0].value = b; //<-- Notice the selection of first array
window.alert(b); // Will display the total value of selected seats.

}
...

How and Why?:当您执行页面时 ss.php , 您正在设置 $b 的值作为0 , 所以直到 php 中的值被改变对于该执行,该值将保持不变。

所以当上面的页面呈现时,$b 的值回显如下。

...
<input type="hidden" name="value" value="0">
...
var b = 0 + total ;
...

现在这是 php 的工作所在。 【后台】搞定,交给javaScript (js) IE。 [前端].

现在在前端进行计算时,<input> 的值还是0 .其中变量的值 b已经升级。因此,为了传递 b 的新值使用表单,您将必须更新 <input> 的值.

因此“解决方案”..

DEMO

关于javascript - 使用隐藏输入将 Javascript 变量传递给 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51458006/

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