简体   繁体   English

在php页面之间传递javascript值

[英]passing javascript value between php pages

I am working on my semester project and I want to pass the variable "value" on the next page (order.php). 我正在研究我的学期项目,我想在下一页(order.php)传递变量“value”。 It is a value retrieved from a dropdown box that is based on another dropdown box populated by database. 它是从下拉框中检索的值,该下拉框基于由数据库填充的另一个下拉框。 I have confirmed that the value is stored but I can't retrieve it on the next page. 我已确认该值已存储但我无法在下一页检索它。

I tried localstorage (turns NULL), session variables(undefined index "value") and putting it in the url but nothing seems to work and is very frustrating. 我尝试了localstorage(转为NULL),会话变量(未定义索引“值”)并将其放入网址但似乎没有任何工作,并且非常令人沮丧。 Can someone please help me with an example? 有人可以帮我举个例子吗? Thank you 谢谢

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>```

The form values in the submitted form will be available in the $_POST variable in 'order.php'. 提交表单中的表单值将在'order.php'中的$_POST变量中提供。 Find the values by using the HTML 'name' attribute of the form input you want the value for. 通过使用您想要值的表单输入的HTML“name”属性来查找值。 So if you want the value of the hidden "new" form element, get the value in 'order.php' by using $_POST['new'] . 因此,如果您想要隐藏的“new”表单元素的值,请使用$_POST['new']获取'order.php'中的值。

well if i understood you want get a value from this first php page and pass to another php page. 好吧,如果我明白你想从这第一个PHP页面获取一个值,并传递到另一个PHP页面。 so just do it in the first php do 所以只需在第一个php中做到这一点

$_SESSION['mything'] = $value_that_want_pass; $ _SESSION ['mything'] = $ value_that_want_pass;

on the second php page do 在第二个php页面上

session_start(); 在session_start();

echo $_SESSION['mything']; echo $ _SESSION ['mything'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM