apot
apot

Reputation: 39

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

Upvotes: 1

Views: 32

Answers (2)

Grant C.
Grant C.

Reputation: 356

The form values in the submitted form will be available in the $_POST variable in 'order.php'. Find the values by using the HTML 'name' attribute of the form input you want the value for. So if you want the value of the hidden "new" form element, get the value in 'order.php' by using $_POST['new'].

Upvotes: 2

demopix
demopix

Reputation: 169

well if i understood you want get a value from this first php page and pass to another php page. so just do it in the first php do

$_SESSION['mything'] = $value_that_want_pass;

on the second php page do

session_start();

echo $_SESSION['mything'];

Upvotes: 1

Related Questions