Click to See Complete Forum and Search --> : calculator script again


gameguy43
03-04-2005, 11:21 PM
its me again, still on that calculator script, thanks for your help thus far!

I want to make it so that the calculator does not need to reload the page every time a problem is put in. I don't know if this can be done with php alone, but I just thought that maybe if i set the form to call to a javascript function, in which i stuck the php code, this just might work. The problem then becomes posting the final output to the correct place on the page, and getting the notation for the javascript function down.


<?php

if(isset($_POST['calc'])){
$hist = $_POST['hist'];
$calc = $_POST['calc'];
eval("\$result=" . $calc . ";");
$hist = $hist . $calc. " = " . $result . "<br>";
echo $hist ;
}

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="hidden" name="hist" value="<?php echo($hist); ?>">
<input type="text" name="calc" size="50"><br>
<input type="submit" value="solve">
</form>


that's the current source. If there is a better way to do this (not what i already have, but what i am trying to accomplish), using only php, please tell me if not, any ideas on how I can make this work?

SpectreReturns
03-05-2005, 01:51 AM
Originally posted by gameguy43
its me again, still on that calculator script, thanks for your help thus far!

I want to make it so that the calculator does not need to reload the page every time a problem is put in. I don't know if this can be done with php alone, but I just thought that maybe if i set the form to call to a javascript function, in which i stuck the php code, this just might work. The problem then becomes posting the final output to the correct place on the page, and getting the notation for the javascript function down.


<?php

if(isset($_POST['calc'])){
$hist = $_POST['hist'];
$calc = $_POST['calc'];
eval("\$result=" . $calc . ";");
$hist = $hist . $calc. " = " . $result . "<br>";
echo $hist ;
}

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="hidden" name="hist" value="<?php echo($hist); ?>">
<input type="text" name="calc" size="50"><br>
<input type="submit" value="solve">
</form>


that's the current source. If there is a better way to do this (not what i already have, but what i am trying to accomplish), using only php, please tell me if not, any ideas on how I can make this work?

There is no way to change a PHP page without reloading (unless you're using sockets). Why don't you just write the whole thing in JavaScript? It would be easier..

NogDog
03-05-2005, 09:21 AM
Originally posted by SpectreReturns
There is no way to change a PHP page without reloading (unless you're using sockets). Why don't you just write the whole thing in JavaScript? It would be easier..
It would be, however, less accessible (not everyone has JavaScript available or activated on his/her browser). But this is the price you pay for using a server-side script: you have to converse with the server to run it. With a reasonably lean HTML output and reasonably efficient code on the server side, this is usually not really much of a problem.

If you use a CSS stylesheet to control the design/layout of the page, you can reduce the amount of data that gets transferred on each subsequent page request (the browser will cache the stylesheet) plus the actual HTML can be kept more streamlined.

gameguy43
03-05-2005, 02:01 PM
interesting... so i guess i can't just completely substitute php for javascript... thanks