FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Math





assex
Hey, I thought that it would be good if I got some help here. Please tell me if you know a way by which I can create a script that allows users to enter any number in a textbox and when they click on a button, that number's square should appear. Help would be appreciated....
n0obie4life
Code:
<?php

if (isset($_POST['number']))
{
$number = intval($_POST['number']);
echo sqrt($number);
}
else
{
?>
Type in the number to be square rooted<br />
<input type="text" name="number" />
<input type="submit" />
<?php
}

?>


Name it a .php file. (something.php)

If it doesn't work, tell me, I just coded this in the broswer and didn't test it P
assex
Hey thanks for being so helpful 0obie, I'm giving you 30 more FRIH$. Let me try the script. I'll definitely tell you if it works.......
assex
Hey, it doesn't work!!!
n0obie4life
My bad, use this code below. I forgot the <form> tags.

Code:
<?php

if (isset($_POST['number']))
{
$number = intval($_POST['number']);
echo sqrt($number);
}
else
{
?>
Type in the number to be square rooted<br />
<form action="page.php" method="POST">
<input type="text" name="number" />
<input type="submit" />
</form>
<?php
}

?>


http://bondings-world.frihost.net/page.php (try it)
Nyizsa
Do you really need a transaction to get a number's square? You might like JavaScript better:
Code:
<html>
<head>
<title>Square.</title>
<script language="javascript">
function square()
{
  if (!isNaN(nu.value)) sq.value=nu.value*nu.value
  else
  {
    window.alert("Numbers only.");
    nu.value="";
  }
}
</script>
</head>
<body onload="javascript:nu.focus();">
<input type="text" id="nu" />
<input type="text" id="sq" />
<input type="button" value="Square" onclick="javascript:square()" />
</body>
</html>

Or, if you insist, here is a short PHP script that does the job:
Code:
<html>
<head>
<title>Square.</title>
</head>
<body onload="javascript:nu.focus();">
<form>
<input type="text" name="nu" id="nu" />
<input type="submit" value="Square" action="<?php $_SERVER['PHP_SELF']; ?>" method="get" />
</form>
<?php if (isset($_GET['nu'])) echo($_GET['nu']*$_GET['nu']); ?>
</body>
</html>

Use .html extension with the first one, and .php with the second.
I hope it helped.
assex
Hey 0obie, could you create a similar page which has two text-boxes where one can enter two numbers that shall be added, multiplied & divided? Could you also put the source code here, along with a link to it? PLEASE DO THAT FOR ME - COME ON....I GAVE YOU 50+ FRIH$!!!!!

Very Happy Very Happy please Very Happy Very Happy
Gieter
assex wrote:
Hey 0obie, could you create a similar page which has two text-boxes where one can enter two numbers that shall be added, multiplied & divided? Could you also put the source code here, along with a link to it? PLEASE DO THAT FOR ME - COME ON....I GAVE YOU 50+ FRIH$!!!!!

Very Happy Very Happy please Very Happy Very Happy


Why don't you do it yourself? The nice thing about programming is the control that you have over the computer... the only thing is that you'll have to learn it, but it isn't hard at all. You'll also don't have to wait for someone to post source-code, you just could do it yourself. Isn't that great? (yes it is!) Wink
n0obie4life
assex wrote:
Hey 0obie, could you create a similar page which has two text-boxes where one can enter two numbers that shall be added, multiplied & divided? Could you also put the source code here, along with a link to it? PLEASE DO THAT FOR ME - COME ON....I GAVE YOU 50+ FRIH$!!!!!

Very Happy Very Happy please Very Happy Very Happy


Sure, just give me a few hours or so, I'm kinda busy now. I'll post the source code & demo page in a few hours.

(and I'm sure it wasn't 50+FRIH$, I just saw lots of decimal places Razz)
n0obie4life
Code:
<?php

if (isset($_POST['mode']))
{
$number1 = intval($_POST['number1']);
$number2 = intval($_POST['number2']);
$mode = $_POST['mode'];

if ( $mode == "add" )
{
   echo $number1+$number2;
}
elseif ( $mode == "subtract" )
{
   echo $number1-$number2;
}
elseif ( $mode == "multiply" )
{
   echo $number1*$number2;
}
elseif ( $mode == "divide" )
{
   echo $number1/$number2;
}
else
{
   echo "Error";
}

}
else
{
?>
Type in the numbers to be calcuated<br />
<form action="page.php" method="POST">
<input type="text" name="number1" />
<input type="text" name="number2" /><br />
<input type="radio" name="mode" value="add">Add</input>
<input type="radio" name="mode" value="subtract">Subtract</input>
<input type="radio" name="mode" value="multiply">Multiply</input>
<input type="radio" name="mode" value="divide">Divide</input>
<input type="submit" />
</form>
<?php
}

?>


http://bondings-world.frihost.net/page.php
assex
Thanks a lot 0obie!!!!!!!! You are really helpful!!!!!
martindecorte
Trading FRIH$ for sqrt... This forum isn't a market place !

assex, you'd maybe better learn scripting yourself. If you really need this sqrt script that you can't do, then learning scripting will be very useful !
assex
Well, its okay now, coz I have got the hang of using this sort of a script.....
I've understood the basics of PHP Math....
Now, I am developing a calculator that is very advanced......I'll post here again when it is ready......
n0obie4life
Start a new topic by then Wink.

closing this.

-close-
Related topics
http://tuvanonline.com/library/index.php
PHP-Nuke platium?
PHP Write to line
Anyone good with PHP-NUKE?
PHP safety?
[Scripts] PHP Clock
math equations
counter help
PHP and cookies. Simple problem.
PHP Whos Online Code OR Page refresh..
AJAX + PHP, UPDATE mysql table nto working.
Odd PHP math...
PHP, AJAX and setInterval. Can't make a stopwatch
help me resolve this weird error in php
This topic is locked: you cannot edit posts or make replies.    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.