Home > Back-end >  is there a way to get the value from a input before the form
is there a way to get the value from a input before the form

Time:10-09

I needed to display the score above the form. is this possible or is there a ways for it.

I want to display the score after the form is submitted. the current code displays it below the form, I need it to display above the form

it is giving me a error "undefined array key"

<?php
$ans = array();
$GLOBALS['score'] = 0;
$answer = array("Greenland", "Sun", "H2O", "Diamond", "-4a   22b", "Debugging", "Consummatum est", "he'd", "Douglas macarthur", "x2   x - 20"); 
extract($_POST);
    $questions= array(
                array("What is the biggest Island in the world","Greenland", "Hawaii", "Porto Rico"), //Greenland
                array("Nearest star to earth", "Sirius", "Sun", "Alcyone"), //Sun
                array("Chemical formula for water is", "NaAlO2", "H2O", "CaSiO3"), //H2O
                array("The hardest substance available on earth is", "Vibranium", "Adamantium", "Diamond"), //Diamond
                array("Combine terms: 12a   26b -4b – 16a.", "4a   22b", "-28a   30b", "-4a   22b"), //-4a   22b
                array("The process of finding errors and fixing them within a program.", "Fixing", "Finding", "Debugging"), //Debugging
                array("Dr. Jose Rizal's last words", "Farewell my friend", "See you in the other side", "Consummatum est"), //Consummatum est
                array("He said that ________ love me", "he'd", "he would", "he woulded"), //he'd
                array("Who said the line I SHALL RETURN", "Jesus Christ", "Sir Arni", "Douglas macarthur"), //Douglas macarthur
                array("Multiply: (x – 4)(x   5)", "x2   x - 20", "x(x   5) -4(x   5)", "7")); //x2   x - 20 
    echo "<table>";
    echo "<tr>";
    echo "<td style=\"border-top: 14px solid #7349BD;\"><h3>Answer the Following:</h3>";
    //show score here
        if(isset($submit))
        {
        echo "<h2>Congratulation $name, Your score is ";
        echo $_POST['point'];
        echo "/10</h2>";
    
        }
    echo "<form action=\"LabAct2_2.php\" method=\"post\">";
        $counter=0;
        echo "<hr><label>Name: </label><input type=\"text\" name=\"name\" required>";
        echo "<p>*Required</p>";
        echo "</td></tr>";
        foreach($questions as $value){
            $counter  ;
            echo "<tr>";
            echo "<td>";
            foreach($value as $v=>$item)
            {
                if ($v==0)
                {
                    echo "<label>$counter. $item</label><br>";
                }else{
                    echo "<input type=\"radio\" name=\"option$counter\" value=\"$item\" id=\"$item\"><label for=\"$item\">$item</label><br>";
                }
            }
        echo "</td>";
        echo "</tr>";
    }
    error_reporting(0);
    echo "</table>";
    echo "<div>";
    $ans = array($_POST["option1"], $_POST["option2"],$_POST["option3"],$_POST["option4"],$_POST["option5"],$_POST["option6"],$_POST["option7"],$_POST["option8"],$_POST["option9"],$_POST["option10"]); // user answer
    $result = array_intersect($answer, $ans); //correct answers
    $name = $_POST['name'];
    $score = count($result); //score
    echo "<input type=\"hidden\" value=\"$score\" name=\"point\">";
    echo "<input type=\"submit\" name=\"submit\" value=Submit></form><br>";


    if(isset($submit)){
        echo "<h2>Congratulation $name, Your score is ";
        echo $score;
        echo "/10</h2></div>";
    }
    
    ?>

CodePudding user response:

Ok I am editing my answer following code should give you the results, notice I moved the code that was calculating score to place httml output is starting.

This code handle your errors too

<?php
$ans = array();
$GLOBALS['score'] = 0;
$answer = array("Greenland", "Sun", "H2O", "Diamond", "-4a   22b", "Debugging", "Consummatum est", "he'd", "Douglas macarthur", "x2   x - 20"); 
extract($_POST);
$questions= array(
            array("What is the biggest Island in the world","Greenland", "Hawaii", "Porto Rico"), //Greenland
            array("Nearest star to earth", "Sirius", "Sun", "Alcyone"), //Sun
            array("Chemical formula for water is", "NaAlO2", "H2O", "CaSiO3"), //H2O
            array("The hardest substance available on earth is", "Vibranium", "Adamantium", "Diamond"), //Diamond
            array("Combine terms: 12a   26b -4b – 16a.", "4a   22b", "-28a   30b", "-4a   22b"), //-4a   22b
            array("The process of finding errors and fixing them within a program.", "Fixing", "Finding", "Debugging"), //Debugging
            array("Dr. Jose Rizal's last words", "Farewell my friend", "See you in the other side", "Consummatum est"), //Consummatum est
            array("He said that ________ love me", "he'd", "he would", "he woulded"), //he'd
            array("Who said the line I SHALL RETURN", "Jesus Christ", "Sir Arni", "Douglas macarthur"), //Douglas macarthur
            array("Multiply: (x – 4)(x   5)", "x2   x - 20", "x(x   5) -4(x   5)", "7")); //x2   x - 20 

if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST'){
    //$ans = array($_POST["option1"], $_POST["option2"],$_POST["option3"],$_POST["option4"],$_POST["option5"],$_POST["option6"],$_POST["option7"],$_POST["option8"],$_POST["option9"],$_POST["option10"]); // user answer
    
    //echo '<pre>'.print_r($_POST,true).'</pre>';
    foreach($_POST as $key=>$value){
        if(substr($key,0,6) === 'option'){
            $ans[] = $value;    
        }
    }
    //echo '<pre>'.print_r($ans,true).'</pre>';
    $result = array_intersect($answer, $ans); //correct answers
    $name = $_POST['name'];
    $score = count($result); //score
}
echo "<table>";
echo "<tr>";
echo "<td style=\"border-top: 14px solid #7349BD;\"><h3>Answer the Following:</h3>";
//show score here
    if(isset($submit))
    {
    echo "<h2>Congratulation $name, Your score is ";
    echo $score;
    echo "/10</h2>";

    }
echo "<form action=\"LabAct2_2.php\" method=\"post\">";
    $counter=0;
    echo "<hr><label>Name: </label><input type=\"text\" name=\"name\" required>";
    echo "<p>*Required</p>";
    echo "</td></tr>";
    foreach($questions as $value){
        $counter  ;
        echo "<tr>";
        echo "<td>";
        foreach($value as $v=>$item)
        {
            if ($v==0)
            {
                echo "<label>$counter. $item</label><br>";
            }else{
                echo "<input type=\"radio\" name=\"option$counter\" value=\"$item\" id=\"$item\"><label for=\"$item\">$item</label><br>";
            }
        }
    echo "</td>";
    echo "</tr>";
}
//error_reporting(0);
echo "</table>";
echo "<div>";
echo "<input type=\"hidden\" value=\"$score\" name=\"point\">";
echo "<input type=\"submit\" name=\"submit\" value=Submit></form><br>";


if(isset($submit)){
    echo "<h2>Congratulation $name, Your score is ";
    echo $score;
    echo "/10</h2></div>";
}

?>

CodePudding user response:

The value of $submit is undefined until the form is submitted

  • Related