当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Send data between pages using $_POST and $_SESSION

问题描述:

Hey I'm trying to send the data that I have in an input saving it in a $_SESSION variable, but when I get redirected to the page I get and Undefined index: idEmployee. Here is my approach.

The code of the first page

session_start();

include('../includes/config.php');

include('../includes/db.php');

if (isset($_POST['updateEmployee'])) {

if (isset($_POST['tempId'])) {

$myVar = $_POST['tempId'];

$_SESSION['idEmployee'] = $myVar;

header("Location:nuevoCliente.php");

}

}

?>

<?php

$query = "SELECT employee.employee_id, employee.employee_name, employee.employee_cc, type_employee.t_Employee_name FROM employee, type_employee WHERE employee.t_Employee_id = type_employee.t_Employee_id";

global $db;

$result = $db->query($query);

while($row = mysqli_fetch_array($result)){

echo "<tr><td style='text-align:center'>" .

$row['employee_name'] . "</td><td style='text-align:center'>" .

$row['employee_cc'] . "</td><td style='text-align:center'> " .

$row['t_Employee_name'] . "</td>" .

"<td style='text-align:center; vertical-align:inherit'>" .

"<form style='margin-bottom: initial;' method='post' action='nuevoCliente.php'>" .

"<input type='hidden' name='tempId' value='" . $row['employee_id'] . "'/>";

echo $row['employee_id'] ;

echo "<input class='btn btn-primary btn-block' name='updateEmployee' type='submit' value='Modificar'/></form></td></tr>";

}

echo "</table></div>";

?>

Code of the Second Page

<?php

session_start();

include('../includes/config.php');

include('../includes/db.php');

echo $_SESSION['idEmployee'];

?>

What am I doing wrong?. Thanks in advance.

EDIT: The Undefined index: idEmployee is solved. Using the var_dump($_SESSION), I realizes that ['idEmployee'] didn't exist, so I just changed with a echo $_SESSION['tempId'] and the problem is already gone.

Code of the Second Page

<?php

session_start();

include('../includes/config.php');

include('../includes/db.php');

echo $_SESSION['tempId'];

?>

Now the error is that the 'tempId' variable is always the same no matters what employee I choose in the table. Any ideas?? Thanks a lot.

分享给朋友:
您可能感兴趣的文章:
随机阅读: