Page 1 of 1

Form redirection(somebody please help)

Posted: Sat Mar 22, 2008 8:30 am
by tabatsoy
Greetings Earthlings!
Please help me in my coding.
Here is my code:

Code: Select all

<?
    session_start();
    $db = mysql_connect("localhost");
    mysql_select_db("examination",$db);
 
        if(!$_SESSION['bago'] && !$_SESSION['bago2']){
            echo '<br><font size = "3" color = "#000000"><strong>Invalid session!
                  <br>You cannot view this page.</font></strong><br><br>';
            exit;
        }
    ?>
        
    
    <html>
    <body>
<FORM name ="form1" method ="post" action ="dummy.php">
 <table width="800" border="0"  align="center" class="underresult">
<?
        $query = "SELECT *
                FROM mathquestions";
        $result = mysql_query($query);
        for($i = 0; $i < mysql_num_rows($result); $i++){
            $question_number ++;
            $question = mysql_result($result, $i, "question");
            $choiceA = mysql_result($result, $i, "choiceA");
            $choiceB = mysql_result($result, $i, "choiceB");
            $choiceC = mysql_result($result, $i, "choiceC");
            $choiceD = mysql_result($result, $i, "choiceD");
            $answer = mysql_result($result, $i, "answer");
            $selected_radio = $_POST[$question_number];
            if ($selected_radio == $answer) {
                $score ++;
                $valid = true;
                if($valid){
                    $result2 = mysql_query("update examinee set math = '".$score."'
                        where firstname = '".$_SESSION['bago']."'");
                }
            }
            echo '<tr>
                    <td></td>';
                '</tr>';
            echo '<tr>
                    <td>&nbsp;</td>';
                '</tr>';
                
            echo '<tr>
                    <td width = "20" align = "left" valign = "top"><font class = "questions">'.$question_number.'.</span><td>';
                    echo '<td width = "280" align = "left" valign = "top" ><font class = "questions">'.$question.'</span></td>';        
                    echo '<td width = "125" align = "left" valign = "center" >&nbsp;</td>';
                    echo '<td width = "125" align = "left" valign = "center" >&nbsp;</td>';
                    echo '<td width = "125"align = "left" valign = "center" >&nbsp;</td>';
                    echo '<td width = "125" align = "left" valign = "center" >&nbsp;</td></tr>';
                    
            echo '<tr>
                    <td>&nbsp;</td>';
                '</tr>';
            
                    
            echo '<tr>
                    <td width = "20" align = "left" valign = "top"><td>';
                    echo '<td width = "280" align = "left" valign = "center" >&nbsp;</td>';     
                    echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number.'" type="radio" value="'.$choiceA.'" />'.$choiceA.'</span></td>';
                    echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number.'" type="radio" value="'.$choiceB.'" />'.$choiceB.'</span></td>';
                    echo '<td width = "125"align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number.'" type="radio" value="'.$choiceC.'" />'.$choiceC.'</span></td>';
                    echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number.'" type="radio" value="'.$choiceD.'" />'.$choiceD.'</span></td></tr>';
            
            echo '<tr>
                    <td colspan = "7" class = "exam">
&nbsp;</td>
                </tr>';             
}
    ?>
                        
            </table>
<br /><br />
<table width="240" border="0" align="center" class="session">
  <tr>
    <td> <Input type = "Submit" Name = "Submit" VALUE = "Submit">
  </tr>
</table>
<br />
</FORM>
</body>
</html>
   
all i want is when i press the submit button the score will be added in the database and it will redirect me on another page.
Or when i press on the submit button the score will be added in the database and a message will appear showing a message("Thank you for taking the exam").

thanks in advance please help :banghead: :banghead: :banghead: :banghead: :banghead: :banghead:

Re: Form redirection(somebody please help)

Posted: Sat Mar 22, 2008 10:56 am
by Christopher
The basic logic for forms is something like this:

Code: Select all

$form->initialize();
$form->processRequest();
if ($form->isSubmitted())  {
     if ($form->hasErrors()) {
          $errors = $form->getErrrorMessages();
     } else {
          $model->save($form->getValues());
          header("Location: http://www.mysite.com/");
          exit;
     )
} else {
     $form->setValues($model->load());
}
 
echo $errors;
echo $form;

Re: Form redirection(somebody please help)

Posted: Sat Mar 22, 2008 11:08 am
by tabatsoy
so, my code is all wrong??

Re: Form redirection(somebody please help)

Posted: Sat Mar 22, 2008 11:14 am
by John Cartwright
It's not a matter of right and wrong. Arborint's last post showed you the correct program flow, and is not a solution itself.

Re: Form redirection(somebody please help)

Posted: Sat Mar 22, 2008 11:20 am
by tabatsoy
ok. thank you very much earthlings.
thanks for the correct flow of information, i'll study that :drunk:

Re: Form redirection(somebody please help)

Posted: Sat Mar 22, 2008 1:37 pm
by Christopher
You don't have to do it OO either. I just used that style because it makes the steps readable. Post some code. Try a simple example first to get the structure correct -- then implement you actual page.