最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Undefined index error for session on loading of page - Stack Overflow

programmeradmin1浏览0评论

I am getting error "Undefined index: Msg in Employee.php " when am deleting the employee. Am trying to display the session value from employeedb.php. Here is my employee.php. Here am trying to display session message inside the 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>
?>

I am passing the $_SESSION["Msg"] value from employeedb.php to employee.php this is my 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');
?>

When am clicking on delete button , am calling this javascript function, this is my javascript function.

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";
}
}

this is my includes.php where I started session already

<?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();
?>

I am getting error "Undefined index: Msg in Employee.php " when am deleting the employee. Am trying to display the session value from employeedb.php. Here is my employee.php. Here am trying to display session message inside the 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>
?>

I am passing the $_SESSION["Msg"] value from employeedb.php to employee.php this is my 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');
?>

When am clicking on delete button , am calling this javascript function, this is my javascript function.

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";
}
}

this is my includes.php where I started session already

<?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();
?>
Share Improve this question edited Mar 25, 2013 at 7:13 Jhonathan H. 2,7131 gold badge20 silver badges28 bronze badges asked Mar 25, 2013 at 6:14 SunishSunish 973 silver badges12 bronze badges 4
  • 2 Have you started session session_start() ? – Edwin Alex Commented Mar 25, 2013 at 6:16
  • yes I had already started seesion in includes.php – Sunish Commented Mar 25, 2013 at 6:18
  • passing the value for $emp_id is missing. – Jhonathan H. Commented Mar 25, 2013 at 6:28
  • Am passing the value here for $emp_id using a hidden field, I didnt post the full code. I just need to display the session message after deleting the employee – Sunish Commented Mar 25, 2013 at 6:32
Add a ment  | 

3 Answers 3

Reset to default 4

You have to check if the variable indeed exists before checking if its empty.

So change line 6 to:

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

From the PHP manual :

isset — Determine if a variable is set and is not NULL

In your code you just echoed $_SESSION['Msg']

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

this will have error at the start because you havent set a value for this one because you havent done delete before so in order to remove the error

So you must add the condition that if $_SESSION['Msg'] is not yet set dont echo it..

<b><?php  if(!empty($_SESSION['Msg'])){ echo $_SESSION["Msg"];} ?> </b>
session_start();
ob_start();

should be:

ob_start();
session_start();
.
.
.
ob_flush();       <------- last statement
发布评论

评论列表(0)

  1. 暂无评论