PHP - 如何在表单提交后继续使用 SESSION 变量



我在使用 SESSION 变量时遇到问题。我计划每次用户在通用脚本上点击"提交"时将数据添加到 SQL .php。我也指望第一个 pag 索引的登录数据.html这是我遇到问题的地方,每当我点击"提交"时,它都会清理变量。我检查了几个 论坛寻找答案,但我仍然没有答案。有什么想法吗?

索引.html

<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Creacion de receta</title>
<link rel="stylesheet" type="text/css" href="default.css">
</head>
<body>
<br>
<div style="text-align: center;"><img style="width: 125px; height: 125px;" alt="Rpi" src="large_logo_white.png"><br></div>
<br>
<table style="text-align: left; margin-left: auto; margin-right: auto;">
<form method="post" action="generar.php">
<tbody>
<tr>
<td>Codigo del Medico</td>
<td><input name="v_codigo"><br>
</td>
</tr>
<tr>
<td>Cedula del paciente</td>
<td><input name="v_cedula"><br>
</td>
</tr>
<tr>
<td> Enviar</td>
<td align="right"><input name="submit" value="Enviar" type="submit"></td>
</tr>
</tbody>
</form>
</table>
<br>
</body>
</html>

通用.php

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Creacion de receta</title>
<link rel="stylesheet" type="text/css" href="default.css">
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<tbody>
<tr>
<td>Medicamento</td>
<td>
<select name="v_meds"><option>Seleccionar</option><option>Rojo</option><option>Verde</option><option>Azul</option><option>Amarillo</option><option>Blanco</option><option>Negro</option></select>
<br>
</td>
</tr>
<tr>
<td>Via</td>
<td>
<select name="v_via"><option>Seleccionar</option><option>Oral</option><option>Rectal</option><option>Nasal</option><option>Otica</option><option>Oftalmica</option><option>Vaginal</option><option>Topica</option></select>
<br>
</td>
</tr>
<tr>
<td>Frecuencia</td>
<td>
<select name="v_frec"><option>Seleccionar</option><option>6</option><option>8</option><option>12</option><option>24</option></select>
<br>
</td>
</tr>
<tr>
<td>Duracion</td>
<td>
<select name="v_dur"><option>Seleccionar</option><option>5</option><option>7</option><option>15</option><option>30</option><option>PRN10</option><option>PRN20</option><option>PRN30</option></select>
<br>
</td>
</tr>
<tr>
<td> Enviar</td>
<td align="right"><input name="submit" value="Enviar" type="submit"></td>
</tr>
<tr>
<td style="text-align: center;" colspan="2" rowspan="1"><a href="view.php">Ver historial de recetas</a></td>
</tr>
</tbody>
</form>
<?php
error_reporting(E_ALL);
session_start();
if (isset($_REQUEST['v_codigo']) || isset($_REQUEST['v_cedula'])) { 
$codigo = $_REQUEST['v_codigo'];
$cedula = $_REQUEST['v_cedula'];
}
$_SESSION['sesion_codigo'] = $codigo;
$_SESSION['sesion_cedula'] = $cedula;
//$codigo1 = $_SESSION['sesion_codigo'];
//$cedula1 = $_SESSION['sesion_cedula'];
$tabla = 'p'.$cedula1;

print 'Codigo: ' . $codigo . '<br/>';
print 'Cedula: ' . $cedula . '<br/>';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name1 = $_POST['v_meds']; 
	$name2 = $_POST['v_via'];
	$name3 = $_POST['v_frec'];
	$name4 = $_POST['v_dur'];
if ($name1 == 'Seleccionar' || $name2 == 'Seleccionar' || $name3 == 'Seleccionar' || $name4 == 'Seleccionar') {
echo "Receta contiene errores, favor llenar correctamente el formulario.<br/>";
} else {
echo $name1 . $name2 . $name3 . $name4;
}
}
?>
</body>
</html>

编辑 1: 我更改session_start((;到 HTML 文件的开头。但结果是一样的。当我第一次选择变量$codigo并$cedula它们是正确的设置时,当我单击提交按钮时,它们就会消失。这个想法是,单击提交按钮后,我保留 losgin 数据以连续发送服务器请求。

在每个步骤的图像下方。再次感谢所有花时间回答的人。

登录数据: 科迪戈瓦 塞杜拉瓦尔

首次加载页面时 点击提交按钮后

编辑2:我还使用@RamRaider代码建议仅在填充变量时才填充变量。

你应该在顶部开始会话

<?php
error_reporting(E_ALL);
session_start();
?>
<!DOCTYPE html>
<html>
<head>

因为在您的代码中,它会显示错误,例如标头已发送 当我们在php中启动会话时,不应该在之前输出到页面

session_start();

所以你的代码应该是这样的:-

<?php
session_start();
error_reporting(E_ALL);
?>
<!DOCTYPE html>
<html>
<head>

我认为,主要问题是SELECT元素都没有OPTION值。对session_start()的调用也应该在任何 html 输出之前 - 因此通常就在脚本的最顶部。此外,调用 tha 设置会话变量可以按原样设置空白值 - 在设置之前确保值存在的测试将是一个好主意。

<?php
session_start();
error_reporting(E_ALL);
/* utility function to debug session and request data */
function pre($data,$title){
echo "<h1>".strtoupper($title)."</h1><pre>".print_r($data,true)."</pre>";
}
?>

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Creacion de receta</title>
<link rel="stylesheet" type="text/css" href="default.css">
</head>
<body>
<?php
if( isset( $_SESSION['sesion_codigo'], $_SESSION['sesion_cedula'] ) ){
echo '<h1>Debug session variables</h1>';
pre($_SESSION,'session');
pre($_POST,'post');
}
?>
<form method="post">
<table>
<tbody>
<tr>
<td>Medicamento</td>
<td>
<select name="v_meds">
<option value=''>Seleccionar
<option value='Rojo'>Rojo
<option value='Verde'>Verde
<option value='Azul'>Azul
<option value='Amarillo'>Amarillo
<option value='Blanco'>Blanco
<option value='Negro'>Negro
</select>
<br>
</td>
</tr>
<tr>
<td>Via</td>
<td>
<select name="v_via">
<option value=''>Seleccionar
<option value='Oral'>Oral
<option value='Rectal'>Rectal
<option value='Nasal'>Nasal
<option value='Otica'>Otica
<option value='Oftalmica'>Oftalmica
<option value='Vaginal'>Vaginal
<option value='Topica'>Topica
</select>
<br>
</td>
</tr>
<tr>
<td>Frecuencia</td>
<td>
<select name="v_frec">
<option value=''>Seleccionar
<option value='6'>6
<option value='8'>8
<option value='12'>12
<option value='24'>24
</select>
<br>
</td>
</tr>
<tr>
<td>Duracion</td>
<td>
<select name="v_dur">
<option value=''>Seleccionar
<option value='5'>5
<option value='7'>7
<option value='15'>15
<option value='30'>30
<option value='PRN10'>PRN10
<option value='PRN20'>PRN20
<option value='PRN30'>PRN30
</select>
<br>
</td>
</tr>
<tr>
<td>Enviar</td>
<td align="right"><input name="submit" value="Enviar" type="submit"></td>
</tr>
<tr>
<td style="text-align: center;" colspan="2" rowspan="1">
<a href="view.php">Ver historial de recetas</a>
</td>
</tr>
</tbody>
</table>
</form>
<?php
$codigo = !empty( $_REQUEST['v_codigo'] ) ? $_REQUEST['v_codigo'] : false;
$cedula = !empty( $_REQUEST['v_cedula'] ) ? $_REQUEST['v_cedula'] : false;
if( $codigo & $cedula ){
$_SESSION['sesion_codigo'] = $codigo;
$_SESSION['sesion_cedula'] = $cedula;
$tabla = 'p'.$cedula1;
printf( "Codigo: %s<br />Cedula: %s<br />", $codigo, $cedula );
}

if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {

$name1 = !empty( $_POST['v_meds'] ) ? $_POST['v_meds'] : false; 
$name2 = !empty( $_POST['v_via'] ) ? $_POST['v_via'] : false;
$name3 = !empty( $_POST['v_frec'] ) ? $_POST['v_frec'] : false;
$name4 = !empty( $_POST['v_dur'] ) ? $_POST['v_dur'] : false;

if( $name1 && $name2 && $name3 && $name4 ){
$message=$name1 . $name2 . $name3 . $name4;
} else {
$message="Receta contiene errores, favor llenar correctamente el formulario.<br/>";
}
echo $message;
}
?>
</body>
</html>

试试这个

<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
...
</html>

最新更新