2012-03-27 167 views
-2

我使用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; 
    ?> 
+2

在嘗試訪問'$ _SESSION'之前,您是否調用'session_start();'?您可以使用'isset($ _ SESSION [「sessionNum」])''函數檢查您的元素是否已設置。 – Crontab 2012-03-27 12:05:28

+0

你在鏈接中粘貼了一個錯誤的URL – Saikios 2012-03-27 12:05:46

+0

你在哪裏初始化會話? – scibuff 2012-03-27 12:05:55

回答

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; 
?> 
+0

你能用所有你正在使用的CURRENT代碼更新你的問題嗎? – 2012-03-27 12:57:39

+0

我已經更新了代碼,我只更改了QandATable2.php(第二代碼塊)中的代碼,但您在那裏看到更新後的代碼。 – user1292857 2012-03-27 13:03:16

+0

我更新了我的答案。 – 2012-03-27 13:13:55

2

確保您有在session_start( )作爲頁面上的第一件事,並檢查索引名稱是否相同

+0

嗨,對不起,我確實包括會話開始,但沒有粘貼在上面的代碼。我沒有將它包含在上一頁中,但我會試試這個並回復你 – user1292857 2012-03-27 12:22:56

2
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; 
?>