Forum Moderators: coopster

Message Too Old, No Replies

Resources for simple math form?

example of using PHP for mathematics?

         

jbeck

12:26 am on Jan 23, 2006 (gmt 0)

10+ Year Member



Hi all,

I am very new to PHP, and I am wondering if anyone can point me towards an example of how to use a PHP form to have the user input some values, perform a mathematical operation, and then write out the answer on the same screen?

For example, the form would have 2 input fields, assigned to variable_a and variable_b respectively. I would like the user to input the integers, click a "calc" button, and have the script print out something to the effect of "the answer is" and then the sum of the values.

I am working this up towards a more complex mathematical equation, which I am sure I could get working if I could find a simple example of this somewhere.

Any help is appreciated!

niels

1:57 am on Jan 23, 2006 (gmt 0)

10+ Year Member



Basic calculations would look like this:

$variable_a = 4;
$variable_b = 2;

$answer = $variable_a + $variable_b;
(answer = 6)

$answer = $variable_a / $variable_b;
(answer = 2)

$answer = $variable_a * $variable_b;
(answer = 8)

$answer = $variable_a - $variable_b;
(answer = 2)

Let me know if you need a "full" example.

jbeck

3:00 am on Jan 23, 2006 (gmt 0)

10+ Year Member



Niels, thanks for taking the time to reply. If you could provide a "full example", that would be great. Believe it or not, I actually need more help with the rest of it than the actual variable assignment and math! :-)

jatar_k

5:01 pm on Jan 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you also might want to look at javascript.

If this is just a type of calculator and there is no need for the information to be sent back to the server then javascript might work better. Then people could do multiple calculations while never refreshing the page.

niels

5:13 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



John, sorry it toke so long, as promised a simple working example.

<?
if($_POST['Submit']!=''){
$variable_a = $_POST['variable_a'];
$variable_b = $_POST['variable_b'];

$result = $variable_a + $variable_b;
echo "<h1>$variable_a + $variable_b = $result</h1>";
}
?>

<form name="calc" method="post">
<input name="variable_a" type="text" id="variable_a">
+
<input name="variable_b" type="text" id="variable_b">
<input type="submit" name="Submit" value="Calc">
</form>

jbeck

9:41 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



Niels,

Thanks, but I can't seem to get that to run. Is that all there is to it? When I put it in an file (test.php), all that happens when I view it is I see the code in the browser window.

Told you I was new at this! If possible, could you let me know what I am doing wrong?

Thanks again,

John

JerryOdom

9:44 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



Is php installed on your server? If you have something in <?php?> tags and nothing is happening but the code being displayed then php isn't processing.

jbeck

10:05 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



Yes, PHP is installed. For example, if I use this code in file called test.php, I get back all my PHP version and config info:

<?php phpinfo()?>

All I did with the code Niels posted above is cut/paste it into a blank file called calc.php, uploaded it to my server, and viewed it through a browser. Is that what I was supposed to do?

niels

10:12 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



Strange, it works fine here.

Moosetick

10:44 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



It worked for me.

Other than having PHP installed, you need to run it through your web server so it will execute. You will use a path like 127.0.0.1/test.php. If you are just doubleclicking on the file and runing it that way it likely won't work.

Look in the address bar. Is it a web address or something like "C:\Program Files\...math.php".

sonjay

10:52 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



Even if he has php running, if he has short_open_tags turned off that example wouldn't work. Opening the php code block with <?php is always a safer bet if you don't know the server configuration.

jbeck

12:04 am on Jan 24, 2006 (gmt 0)

10+ Year Member



Yup, that did it. Thanks everyone, and Niels, thank you again for taking the time to put that example together for me.

niels

12:36 am on Jan 24, 2006 (gmt 0)

10+ Year Member



Jep my bad, i should have used <?php

jbeck, no problem i hope it helps you building that more complex scipt.