Form redirection(somebody please help)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tabatsoy
Forum Commoner
Posts: 29
Joined: Thu Mar 13, 2008 10:14 am

Form redirection(somebody please help)

Post 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:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Form redirection(somebody please help)

Post 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;
(#10850)
tabatsoy
Forum Commoner
Posts: 29
Joined: Thu Mar 13, 2008 10:14 am

Re: Form redirection(somebody please help)

Post by tabatsoy »

so, my code is all wrong??
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Form redirection(somebody please help)

Post 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.
tabatsoy
Forum Commoner
Posts: 29
Joined: Thu Mar 13, 2008 10:14 am

Re: Form redirection(somebody please help)

Post by tabatsoy »

ok. thank you very much earthlings.
thanks for the correct flow of information, i'll study that :drunk:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Form redirection(somebody please help)

Post 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.
(#10850)
Post Reply