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

时间:2018-07-21 16:21:57

标签: javascript php jquery

在下面的代码中,我要执行的操作是将票证的总金额发送,然后从Javascript发送到PHP,action_page.php是接收者,但问题是每次单击“预定”按钮时它都会发送{{ 1}},与此同时,当用户选择座位时,如果我使用0显示当前选择的值,它将显示没有问题。仅当使用按钮将值从Javascript发送到PHP时,才会出现问题。

我在哪里错了?下面是代码

window.alert(b);

    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>

1 个答案:

答案 0 :(得分:2)

解决方案:

...
// 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.

}
...

如何以及为什么?:执行页面ss.php时,您将$b的值设置为0,因此直到该值是在php内执行更改,该值将保持不变。

因此,在呈现以上页面时,$b的值将回显如下。

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

现在这是php即工作的地方。 [后端]完成并移交给javaScript (js),即。 [前端]。

现在进行前端计算时,<input>的值仍为0。其中变量b的值已更新。因此,为了通过表单传递新的b值,您将不得不更新<input>的值。

因此,“解决方案”。

DEMO