Page 1 of 1

EQUATION IN PHP PLZ HELP...

Posted: Sat Oct 30, 2010 6:19 pm
by amirbwb
hi, my point is to make a programme with php, so here we go:
you put an equation in ONE feild and it will solve it to you
here is a simple example:
<?php
$eq=$_POST['eq'];
?>

<form name="equation" method="POST" action="index.php">
<input type="text" name="eq" id="eq" value="<?php echo $eq;?>" >
<input type="submit" name="submit" value="SOLVE" >
</form>


<?php echo $eq;?> // if I put this line it will show me(lets say that i entered in the feild "1+2") :-----------------> it will show me "1+2", not "3"(the result)

NB : I don't want to use 2 feilds with 2 variables $a=$_POST['a']; $b=$_POST['b']; $result=$a+$b because the "+" can be chaged "/" "*" "-"

/////////////////////////////////////
another example:

[2+1__________________] [submit]

Result:
2+1

//////////////////////////////// I WANT THE RESULT TO BE "3"

THANK YOU

Re: EQUATION IN PHP PLZ HELP...

Posted: Sat Oct 30, 2010 9:59 pm
by yacahuma
yoiu could use this but not in a production environment

Code: Select all

  <form action="test2.php" method="post">
  <input type="text" name="eq" />
  <input type="submit" name="submit" />
  </form>
  
  <?
    if (isset($_POST['submit']))
    {
       eval("\$res = {$_POST['eq']};");    
       echo $_POST['eq']  . ' = ' . $res;
    }
?>

Re: EQUATION IN PHP PLZ HELP...

Posted: Sat Oct 30, 2010 10:06 pm
by califdon
The trouble with answering questions like this is that the OP is apparently trying to learn about PHP programming, but they are asking questions that bear little relation to anything useful that they are likely to ever need to do. As a former teacher, it is my opinion that this is a practice that does NOT lead to learning anything useful. If I understand the OP's question, it has little value in the real world of PHP applications and is thus counterproductive for the OP. If the OP has some other goal in mind, they would be well advised to explain what they're really trying to do. Otherwise it is just a waste of time for all of us, including the OP.

Re: EQUATION IN PHP PLZ HELP...

Posted: Sun Oct 31, 2010 9:53 am
by Placebo
basically its a calculator... surely you best off having three field in your form, you can find them on google easily enough just have put in search simple php calculator