0

我在删除员工时收到错误“Undefined index: Msg in Employee.php”。我试图显示来自employeedb.php 的会话值。这是我的employee.php。这里我试图在 div“divGuestRegisterContainer”中显示会话消息

<?php
require_once("includes.php");
<div id="divGuestRegisterContainer" >
<b>
<?php   
if($_SESSION["Msg"]!="")
{
?>
<ul>
<li>
<b><?php 
echo $_SESSION["Msg"];
?>
</b>
</li>
</ul>
<?php
$_SESSION["Msg"]="";
}
?>
</b>
<ul>
<li>
<span style="width:120px;">Employee Name :</span>
<input type="text" name="txt_empname" id="txt_empname" class="textbox" value="<?php echo $emp_name;?>" />                       
</li>
<li >
<input type='button' id='btnDelete' name='btnDelete' class='button' value='Delete' onclick='deleteEmployee()' style='margin-left:10px;width:85px;display:inline' />
</li>
</ul>
</div>
?>

我将 $_SESSION["Msg"] 值从employeedb.php 传递给employee.php 这是我的employeedb.php

<?php
require_once("includes.php");
$genObj=new general();
$type=$_GET["type"];
$emp_id=$_POST["HFEmpID"];
if($type=="delete")
{
mysql_query("UPDATE employee SET del_flag=1 WHERE emp_id=".$emp_id."");
$_SESSION["Msg"]="Employee details deleted successfully!";
}
 header( 'Location:Employee.php');
?>

当我点击删除按钮时,正在调用这个 javascript 函数,这是我的 javascript 函数。

function deleteEmployee()
{
var del = confirm("Are you sure you want to delete this Employee?");
if (del == true)
{
document.forms[0].action="employeeDB.php?type=delete";
document.forms[0].submit();
}
else
{
document.forms[0].action="employeeDB.php";
}
}

这是我的includes.php,我已经开始了会话

<?php
session_start();
ob_start();
//Session Starts    
require_once("config/config.php");      
require_once("config/dbcon/dbcon.php");
require_once("classes/csGeneral.php");
require_once("classes/csPager.php");
require_once("classes/csImageFunctions.php");
require_once("classes/striptag.php");
/* $_SESSION["returnURL"]=$_SERVER['HTTP_REFERER'];  */
$genCheckUser=new general();
$genCheckUser->getUserIDCookie();
?>
4

3 回答 3

4

在检查变量是否为空之前,您必须检查变量是否确实存在。

所以将第 6 行更改为:

if (isset($_SESSION['Msg']))

从 PHP 手册:

isset — 确定变量是否已设置且不为 NULL

于 2013-03-25T16:07:58.133 回答
1

在您的代码中,您刚刚回显了$_SESSION['Msg']

<b><?php  echo $_SESSION["Msg"]; ?> </b>

这将在开始时出现错误,因为您尚未为此设置值,因为您之前没有执行过删除操作,以便消除错误

因此,您必须添加条件,如果 $_SESSION['Msg'] 尚未设置,请不要回显它..

<b><?php  if(!empty($_SESSION['Msg'])){ echo $_SESSION["Msg"];} ?> </b>
于 2013-03-25T06:19:14.160 回答
0
session_start();
ob_start();

应该:

ob_start();
session_start();
.
.
.
ob_flush();       <------- last statement
于 2013-03-25T07:06:51.590 回答