Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Quote:
when i do any operation it work ok like 5+6 = 11, but again if i want to add say a number 5 to the evaluated answer its showing error that means only one time i can do operation again if i want to do operation its showing error. How to fix this issue any suggestion ?


What I have tried:

<?php
// current formula in input box
if (isset($_POST['txt'])) {
    $current_txt = $_POST['txt'];
}

if (isset($_POST['select1'])) {
  $message = "1";
}
if (isset($_POST['select2'])) {
  $message = "2";
}
if (isset($_POST['select3'])) {
  $message = "3";
}
if (isset($_POST['select4'])) {
  $message = "4";
}
if (isset($_POST['select5'])) {
  $message = "5";
}
if (isset($_POST['select6'])) {
  $message = "6";
}
if (isset($_POST['select7'])) {
  $message = "7";
}
if (isset($_POST['select8'])) {
  $message = "8";
}
if (isset($_POST['select9'])) {
  $message = "9";
}
if (isset($_POST['select0'])) {
  $message = "0";
}
if (isset($_POST['select+'])) {
  $message = "+";
}
if (isset($_POST['select-'])) {
  $message = "-";
}
if (isset($_POST['select/'])) {
  $message = "/";
}
if (isset($_POST['select*'])) {
  $message = "*";
}
if (isset($_POST['clear'])) {
  $message = "clear";
}
if (isset($_POST['calculate'])) {
  $message = "calc";
}

if ($message == "calc") {
    // Calculate using eval 
    $current_txt .= " = " . eval('return ' . $current_txt . ';');
} else {
    // Append input to formula
    $current_txt .= $message;
}

if ($message == "clear") {
    $current_txt = "";
}

?>

<div style="padding-left: 200px; margin-top: 100px">
  <form  method="post">
    Enter value:  <input type="text" name="txt" value="<?php
    echo $current_txt;
?>" >
    <div style="padding-left: 105px"><br>

      <input type="submit" name="select1" value="1">
      <input type="submit" name="select2" value="2">
      <input type="submit" name="select3" value="3">
      <input type="submit" name="select4" value="4"><br><br>
      <input type="submit" name="select5" value="5">
      <input type="submit" name="select6" value="6">
      <input type="submit" name="select7" value="7">
      <input type="submit" name="select8" value="8"><br><br>
      <input type="submit" name="select9" value="9">
      <input type="submit" name="select0" value="0">
      <input type="submit" name="select+" value="+">
      <input type="submit" name="select-" value="-"><br><br>
      <input type="submit" name="select/" value="/">
      <input type="submit" name="select*" value="*">
      <input type="submit" name="clear" value="clear"> <br><br>
      <input type="submit" name="calculate" value="calculate">

    </div>

  </form>
</div>
Posted
Updated 10-Apr-18 6:34am
Comments
Sunasara Imdadhusen 10-Apr-18 5:48am    
You must convert your value into number
123456patil 10-Apr-18 7:12am    
ok thank you

1 solution

Once you get your total, what do you do with the result?

You need to put the result in some storage and have your application look for it if it does not have enough input elements (i.e., you didn't start with a new a+b)


something like this (pseudocode) for addtion:
// Assuming 'a' is the first of the pair of operators
  if(b is undefined) b = c;
  c = a + b;
  write c;


Now the above doesn't work (as written) for the first input - it's only an example showing you that if you wish to add a second number to the result AFTER an operation you need to have some way to recall and use the old answer for the new operation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900