Jump to content

php form repeat button after submit and null input


sora

Recommended Posts

hello, 
i know the title looks little bit strange but i have an issue with my php form , after i submit with empty input box i don't get the echo results as wanted in the code with unanswered mark , as well when put right answer it gives back blank page after submitting or just the sentence of you like lemon . one more thing how can i add the button of repeat the exercise after submitting so it gives the form again for do the exercise over again, and how as well the button of showing results without taking the exam , means press the show results button shows them directly and thank you all in advance 

this is my code

<?php
$solution="do";
$solution3="do1";
if (isset($_POST['solution1'])){
  $solution1=$_POST['solution1'];
  if ($_POST['add']){
    if ($solution1 != $solution){
      echo '<span style="color: red;">'.$solution1.'</span> you like lemon?</br>';
    }
  } else if ($solution1 = null) { 
    echo '<span style="color: blue;">"unanswered"</span> you like lemon?</br>';
  } else { 
    echo '<span style="color: green;">'.$solution1.'</span> you like lemon?</br>';
  }
}
if (isset($_POST['solution2'])){
  $solution2=$_POST['solution2'];
  if ($_POST['add']){
    if ($solution2 != $solution3){
      echo '<span style="color: red;">'.$solution2.'</span> you like lemon?</br>' ;
    }
  } else if ($solution2 = null) { 
    echo '<span style="color: blue;">"unanswered"</span> you like lemon?</br>';
  } else { 
    echo '<span style="color: green;">'.$solution2.'</span> you like lemon?</br>';
  }
}
else {
?>
        <form   method="post" autocomplete="off" >
            <input type="text"  name="solution1" /> you like lemon?
            </br>
            <input type="text"  name="solution2" /> you like apple?
  
            <input  type="submit" name="add" value="Check" />
        </form>
<?php
}
?>

and thank you guys

Link to comment
Share on other sites

Lets take this step by step, the logic of this script is missed up try this

<?php

// The correct answer for solution1
$correctSolution1 = 'do';
// The correct answer for solution2
$correctSolution2 = 'do';

// If the user submitted the form
if(isset($_POST['add']) == TRUE) {
    
    /** Getting the value of solution1
     * @todo Always filter the values comming from a POST request.
     */
    
    $solution1 = $_POST['solution1'];
    
    // If the field solution1 was submitted empty
    if(empty($solution1) == TRUE) {
        echo '<span style="color: blue;">"unanswered"</span> you like lemon?</br>';
    }
    // The answer is correct
    elseif($solution1 == $correctSolution1) {
        echo '<span style="color: green;">'.$solution1.'</span> you like lemon?</br>';
    }
    // Wrong answer
    else {
        echo '<span style="color: red;">'.$solution1.'</span> you like lemon?</br>';
    }
    
    // We do the same for solution2
    
    $solution2 = $_POST['solution2'];
    
    // If the field solution1 was submitted empty
    if(empty($solution2) == TRUE) {
        echo '<span style="color: blue;">"unanswered"</span> you like lemon?</br>';
    }
    // The answer is correct
    elseif($solution2 == $correctSolution2) {
        echo '<span style="color: green;">'.$solution2.'</span> you like lemon?</br>';
    }
    // Wrong answer
    else {
        echo '<span style="color: red;">'.$solution2.'</span> you like lemon?</br>';
    }
    
}
// The user did not submit the form
// Display the form
else {
    ?>
        <form   method="post" autocomplete="off" >
            <input type="text"  name="solution1" /> you like lemon?
            </br>
            <input type="text"  name="solution2" /> you like apple?
  
            <input  type="submit" name="add" value="Check" />
        </form>
<?php
}
?>

For repeating the test you can use JavaScript or PHP itself.

Edited by JIXO
Link to comment
Share on other sites

thats right brother thank you, it works well 

whats the javascript code i can put for show answers button without answering any of question , and as well the button of repeating after submission

 

and thank you for your help

Link to comment
Share on other sites

You're welcome my friend, the code for repeating the exersice is just one line

I'm sorry I did not add numbers to lines in the last post, so I will post everything again :happy-04:

<?php

// The correct answer for solution1
$correctSolution1 = 'do';
// The correct answer for solution2
$correctSolution2 = 'do';

// If the user submitted the for
if(isset($_POST['add']) == TRUE) {
    
    /** Getting the value of solution1
     * @todo Always filter the values comming from a POST request.
     */
    
    $solution1 = $_POST['solution1'];
    
    // If the field solution1 was submitted empty
    if(empty($solution1) == TRUE) {
        echo '<span style="color: blue;">"unanswered"</span> you like lemon?</br>';
    }
    // The answer is correct
    elseif($solution1 == $correctSolution1) {
        echo '<span style="color: green;">'.$solution1.'</span> you like lemon?</br>';
    }
    // Wrong answer
    else {
        echo '<span style="color: red;">'.$solution1.'</span> you like lemon?</br>';
    }
    
    // We do the same for solution2
    
    $solution2 = $_POST['solution2'];
    
    // If the field solution1 was submitted empty
    if(empty($solution2) == TRUE) {
        echo '<span style="color: blue;">"unanswered"</span> you like lemon?</br>';
    }
    // The answer is correct
    elseif($solution2 == $correctSolution2) {
        echo '<span style="color: green;">'.$solution2.'</span> you like lemon?</br>';
    }
    // Wrong answer
    else {
        echo '<span style="color: red;">'.$solution2.'</span> you like lemon?</br>';
    }
    
    // This line will take us back to the form
    echo '<input type="button" name="back" value="Repeat test" onclick="history.go(-1); return false" \> ';
}
// The user did not submit the form
// Display the form
else {
    ?>
        <form   method="post" autocomplete="off" >
            <input type="text"  name="solution1" /> you like lemon?
            </br>
            <input type="text"  name="solution2" /> you like apple?
            <input  type="submit" name="add" value="Check" />
        </form>
<?php
}
?>

 

whats the javascript code i can put for show answers button without answering any of question

 

I didn't unserstand what you're trying to do exactly, can you explain more ?

Link to comment
Share on other sites

thank you friend for your ur help im appreciated it, wat i meant with the show answers button , is next the button check , wanna add another button funtions with answering the results of the test without take it, once u hit it , it takes u to result page shows rights answers , i hope you get my idea

Link to comment
Share on other sites

Hey, I've added a JavaScript function that will fill the form with the correct answers, however I couldn't submit the form automatically, I forgot how to do that : ,

 

I will try to make it work, here is the code up until now :

<!DOCTYPE html>
<html>
    
    <head>
        
        <title>Exam</title>
        
        <script type="text/javascript">
            function answer() {
                document.getElementById('solution1').value = document.getElementById('answer1').value;
                document.getElementById('solution2').value = document.getElementById('answer2').value;
            }
        </script>
        
    </head>
    
    <body>

<?php

// The correct answer for solution1
$correctSolution1 = 'do';
// The correct answer for solution2
$correctSolution2 = 'do';

// If the user submitted the for
if(isset($_POST['add']) == TRUE) {
    
    /** Getting the value of solution1
     * @todo Always filter the values comming from a POST request.
     */
    
    $solution1 = $_POST['solution1'];
    
    // If the field solution1 was submitted empty
    if(empty($solution1) == TRUE) {
        echo '<span style="color: blue;">"unanswered"</span> you like lemon?</br>';
    }
    // The answer is correct
    elseif($solution1 == $correctSolution1) {
        echo '<span style="color: green;">'.$solution1.'</span> you like lemon?</br>';
    }
    // Wrong answer
    else {
        echo '<span style="color: red;">'.$solution1.'</span> you like lemon?</br>';
    }
    
    // We do the same for solution2
    
    $solution2 = $_POST['solution2'];
    
    // If the field solution1 was submitted empty
    if(empty($solution2) == TRUE) {
        echo '<span style="color: blue;">"unanswered"</span> you like lemon?</br>';
    }
    // The answer is correct
    elseif($solution2 == $correctSolution2) {
        echo '<span style="color: green;">'.$solution2.'</span> you like lemon?</br>';
    }
    // Wrong answer
    else {
        echo '<span style="color: red;">'.$solution2.'</span> you like lemon?</br>';
    }
    echo '<input type="button" name="back" value="Repeat test" onclick="history.go(-1); return false" /> ';
}
// The user did not submit the form
// Display the form
else {
    ?>
        <form id="exam" method="post" autocomplete="off">
            <input type="text" id="solution1" name="solution1" /> you like lemon?
            <input type="hidden"  name="answer1" id="answer1" value="<?php echo $correctSolution1; ?>" />
            <br />
            <input type="text" id="solution2" name="solution2" /> you like apple?
            <input type="hidden"  name="answer2" id="answer2" value="<?php echo $correctSolution2; ?>" />
            <input  type="submit" name="add" value="Check" />
            <input  type="button" name="resolve" value="View answers" onclick="answer();" />
        </form>
<?php
}
?>
        </body>
    
</html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.