I’m new on php and I’m trying to get POST value from html and pass into two PHP files, where
- First one execute query;
- Second elaborate the query;
So the code html:
<form action="ExecSelect.php" method="post"> <div class="row form-group"> <div class="col-md-6">
ExecSelect.php:
<?php include 'connection.php'; include 'select_querys.php'; // Initialize the session session_start(); $SS_IdProduct = $_POST['IdProduct']; $_SESSION['$SS_IdProduct'] = $SS_ProdSelect; // switch($_POST['IdProduct']){ switch($_POST['IdProduct']){ case 'Conecta': Echo "Something 1" break; case 'Radar': Echo "Something 2" break; default: echo 'ERRO! page1'; } ?>
And select_querys.php is:
<?php include 'connection.php'; //On page 2 $SS_ProdSelect = $_SESSION['$SS_IdProduct']; switch($_GET['$SS_ProdSelect']){ case 'Conecta': //get results from database $result = mysqli_query($conn,"SELECT 1;"); $all_property = array(); //declare an array for saving property case 'Radar': //get results from database $result = mysqli_query($conn,"SELECT 2;"); $all_property = array(); //declare an array for saving property break; default: echo 'ERRO! pagina selct PHP!'; } ?>
What I’m doing wrong?
Advertisement
Answer
You’re including this file at the top:
include 'select_querys.php';
So it’s executing before any of this:
$SS_IdProduct = $_POST['IdProduct']; $_SESSION['$SS_IdProduct'] = $SS_ProdSelect;
Thus none of that is defined when it’s executing. (The session hasn’t even been started yet at that time.)
Since the value is expected to be in the POST array, then in your select_querys.php
you can simply read the value from $_POST['IdProduct']
just like you do in ExecSelect.php
:
$SS_IdProduct = $_POST['IdProduct'];
So you don’t even really need the use of session here, unless you plan on using that session value in future requests.
Just note that if you define the same variable (in this case $SS_IdProduct
) in both files then when executing those files in the same context (as you do here) the second declaration of that variable will over-write any changes made to it previously. That’s not happening here, but something to be aware of as you continue to update your code.
(It’s also worth noting at this time that you’re not even using $SS_ProdSelect
in select_querys.php
, but I’ll assume that you plan to at some point.)