-1

可能重复:
遇到函数结构不遵循的问题(PHP)

问题:每次我提交按钮时,它都不会在函数answerSubmit中显示回声,而是返回到函数。student answersStudentAnswers()pickSession()

我在这里有一个演示,你可以用它来看看发生了什么:DEMO

当您打开演示时,从评估(会话)下拉菜单中选择评估。您将看到另外 2 个下拉菜单studentsquestions出现下拉菜单。现在,如果您单击answerSubmit按钮(Get Students Answers按钮),那么它应该显示回声说明student answers,但是您可以看到它返回到仅显示评估下拉菜单。为什么是这样?

下面是演示的代码:

function PickSession()
    {

//mysqli code retreiving data for options in assessments drop down menu goes here ....  

        ?>

        <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> 
            <p>
            <strong>Asessments:</strong>
            <select name="session" id="sessionsDrop">
                <option value="">Please Select</option>
                <?php
                    while ( $sessionqrystmt->fetch() ) {
                        $sv = $dbSessionId;
                        if(isset($_POST["session"]) && $sv == $_POST["session"]) 
                            echo "<option selected='selected' value='$sv'>" .
                                  $dbSessionName .
                                  " - " .
                                  date('d-m-Y',strtotime($dbSessionDate)) .
                                  " - " . 
                                  date('H:i',strtotime($dbSessionTime)) .
                                 "</option>" . PHP_EOL;
                        else
                            echo "<option value='$sv'>" .
                                  $dbSessionName . 
                                  " - " . 
                                  date('d-m-Y',strtotime($dbSessionDate)) . 
                                  " - " .
                                  date('H:i',strtotime($dbSessionTime)) .
                                  "</option>" . PHP_EOL;
                    }
                ?>
            </select>
            </p>
            <input id="sessionSubmit" type="submit" value="Submit Assessments" name="sessionSubmit" />
        </form>
    <?php
    }

    function SessionIsSubmitted()
    {
        if(isset($_POST['sessionSubmit'])) // we have subbmited the second form
        {
            if(empty($_POST["session"])) // We picked the "Please select" option
            { ?>
                    Please Select an Assessment

            <?php 
                return false;
            }
            else // All is ok
            {
                return true;
            }
        }
        return false;
    }


    function ShowAssessment()
    {   

//mysqli code retreiving data for options in students drop down menu goes here ....  

    if($studentnum == 0){ ?>

    There are no Students who have currently taken this Assessment

    <?php } else { 


//mysqli code retreiving data for options in questions drop down menu goes here .... 

            ?>

    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">         
    <p>
    <strong>Student:</strong>
    <select name="student" id="studentsDrop">
    <option value="All">All</option>
    <?php
    while ( $currentstudentstmt->fetch() ) {
    $stu = $dbStudentId;
    if(isset($_POST["student"]) && $stu == $_POST["student"]) 
        echo "<option selected='selected' value='$stu'>" .
             $dbStudentAlias . " - " . $dbStudentForename . " " .
             $dbStudentSurname . "</option>" . PHP_EOL;
    else
        echo "<option value='$stu'>" . $dbStudentAlias . " - " . 
              $dbStudentForename . " " . $dbStudentSurname . 
              "</option>" . PHP_EOL;
    }
    ?>
    </select>
    </p>

    <p>
    <strong>Question:</strong>
    <select name="question" id="questionsDrop">
    <option value="All">All</option>
    <?php
    while ( $questionsstmt->fetch() ) {
    $ques = $dbQuestionId;
    if(isset($_POST["question"]) && $ques == $_POST["question"]) 
        echo "<option selected='selected' value='$ques'>" . 
              $dbQuestionNo . "</option>" . PHP_EOL;
    else
        echo "<option value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
    }
    ?>
    </select>
    </p>

    <input id="answerSubmit" type="submit" value="Get Student's Answers" name="answerSubmit" />
    </form>

    <?php
    }
    }

    function StudentAnswersIsSubmitted()
    {
        if(isset($_POST['answerSubmit'])) // we have subbmited the second form
        {

        return false;

        }
        return false;
    }

    function StudentAnswers()
    {

    echo "student answers";

    }

    ?>

...

 <body>


    <?php


            PickSession(); // Show the thing to pick session
            if(SessionIsSubmitted()) // When session is picked
            {
              ShowAssessment(); // Show students and questions information
              if(StudentAnswersIsSubmitted()) // Student Answers button is submitted
                {
                  StudentAnswers();
                }

            }



    ?>


    </body>

更新:

function PickSession()
    {

//mysqli code retreiving data for options in assessments drop down menu goes here ....  

        ?>

        <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> 
               <p>
            <strong>Asessments:</strong>
            <select name="session" id="sessionsDrop">
                <option value="">Please Select</option>
                <?php
                    while ( $sessionqrystmt->fetch() ) {
                        $sv = $dbSessionId;
                        if(isset($_POST["session"]) && $sv == $_POST["session"]) 
                            echo "<option selected='selected' value='$sv'>" . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "</option>" . PHP_EOL;
                        else
                            echo "<option value='$sv'>" . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "</option>" . PHP_EOL;
                    }
                ?>
            </select>
            </p>
            <input id="sessionSubmit" type="submit" value="Submit Assessments" name="sessionSubmit" />
        </form>
    <?php
    }


function SessionIsSubmitted()
{
        if(isset($_POST["session"]) && empty($_POST["session"])) // We picked the "Please select" option
        { ?>
            <div class="red">
                Please Select an Assessment
            </div>
        <?php 
            return false;
        }
    else if(!isset($_POST["session"]))
    {
        return false;
    }
    else // All is ok
    {
        return true;
    }
    return false;

}


    function ShowAssessment()
    {   

//mysqli code retreiving data for options in students drop down menu goes here ....  

    if($studentnum == 0){ ?>

    There are no Students who have currently taken this Assessment

    <?php } else { 


//mysqli code retreiving data for options in questions drop down menu goes here .... 

            ?>

    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">         
    <p>
    <input type="text" name="session" value="<?php echo $_POST['session']; ?>">
    <strong>Student:</strong>
    <select name="student" id="studentsDrop">
    <option value="All">All</option>
    <?php
    while ( $currentstudentstmt->fetch() ) {
    $stu = $dbStudentId;
    if(isset($_POST["student"]) && $stu == $_POST["student"]) 
        echo "<option selected='selected' value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
    else
        echo "<option value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
    }
    ?>
    </select>
    </p>

    <p>
    <strong>Question:</strong>
    <select name="question" id="questionsDrop">
    <option value="All">All</option>
    <?php
    while ( $questionsstmt->fetch() ) {
    $ques = $dbQuestionId;
    if(isset($_POST["question"]) && $ques == $_POST["question"]) 
        echo "<option selected='selected' value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
    else
        echo "<option value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
    }
    ?>
    </select>
    </p>

    <input id="answerSubmit" type="submit" value="Get Student's Answers" name="answerSubmit" />
    </form>

    <?php
    }
    }

function StudentAnswersIsSubmitted()
{

if(!isset($_POST["answerSubmit"]))
    {
        return false;
    }
    else // All is ok
    {
        return true;
    }
    return false;

}

    ?>

更新 2:

这是以下代码的演示:演示

function PickModule()
{ ?>
    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> 
        <?php            
        $moduleactive = 1;

        $sql = "SELECT ModuleId, ModuleNo, ModuleName FROM Module WHERE ModuleActive = ? ORDER BY ModuleNo"; 

        //mysqli code for modules drop down menu

        ?>
        <strong>Module:</strong>
        <select name="module" id="modulesDrop">
            <option value="">Please Select</option>
            <?php
            while($sqlstmt->fetch()) { 
                $ov = $dbModuleNo . "_" . $dbModuleName . "_" . $dbModuleId; 
                if(isset($_POST["module"]) && $ov == $_POST["module"]) 
                    echo "<option selected='selected' value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL; 
                else 
                    echo "<option value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL;
             } 
            ?>
        </select>
        <br>
        <input id="moduleSubmit" type="submit" value="Submit Module" name="moduleSubmit" />
    </form>
<?php }
function ModuleIsSubmitted()
{
    if(isset($_POST["module"]) && empty($_POST["module"])) // We picked the "Please select" option
    { ?>
            Please Select a Module
    <?php 
        return false;
    }
    else if(!isset($_POST["module"]))
    {
        return false;
    }
    else // All is ok
    {
        return true;
    }
    return false;
}
function PickSession()
{

    $dataTransfered = explode( "_" , $_POST["module"] );
    $moduleNo = $dataTransfered[0];
    $moduleName = $dataTransfered[1];
    $moduleId = $dataTransfered[2];

    //Get data from database
    $sessionquery = "
        SELECT s.SessionId, SessionName, SessionDate, SessionTime, ModuleId, SessionActive, Complete
        FROM Session s
        INNER JOIN Session_Complete sc ON sc.SessionId = s.SessionId
        WHERE
        (ModuleId = ? AND Complete = ?)
        ORDER BY SessionName 
        ";
    $complete = 1;

//mysqli code for assessments drop down menu  

    ?>

    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> 
        <input type="hidden" name="module" value="<?php echo $_POST['module']; ?>">
        <p>
            <strong>Selected Module: </strong><?php echo $moduleNo ." - ". $moduleName; ?> 
        </p>
    <?php if ($sessionnum == 0 ){ ?>
        <div class="red">
            Sorry, You have No Assessments under this Module
        </div>
    <?php } else { ?>
           <p>
        <strong>Asessments:</strong>
        <select name="session" id="sessionsDrop">
            <option value="">Please Select</option>
            <?php
                while ( $sessionqrystmt->fetch() ) {
                    $sv = $dbSessionId;
                    if($dbSessionActive == 0){
                        $class = 'red';
                    }else{
                        $class = 'green';   
                    }
                    if(isset($_POST["session"]) && $sv == $_POST["session"]) 
                        echo "<option selected='selected' value='$sv' class='$class'>" . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "</option>" . PHP_EOL;
                    else
                        echo "<option value='$sv' class='$class'>" . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "</option>" . PHP_EOL;
                }
            ?>
        </select>
        </p>
        <input id="sessionSubmit" type="submit" value="Submit Assessments" name="sessionSubmit" />
    </form>
<?php
}
}
function SessionIsSubmitted()
{
        if(isset($_POST["session"]) && empty($_POST["session"])) // We picked the "Please select" option
        { ?>
            <div class="red">
                Please Select an Assessment
            </div>
        <?php 
            return false;
        }
    else if(!isset($_POST["session"]))
    {
        return false;
    }
    else // All is ok
    {
        return true;
    }
    return false;

}


function ShowAssessment()
{   

$studentactive = 1;

$currentstudentqry = "
SELECT
st.StudentId, st.StudentAlias, st.StudentForename, st.StudentSurname
FROM
Student_Session ss 
INNER JOIN
Student st ON ss.StudentId = st.StudentId
WHERE
(ss.SessionId = ? AND st.Active = ?)
ORDER BY st.StudentAlias
";

//mysqli code for students drop down menu


if($studentnum == 0){ ?>

<div class="red">
There are no Students who have currently taken this Assessment
</div>
<?php } else { 

$questionsqry = "
SELECT
QuestionId, QuestionNo
FROM
Question
WHERE
(SessionId = ?)
ORDER BY QuestionNo
";

//mysqli code for questions drop down menu

        ?>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">         
<p>
<input type="text" name="session" value="<?php echo $_POST['session']; ?>">
<strong>Student:</strong>
<select name="student" id="studentsDrop">
<option value="All">All</option>
<?php
while ( $currentstudentstmt->fetch() ) {
$stu = $dbStudentId;
if(isset($_POST["student"]) && $stu == $_POST["student"]) 
    echo "<option selected='selected' value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
else
    echo "<option value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
}
?>
</select>
</p>

<p>
<strong>Question:</strong>
<select name="question" id="questionsDrop">
<option value="All">All</option>
<?php
while ( $questionsstmt->fetch() ) {
$ques = $dbQuestionId;
if(isset($_POST["question"]) && $ques == $_POST["question"]) 
    echo "<option selected='selected' value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
else
    echo "<option value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
}
?>
</select>
</p>

<input id="answerSubmit" type="submit" value="Get Student's Answers" name="answerSubmit" />
</form>

<?php
}
}

function StudentAnswersIsSubmitted()
{

if(!isset($_POST["answerSubmit"]))
    {
        return false;
    }
    else // All is ok
    {
        return true;
    }
    return false;

}

function StudentAnswers()
{

echo "student answers";

}

?>

…………

<?php


    PickModule(); // Show the thing to pick module

    if(ModuleIsSubmitted()) // When module is picked
    {
        PickSession(); // Show the thing to pick session
        if(SessionIsSubmitted()) // When session is picked
        {
          ShowAssessment(); // Show students and questions information
          if(StudentAnswersIsSubmitted()) // Student Answers button is submitted
            {
              StudentAnswers();
            }

        }

    }



?>
4

1 回答 1

1

当您提交第二个表单时,该SessionIsSubmitted函数不会通过,因为您页面上的第二个表单不包含所需的信息 (sessionSubmit)。仅在第sessionSubmit一个表单内,因此不会被提交。

你需要在你的第二个表格中有一些东西可以让你通过第一个表格测试

于 2013-01-27T17:26:17.963 回答