1

here is my form in which there are 4 checkboxes for 4 operations.

<form action="" method="POST">
 Select no.of questions:<input type="number" name="que" value="que">
 <br> <br>
 Select no. of series: <select name="select_box">
    <option value="0">Please Select</option>
    <option value="1"> 2 </option>
    <option value="2"> 3 </option>
</select>

<br><br>
  Select number type(in digits) <input type="number" name="digits" 
value="digits">
 <br><br>
Select operations:<br />
<input type="checkbox" id="add" name="operation" value="addition" 
id="add"><label>Addition</label>
<br/>
<input type="checkbox" id="sub" name="operation" value="substraction" 
id="sub"><label>substraction</label><br/>
<input type="checkbox" id="add" name="operation" value="multiplication" 
id="mul"><label>Multiplication</label><br/>
 <input type="checkbox" id="add" name="operation" value="division" 
id="div"><label>Division</label><br/><br / >

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

now how can i bind the respective event to respective checkbox (ex.If user click on addition then then only addition will perform) and in case user select 2 checkboxes at a time suppose add and subtract then only those two operation will have to perform ?

here is my php code where I am doing the rest code

 <?php

  //for 1 digits
 if($_POST['digits'] == 1)
 {
 $que = $_POST['que'];
 for ($x = 1; $x <=$que; $x++) 
 {

  $rand1 = rand(0,9);
  $rand2 = rand(0,9);
  $rand3 = rand(0,9);

  $operator = array('+', '-','*','/');
  $randoperator = $operator[rand(0,3)];
   switch ($randoperator) {
   case "+":
    $finalvalue = $rand1 + $rand2 + $rand3;
    break;
  case "-":
    $finalvalue = $rand1 - $rand2 - $rand3;
    break;
     case "*":
    $finalvalue = $rand1 * $rand2 * $rand3;
    break;
   case "/":
    $finalvalue = $rand1 / $rand2 / $rand3;
    break;

   }
   echo ("This is Q(".$x."):"), $rand1 . $randoperator . $rand2 . 
   $randoperator . $rand3 . '=' . $finalvalue ,'<br /><br />';
   }
  }
 elseif(isset($_POST['digits']) == 2)
   {
 $qu = $_POST['que'];
 for ($y = 1; $y <=$qu; $y++) 
{
$rand1 = rand(10, 99);
$rand2 = rand(10, 99);
$rand3 = rand(10, 99);

$operator2 = array('+', '-','*','/');
$randoperator2 = $operator2[rand(0, 3)];
switch ($randoperator2) {
 case "+":
    $finalvalue2 = $rand1 + $rand2 + $rand3;
    break;
case "-":
    $finalvalue2 = $rand1 - $rand2 - $rand3;
    break;
 case "*":
    $finalvalue2 = $rand1 * $rand2 * $rand3;
    break;
 case "/":
    $finalvalue2 = $rand1 / $rand2 / $rand3;
    break;

 }
echo ("This is Q(".$y."):"), $rand1 . $randoperator2 . $rand2 . 
$randoperator2 . $rand3 . '=' . $finalvalue2 ,'<br /><br />';
}
}
elseif(isset($_POST['digits']) == 3)
{
$q = $_POST['que'];
for ($p = 1; $p <=$q; $p++) 
{


$rand1 = rand(100,999);
$rand2 = rand(100,999);
$rand3 = rand(100,999);

 $operator3 = array('+', '-','*','/');
 $randoperator3 = $operator3[rand(0,3)];
 switch ($randoperator3) {
 case "+":
    $finalvalue3 = $rand1 + $rand2 +$rand3;
    break;
 case "-":
    $finalvalue3 = $rand1 - $rand2 - $rand3;
    break;
 case "*":
    $finalvalue3 = $rand1 * $rand2 * $rand3;
    break;
 case "/":
    $finalvalue3 = $rand1 / $rand2 / $rand3;
    break;

  }
  echo ("This is Q(".$p."):"), $rand1 . $operator3 . $rand2 . $operator3 . 
 $rand3 . '=' . $finalvalue3 ,'<br /><br />';
 }
 }   
 else{
 //invalid;
}
?>

I am beginner in php ,how can I do the checkbox event that suggested in the answer for this code ?

  • Check this out.. [This can help you bro. ](https://stackoverflow.com/questions/9709209/html-select-only-one-checkbox-in-a-group) – j3thamz Nov 14 '18 at 07:09

1 Answers1

2

In your html, add a onclick event as below:

<input type="checkbox" id="add" name="operation" value="addition" id="add" onclick="doAddOperation()"><label>Addition</label>

In your js, implement the doAddOperation() as below. Do this for all operation.

function doAddOperation(){
    var checkbox = document.getElementById('add');
    if (checkbox.checked == true){
        add();
    }
 }
Harun Or Rashid
  • 5,589
  • 1
  • 19
  • 21
  • I am done with that Thank you so much . Actually I have another problem , According to fom hope you have got an idea about what I am trying to do there .I am trying to generate an arithmatic expression which will have random operands and operators based on input given by user from the above form I am uploading my rest of the code so that you willhave clear idea about it . – user10625430 Nov 14 '18 at 07:27
  • Hey Rashid can yoou tell me how should I do the operation by using my php code I have posted above , or tel me what changes I can do in that code to make it work. – user10625430 Nov 14 '18 at 14:36
  • Sorry brother, i am not a `php` guy. Sorry, i am not able to help you in this point :( – Harun Or Rashid Nov 14 '18 at 15:36