How can I show the output vertically after submitting the html form?

user10625430

This is the screenshot of the output I am getting

The who;e sequence(having operands , opertors and answer) in output is generating randomly which is perfect. but I want to show that in vertical manner and this is the output I want to show after clicking on generate the expected output based on screenshot then Q(2)will show up in the same way but after Q(1).

this is my html form.

    <form action="" method="POST">
              <div class="row">
                 <div class="col-25">
                   <label for="qnum">Select no.of questions:</label>
                    </div>
                     <div class = "col-75"><input type="number" id="qnum" name="que" value="1"  min="1" max="100"></div>
                     <br /><br />
               </div>
             <br />

        <div class="row">
              <div class="col-25">
                <label for="int">How many numbers You want in a sequence:</label></div>
                    <div class="col-75">
                       <select name="select" id="int">
                        <option value="2"> 2 </option>
                        <option value="3"> 3 </option>
                        <option value="4"> 4 </option>
                        <option value="5"> 5 </option>
                        <option value="6"> 6 </option>
                        <option value="7"> 7 </option>
                        <option value="8"> 8 </option>
                        <option value="9"> 9 </option>
                        <option value="10"> 10 </option>
                      </select>
                    </div>
      </div>
      <br /> 

       <div class="row">
           <div class="col-25">
             <label for="dig">Select no. of digits:</label></div>
                <div class="col-75">
                  <select name="digits" id="dig">
                   <option value="1"> 1 digit</option>
                   </select>
              </div>
              <br /><br /><br /><br />
        </div>

          <div class="row">
          <div class="col-25"><label>Select operations:</label><br /></div>
              <div class="col-75">
                <input type="checkbox" id="mix" value="all" class="toggle-button"><label>All</label>
                <input type="checkbox" id="add" name="operation[]" value="Addition" checked><label>Addition</label>
                <input type="checkbox" id="sub" name="operation[]" value="Subtraction"><label>Subtraction</label>
                <input type="checkbox" id="mult" name="operation[]" value="Multiplication"><label>Multiplication</label>
                <input type="checkbox" id="div" name="operation[]" value="Division"><label>Division</label>
               </div>
                <br /><br />
                <br /><br />
            </div><br />


        <input type="submit" name="submit" value="Generate"><br>
         <br />

 </form>
   </div>
      <!---Toggle button to select all operations if user select checkbox named "All" -->
           <script language="JavaScript">
                $( '.col-75 .toggle-button' ).click( function () {
                $( '.col-75 input[type="checkbox"]' ).prop('checked', this.checked)
      })
          </script>

Basically I generating a random sequence of arithmetic expressions which is working perfectly without any error. But after clicking generate button the output is showing horizontally and I want to generate it vertically. So how can I do that?

Below is my php code which is generating the sequence perfectly without any error. How can I generate the output vertically?

<?php
  error_reporting(E_ALL & ~E_NOTICE);

  if(isset($_POST['submit']))
  {
       //validation for no of operation selected
       $operation = $_POST['operation'];
       if(empty($operation)) 
             {
               echo "Please Select at least one operation <br/>";
             }
              else
              {  
                 $N = count($operation); //count selected operation
                    echo  "<br />";
                     echo "You have selected $N operation(s):"; 

                            for($i=0; $i < $N; $i++)  //for loop for for operator array.
                               {
                                  echo($operation[$i] . ", "); //display selected operation
                               }
                               echo "<br />";
                     //checkbox operations
                    if($N==1)   //if only one operation is selected
                        {
                             if($operation[0]=='Addition')
                                 {
                                      $rOperators = array('+');                           
                                 }
                                else if($operation[0]=='Subtraction')
                                   {
                                      $rOperators = array('-');
                                    }
                                    else if($operation[0]=='Multiplication')
                                      {
                                      $rOperators = array('*');
                                       }
                                        else
                                          {
                                            $rOperators = array('/');
                                          }
                        }
                    else if ($N==2) //if only two operations are selected by the user
                     {
                        if($operation[0]=='Addition')
                            {
                                if($operation[1]=='Subtraction')
                                    {
                                        $rOperators = array('+','-');
                                    }
                                     else if($operation[1]=='Multiplication')
                                         {
                                           $rOperators = array('+','*');
                                         }
                                          else
                                             {
                                               $rOperators = array('+','/');
                                             }                           
                            }
                            else if($operation[0]=='Subtraction')
                            {
                                if($operation[1]=='Multiplication')
                                     {
                                        $rOperators = array('-','*');
                                     }
                                    else
                                      {
                                        $rOperators = array('-','/');
                                      }
                            }
                              else
                                {
                                  $rOperators = array('*','/');
                                }
                      }
                        else if ($N==3) //if 3 operators gets selected by user
                            {
                                if($operation[0]=='Addition')
                                 {
                                     if($operation[1]=='Subtraction')
                                         {
                                             if($operation[2]=='Multiplication')
                                             {
                                                 $rOperators = array('+','-','*');    
                                             }
                                             else
                                             {
                                                 $rOperators = array('+','-','/');
                                             }

                                         }
                                      else
                                         {
                                              $rOperators = array('+','*','/');
                                         }                         
                                 }
                                 else
                                 {
                                     $rOperators = array('-','*','/');
                                 }
                            }
                        else
                            {
                                $rOperators = array('+','-','*','/');
                            }

             }

//display sequence having only single digit numbers
if($_POST['digits'] == 1)
     {
        $q = $_POST['que'];
             $previousInt = null;        //Track the previous operand
             $s = $_POST['select'];

                     for ($x = 1; $x<=$q; $x++) //for loop for no. of questions
                        {
                           $randomOperands = array();
                            $randomOperators = array();

                                    // loop over $i n times
                                   for ($i = 1; $i <=$s; $i++) //for loop for no. of integers user entered
                                     {
                                        $nextInt = rand(1, 9); // assign random operand to array slot
                                         $randomOperands[] = $nextInt;
                                            if($i < $s)
                                             {   
                                               if($previousInt === 0) //No operator after last opearnd
                                                  {
                                                 //avoid division-by-zero
                                                   $randomOperators[] = $rOperators[rand(0, 2)];
                                                  }
                                                else
                                                  {
                                                   $randomOperators[] = $rOperators[rand(0, $N-1)];
                                                  } 
                                              }  
                                        $previousInt = $nextInt;
                                      }
                                    // print array values
                                    $exp = '';     //Generating the expression
                                      foreach($randomOperands as $key=>$value1)
                                         {
                                          $exp .=  $value1 . " " ;  
                                             if(isset($randomOperators[$key]))
                                                {
                                                 $exp .=  $randomOperators[$key] ." "; 
                                                }
                                         }
                                         $res = eval("return ($exp);");//evaluate the expression 
                                           //print expression
                                        echo ("This is Q(".$x."):"), $exp."=". $res."<br>";
                          }
        }
    }
    ?>
karthzDIGI

Modify the code in which you are generating the expression which you are using as the input to the eval() function. Create another string say $output_string which can be used to output your answer. In this newly created string use proper HTML tags along with the generated string, for formatting it the way you want your output to look like.

It should look something like this:

$exp = '';     //Generating the expression
$output_string = ''; // new string which can be used for generating the output string
foreach ( $randomOperands as $key => $value1 ) {
    $exp .=  $value1 . " " ;
    $output_string .=  $value1 . " <br>" ;
    if ( isset($randomOperators[$key]) ) {
        $exp .=  $randomOperators[$key] ." ";
        $output_string .=  $randomOperators[$key] ." <br> "; 
    }
}
$res = eval("return ($exp);");//evaluate the expression 
//print expression
echo ("This is Q(".$x."): <br>"), $output_string."= <br>". $res."<br>";

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to show a success dialog box after submitting the form in html?

How can I edit a google sheets HTML UI element after submitting form?

How can I empty this AngularJS form after submitting the data?

How can I get the form values to appear in the table after submitting?

How can I check if form input is not all number before submitting form and show message?

How to hide the html form and then show the output after clickin on submit button?

How can I show a dialog before submitting?

How can I redirect to a new page after submitting a form using Jinja template?

How can I decrement a field value from firestore after submitting a form successfully?

How can I insert a variable into the resulting url parameter string after submitting my GET form?

How can i get data or pass a data from one page to another after submitting a form in reactjs

How can i automatically insert the current user_id after submitting a form in Filament v3.0

How can I show a part of form after a yes/no radio?

How can I make this loader show only after form validation?

How can I edit form json data before submitting a form?

how to show the output after the form submit in JavaScript

How can I show and hide the form in HTML page?

How to redirect to another HTML page after submitting a form?

How to open another submit form after submitting one on html or javascript?

Show bootstrap popover after submitting form that is in modal

How do I redirect users after successfully submitting form data?

How to redirect after submitting form

How can I change the state submitting a form in React?

How can I use javascript to add a value to a form before submitting it?

How can i redirect on submitting my form in reactjs and using formik

HTML5 show hidden form elements after submitting and showing the required tags

Display a hidden div after a html form submit to show output

How can I reset a form field and show Success or Thanks you after form submission?

After submitting simple_form i get a blank page in show view