PHP:注意:使用$ _SESSION时“未定义的索引”

时间:2012-03-27 12:03:05

标签: php session-variables

我使用PHP并返回此值:$_SESSION['sessionNum']我收到错误:

Notice: Undefined index: sessionNum.

这个错误是什么意思?

以下是引用页面(create_session2.php)的错误PHP代码

<?php
  session_start();
?>
<script type="text/javascript">
  function validation() {
    var isDataValid = true;
    var sessionNoO = document.getElementById("sessionNo");            
    var errSessionNoMsgO = document.getElementById("sessionNoAlert")

    if(sessionNoO.value == ""){
      errSessionNoMsgO.innerHTML = "Please Set the Number of Sessions";
      isDataValid = false;
    } 
    else if (sessionNoO.value == 0){
      errSessionNoMsgO.innerHTML = "Number of Sessions Must be More than 0";
      isDataValid = false;
    } 
    else {
      errSessionNoMsgO.innerHTML = "";
    }   
    return isDataValid;
  }

  function showConfirm(){
    var confirmMsg=confirm(
      "Make sure that your details are correct, once you proceed after " +
      "this stage you would not be able to go back and change any " +
      "details towards your Session." + 
      "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" );

    if (confirmMsg==true){
      submitform();   
    }
  }

  function submitform(){
    var sessionFormO = document.getElementById("sessionForm");
    sessionFormO.submit();
  }
</script>
</head>

<body>
  <h1>CREATING A NEW SESSION</h1>
  <br/>
  <form action="QandATable2.php" 
    method="post" id="sessionForm">
    <p><strong> Number of Sessions you Require:</strong> 
      <input type="text" 
        id="sessionNo" 
        name="sessionNum" 
        onkeypress="return isNumberKey(event)" 
        maxlength="5" /><br/>
      <span id="sessionNoAlert"></span>
    </p>
    <p><input class="questionBtn" 
      type="submit" 
      value="Prepare Questions" 
      name="prequestion" onClick="myClickHandler(); return false;"/>
    </p>
    <!-- Prepare Questions here-->
  </form>

  <script type="text/javascript">
    function myClickHandler(){ 
      if(validation()){ 
        showConfirm(); 
      } 
    }
  </script>
  </body>

以下是当前页面的代码(QandATable2.php)

<script type="text/javascript">
  function showConfirm(){
    var confirmMsg=confirm(
      "Make sure that your details are correct, once you proceed after " +
      "this stage you would not be able to go back and change any details " +
      "towards Questions, Options and Answers for your Session." + 
      "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" );
    if (confirmMsg==true){
      submitform();   
    }
  }

  function submitform(){
    var sessionMarksO = document.getElementById("sessionMarks");
    sessionMarksO.submit();
  }

</script>

<body>
  <form id="enter" 
    action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" 
    method="post" onsubmit="return validateForm(this);" >
     <p>
       <input id="submitBtn" name="submitDetails" 
         type="submit" 
         value="Submit Details" 
         onClick="myClickHandler(); return false;" />

     </p>
   </form> 

   <script type="text/javascript">
     function myClickHandler(){
       if(validation()){
         showConfirm();
       }
     }
   </script>

   <?php
     session_start();

     $outputDetails = "";
     $outputDetails .= "
     <table id='sessionDetails' border='1'>
     <tr>
       <th>Number of Sessions:</th> 
       <th>$_SESSION['sessionNum'] = $_POST['sessionNum'];</th>
     </tr>";
     $outputDetails .= "        </table>";

     echo $outputDetails;
   ?> 

3 个答案:

答案 0 :(得分:2)

确保将session_start()作为页面上的第一件事并检查索引名称是否相同

答案 1 :(得分:2)

修改

您永远不会初始化$_SESSION['sessionNum']。 QandATable2.php需要看起来像:

<?php
  session_start();
  //validate the post data if necessary
  $_SESSION['sessionNum'] = $_POST['sessionNum'];
?>

<script type="text/javascript">

function showConfirm(){

  var confirmMsg=confirm("Make sure that your details are correct, once " +
    "you proceed after this stage you would not be able to go back and  " +
    "change any details towards Questions, Options and Answers for your  " +
    "Session." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" );

  if (confirmMsg==true){
    submitform();   
  }
}

function submitform()
{
    var sessionMarksO = document.getElementById("sessionMarks");
      sessionMarksO.submit();

}
</script>
<body>

<form id="enter" 
  action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"  
  method="post"  
  onsubmit="return validateForm(this);" >
      <p>
        <input id="submitBtn"  
          name="submitDetails" 
          type="submit" 
          value="Submit Details" onClick="myClickHandler(); return false;" />
      </p>
</form>

<script type="text/javascript">

  function myClickHandler(){
    if(validation()){
      showConfirm();
    }
  }
</script> 

<?php  
  $outputDetails = "";
  $outputDetails .= "
  <table id='sessionDetails' border='1'>
  <tr>
    <th>Number of Sessions:</th> 
    <th>$_SESSION['sessionNum']</th>
  </tr>";
  $outputDetails .= "</table>";

  echo $outputDetails;
?>

答案 2 :(得分:0)

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /web/stud/u0867587/Mobile_app/QandATable2.php on line 72

这是检查页面时出现的错误

我想请以正确的格式使用这个,我在下面发布试试这个

<?php  
session_start();
$_SESSION['sessionNum'] = $_REQUEST['sessionNum'];
$outputDetails = "";
$outputDetails .= "
<table id='sessionDetails' border='1'>
  <tr>
    <th>Number of Sessions:</th>
    <th>".$_SESSION['sessionNum']."</th>
 </tr>";
$outputDetails .= "</table>";

echo $outputDetails;
?>