How can I bind arithmetic operations event to checkboxes and perform operation respectively?
up vote
-1
down vote
favorite
I have checked the previous asked questions like this but for my purpose they weren't helpful.
The purpose is to perform operation on the basis of users input:
If user clicked on addition then a random sequence having only addition operation should be generated and so on.
Also user can select more than one operation at a time and accordingly output will be shown to user on basis of their choice.
The code is generating random sequence having random operands and operator with answer properly I just want to do it for selected operations which will be selected by user.
How can I do it?
Below is my code for random sequence generator, The code is working fine but for all the arithmetic operations even without selecting any checkbox after clicking on generate button.
<form action="" method="POST">
Select no.of questions:<input type="number" name="que" value="que">
<br /> <br />
Select no. of Integers:
<select name="select">
<option value="0"> Please select </option>
<option value="1"> 1 </option>
<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>
<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"
onclick="doOperation()">
<label>Addition</label>
<input type="checkbox" id="sub" name="operation" value="subtract"
onclick="doOperation()">
<label>Subtraction</label>
<input type="checkbox" id="mult" name="operation" value="multiply"
onclick="doOperation()">
<label>Multiplication</label>
<input type="checkbox" id="div" name="operation" value="divide"
onclick="doOperation()">
<label>Division</label>
<br /><br />
<br /><br />
<input type="submit" name="submit" value="Generate"><br>
<br />
</form>
Below is my php code for sequence generator, in this code I managed to do all the operations when generate button is clicked.
I also want to do it for operation that will be selected by user using checkbox. Suppose the user selected addition and multiplication or any two or any three operation then in output sequence having only those selected operations should be generate.
So how can I do that?
For clear idea you can run this code you will understand the problem.
Here is my php code
<?php
if(isset($_POST['submit'])) {
if($_POST['digits'] == 1) {
$q = $_POST['que'];
$rOperators = array('+', '-', '*', '/');
$previousInt = null; //Track the previous operand
$s = $_POST['select'];
echo "no.of questions:".$q. "<br />";
echo "no of digits: 1 <br />";
echo "no. of integers:" .$s. "<br />";
echo "Your generted sequence is below :<br /><br />";
for ($x = 1; $x<=$q; $x++)
{
$randomOperands = array();
$randomOperators = array();
// do a loop over $i n times
for ($i = 1; $i <=$s; $i++)
{
// assign random operand to array slot
$nextInt = rand(0, 9);
$randomOperands = $nextInt;
if($i < $s) {
//No operator after last opearnd
if($previousInt === 0) {
//Make sure to avoid a potenial division-by-zero
$randomOperators = $rOperators[rand(0, 2)];
} else {
$randomOperators = $rOperators[rand(0, 3)];
}
}
$previousInt = $nextInt;
}
// print array values:
$exp = '';
foreach($randomOperands as $key=>$value1)
{
$exp .= $value1 . " ";
if(isset($randomOperators[$key])) {
$exp .= $randomOperators[$key] ." ";
}
}
$res = eval("return ($exp);");
//print your expression
echo ("This is Q(".$x."):"), $exp."=".$res."<br />";
}
}
?>
I know we have to use JavaScript here for the operations but I am a beginner in this so how can I do it?
javascript php html
add a comment |
up vote
-1
down vote
favorite
I have checked the previous asked questions like this but for my purpose they weren't helpful.
The purpose is to perform operation on the basis of users input:
If user clicked on addition then a random sequence having only addition operation should be generated and so on.
Also user can select more than one operation at a time and accordingly output will be shown to user on basis of their choice.
The code is generating random sequence having random operands and operator with answer properly I just want to do it for selected operations which will be selected by user.
How can I do it?
Below is my code for random sequence generator, The code is working fine but for all the arithmetic operations even without selecting any checkbox after clicking on generate button.
<form action="" method="POST">
Select no.of questions:<input type="number" name="que" value="que">
<br /> <br />
Select no. of Integers:
<select name="select">
<option value="0"> Please select </option>
<option value="1"> 1 </option>
<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>
<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"
onclick="doOperation()">
<label>Addition</label>
<input type="checkbox" id="sub" name="operation" value="subtract"
onclick="doOperation()">
<label>Subtraction</label>
<input type="checkbox" id="mult" name="operation" value="multiply"
onclick="doOperation()">
<label>Multiplication</label>
<input type="checkbox" id="div" name="operation" value="divide"
onclick="doOperation()">
<label>Division</label>
<br /><br />
<br /><br />
<input type="submit" name="submit" value="Generate"><br>
<br />
</form>
Below is my php code for sequence generator, in this code I managed to do all the operations when generate button is clicked.
I also want to do it for operation that will be selected by user using checkbox. Suppose the user selected addition and multiplication or any two or any three operation then in output sequence having only those selected operations should be generate.
So how can I do that?
For clear idea you can run this code you will understand the problem.
Here is my php code
<?php
if(isset($_POST['submit'])) {
if($_POST['digits'] == 1) {
$q = $_POST['que'];
$rOperators = array('+', '-', '*', '/');
$previousInt = null; //Track the previous operand
$s = $_POST['select'];
echo "no.of questions:".$q. "<br />";
echo "no of digits: 1 <br />";
echo "no. of integers:" .$s. "<br />";
echo "Your generted sequence is below :<br /><br />";
for ($x = 1; $x<=$q; $x++)
{
$randomOperands = array();
$randomOperators = array();
// do a loop over $i n times
for ($i = 1; $i <=$s; $i++)
{
// assign random operand to array slot
$nextInt = rand(0, 9);
$randomOperands = $nextInt;
if($i < $s) {
//No operator after last opearnd
if($previousInt === 0) {
//Make sure to avoid a potenial division-by-zero
$randomOperators = $rOperators[rand(0, 2)];
} else {
$randomOperators = $rOperators[rand(0, 3)];
}
}
$previousInt = $nextInt;
}
// print array values:
$exp = '';
foreach($randomOperands as $key=>$value1)
{
$exp .= $value1 . " ";
if(isset($randomOperators[$key])) {
$exp .= $randomOperators[$key] ." ";
}
}
$res = eval("return ($exp);");
//print your expression
echo ("This is Q(".$x."):"), $exp."=".$res."<br />";
}
}
?>
I know we have to use JavaScript here for the operations but I am a beginner in this so how can I do it?
javascript php html
hey paul , I ahve gone through the similar questions asked before but they are not helping for my purpose so it will be reat if you take a look at my code once and sugesst how can I do it?
– user10625430
Nov 16 at 9:18
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have checked the previous asked questions like this but for my purpose they weren't helpful.
The purpose is to perform operation on the basis of users input:
If user clicked on addition then a random sequence having only addition operation should be generated and so on.
Also user can select more than one operation at a time and accordingly output will be shown to user on basis of their choice.
The code is generating random sequence having random operands and operator with answer properly I just want to do it for selected operations which will be selected by user.
How can I do it?
Below is my code for random sequence generator, The code is working fine but for all the arithmetic operations even without selecting any checkbox after clicking on generate button.
<form action="" method="POST">
Select no.of questions:<input type="number" name="que" value="que">
<br /> <br />
Select no. of Integers:
<select name="select">
<option value="0"> Please select </option>
<option value="1"> 1 </option>
<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>
<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"
onclick="doOperation()">
<label>Addition</label>
<input type="checkbox" id="sub" name="operation" value="subtract"
onclick="doOperation()">
<label>Subtraction</label>
<input type="checkbox" id="mult" name="operation" value="multiply"
onclick="doOperation()">
<label>Multiplication</label>
<input type="checkbox" id="div" name="operation" value="divide"
onclick="doOperation()">
<label>Division</label>
<br /><br />
<br /><br />
<input type="submit" name="submit" value="Generate"><br>
<br />
</form>
Below is my php code for sequence generator, in this code I managed to do all the operations when generate button is clicked.
I also want to do it for operation that will be selected by user using checkbox. Suppose the user selected addition and multiplication or any two or any three operation then in output sequence having only those selected operations should be generate.
So how can I do that?
For clear idea you can run this code you will understand the problem.
Here is my php code
<?php
if(isset($_POST['submit'])) {
if($_POST['digits'] == 1) {
$q = $_POST['que'];
$rOperators = array('+', '-', '*', '/');
$previousInt = null; //Track the previous operand
$s = $_POST['select'];
echo "no.of questions:".$q. "<br />";
echo "no of digits: 1 <br />";
echo "no. of integers:" .$s. "<br />";
echo "Your generted sequence is below :<br /><br />";
for ($x = 1; $x<=$q; $x++)
{
$randomOperands = array();
$randomOperators = array();
// do a loop over $i n times
for ($i = 1; $i <=$s; $i++)
{
// assign random operand to array slot
$nextInt = rand(0, 9);
$randomOperands = $nextInt;
if($i < $s) {
//No operator after last opearnd
if($previousInt === 0) {
//Make sure to avoid a potenial division-by-zero
$randomOperators = $rOperators[rand(0, 2)];
} else {
$randomOperators = $rOperators[rand(0, 3)];
}
}
$previousInt = $nextInt;
}
// print array values:
$exp = '';
foreach($randomOperands as $key=>$value1)
{
$exp .= $value1 . " ";
if(isset($randomOperators[$key])) {
$exp .= $randomOperators[$key] ." ";
}
}
$res = eval("return ($exp);");
//print your expression
echo ("This is Q(".$x."):"), $exp."=".$res."<br />";
}
}
?>
I know we have to use JavaScript here for the operations but I am a beginner in this so how can I do it?
javascript php html
I have checked the previous asked questions like this but for my purpose they weren't helpful.
The purpose is to perform operation on the basis of users input:
If user clicked on addition then a random sequence having only addition operation should be generated and so on.
Also user can select more than one operation at a time and accordingly output will be shown to user on basis of their choice.
The code is generating random sequence having random operands and operator with answer properly I just want to do it for selected operations which will be selected by user.
How can I do it?
Below is my code for random sequence generator, The code is working fine but for all the arithmetic operations even without selecting any checkbox after clicking on generate button.
<form action="" method="POST">
Select no.of questions:<input type="number" name="que" value="que">
<br /> <br />
Select no. of Integers:
<select name="select">
<option value="0"> Please select </option>
<option value="1"> 1 </option>
<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>
<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"
onclick="doOperation()">
<label>Addition</label>
<input type="checkbox" id="sub" name="operation" value="subtract"
onclick="doOperation()">
<label>Subtraction</label>
<input type="checkbox" id="mult" name="operation" value="multiply"
onclick="doOperation()">
<label>Multiplication</label>
<input type="checkbox" id="div" name="operation" value="divide"
onclick="doOperation()">
<label>Division</label>
<br /><br />
<br /><br />
<input type="submit" name="submit" value="Generate"><br>
<br />
</form>
Below is my php code for sequence generator, in this code I managed to do all the operations when generate button is clicked.
I also want to do it for operation that will be selected by user using checkbox. Suppose the user selected addition and multiplication or any two or any three operation then in output sequence having only those selected operations should be generate.
So how can I do that?
For clear idea you can run this code you will understand the problem.
Here is my php code
<?php
if(isset($_POST['submit'])) {
if($_POST['digits'] == 1) {
$q = $_POST['que'];
$rOperators = array('+', '-', '*', '/');
$previousInt = null; //Track the previous operand
$s = $_POST['select'];
echo "no.of questions:".$q. "<br />";
echo "no of digits: 1 <br />";
echo "no. of integers:" .$s. "<br />";
echo "Your generted sequence is below :<br /><br />";
for ($x = 1; $x<=$q; $x++)
{
$randomOperands = array();
$randomOperators = array();
// do a loop over $i n times
for ($i = 1; $i <=$s; $i++)
{
// assign random operand to array slot
$nextInt = rand(0, 9);
$randomOperands = $nextInt;
if($i < $s) {
//No operator after last opearnd
if($previousInt === 0) {
//Make sure to avoid a potenial division-by-zero
$randomOperators = $rOperators[rand(0, 2)];
} else {
$randomOperators = $rOperators[rand(0, 3)];
}
}
$previousInt = $nextInt;
}
// print array values:
$exp = '';
foreach($randomOperands as $key=>$value1)
{
$exp .= $value1 . " ";
if(isset($randomOperators[$key])) {
$exp .= $randomOperators[$key] ." ";
}
}
$res = eval("return ($exp);");
//print your expression
echo ("This is Q(".$x."):"), $exp."=".$res."<br />";
}
}
?>
I know we have to use JavaScript here for the operations but I am a beginner in this so how can I do it?
javascript php html
javascript php html
edited Nov 19 at 15:31
treyBake
2,8063831
2,8063831
asked Nov 16 at 5:42
user10625430
397
397
hey paul , I ahve gone through the similar questions asked before but they are not helping for my purpose so it will be reat if you take a look at my code once and sugesst how can I do it?
– user10625430
Nov 16 at 9:18
add a comment |
hey paul , I ahve gone through the similar questions asked before but they are not helping for my purpose so it will be reat if you take a look at my code once and sugesst how can I do it?
– user10625430
Nov 16 at 9:18
hey paul , I ahve gone through the similar questions asked before but they are not helping for my purpose so it will be reat if you take a look at my code once and sugesst how can I do it?
– user10625430
Nov 16 at 9:18
hey paul , I ahve gone through the similar questions asked before but they are not helping for my purpose so it will be reat if you take a look at my code once and sugesst how can I do it?
– user10625430
Nov 16 at 9:18
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332083%2fhow-can-i-bind-arithmetic-operations-event-to-checkboxes-and-perform-operation-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
hey paul , I ahve gone through the similar questions asked before but they are not helping for my purpose so it will be reat if you take a look at my code once and sugesst how can I do it?
– user10625430
Nov 16 at 9:18