473,671 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculation script to display on same page

28 New Member
Hi,

First time in the Java section here.

I have an HTML form and a PHP script to do a simple calculation but it has to take me to a different page for the results.

The calculator is really simple. The user enters just height and width and the calculator applies the formula, then display the answer to the nearest 10th.

[PHP]
<?

$cost=((($_REQU EST["height"]*$_REQUEST["width"])+160)*1.65);

echo "<br />The cost of the commission will be $" .round($cost, -1);

?>
[/PHP]

I know nothing of Java script. Can someone point me in the right direction for a script that will do this calculation and display it on the same page as the user inputs the data?

Thank you!!

Michael
Mar 6 '08 #1
11 4272
BigDaddyLH
1,216 Recognized Expert Top Contributor
I guess the first thing to learn about the scripting language ECMAScript (sometimes called JavaScript) is that it is unrelated to the programming language Java. Moved to this forum...

--JAVA MODERATOR
Mar 6 '08 #2
hsriat
1,654 Recognized Expert Top Contributor
Hi,

First time in the Java section here.

I have an HTML form and a PHP script to do a simple calculation but it has to take me to a different page for the results.

The calculator is really simple. The user enters just height and width and the calculator applies the formula, then display the answer to the nearest 10th.

[PHP]
<?

$cost=((($_REQU EST["height"]*$_REQUEST["width"])+160)*1.65);

echo "<br />The cost of the commission will be $" .round($cost, -1);

?>
[/PHP]

I know nothing of Java script. Can someone point me in the right direction for a script that will do this calculation and display it on the same page as the user inputs the data?

Thank you!!

Michael
Yeah, that can be done easily. Have a look at
how to set and return value of an input element.
Use Math.round(vari able) to round the number.

This is another example.
Mar 6 '08 #3
mrking
28 New Member
Thanks for the info...

This is my start, but there is a lot wrong with it of course.

I have no idea how to start a calculator function so far. Still digging. help would be a godsend. :)

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4.  
  5.  
  6. function createCost()
  7. {
  8. alert(document.getElementById("h1").value);
  9. alert(document.getElementById("w1").value);
  10. }
  11. </script>
  12. </head>
  13. <body>
  14.  
  15. <form>
  16. Height: <input type="text" size="4" id="h1" value="" />
  17. Width: <input type="text" size="4" id="w2" value="" />
  18. <input type="button" id="button1" onclick="createCost()" value="Calculate Cost" />
  19.  
  20. <br /><br />
  21. <input type="text" id="h1" size="50">
  22. </form>
  23.  
  24.  
  25.  
  26. </body>
  27. </html>
  28.  
  29.  
Mar 6 '08 #4
hsriat
1,654 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. function createCost() {
  2.     //alert() is to give an alert message, save the values as variables instead.
  3.     var h = parseInt(document.getElementById("h1").value);
  4.     var w = parseInt(document.getElementById("w1").value);
  5.     //parseInt() is used to use the values later on as integers and not strings.
  6.     //coz in js, + is mainly used to join strings like . in PHP
  7.  
  8.     //do your calculations like u do in PHP
  9.     var c = h+w;
  10.  
  11.     //set the value in the input field
  12.     document.getElementById("c").value = c;
  13. }
[html]<form>
Height: <input type="text" size="4" id="h1" value="" />
Width: <input type="text" size="4" id="w2" value="" />
<!-- input to display cost-->
Cost: <input type="text" size="4" id="c" value="" readonly />
<input type="button" id="button1" onclick="create Cost()" value="Calculat e Cost" />

<br /><br />
<!-- //do not repeat id
<input type="text" id="h1" size="50">
-->
</form>[/html]
Mar 6 '08 #5
mrking
28 New Member
Thanks! I would have never got to that. Once I see it written it makes sense.

I use math.round and it will round up to the nearest integer, but I am looking to round up to the nearest 10th.

So, 491=500; 348=350... etc.

Is there an easy way to do this?

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head>
  4. <script type="text/javascript">
  5.  
  6. function createCost() {
  7. //alert() is to give an alert message, save the values as variables instead.
  8.  
  9. var h = document.getElementById("h1").value;
  10. var w = document.getElementById("w1").value;
  11.  
  12. //do your calculations like u do in PHP
  13.  
  14. var c = (((h*w)+160)*1.65);
  15. c = Math.round(c);
  16.  
  17.  
  18.        //set the value in the input field
  19.  
  20. document.getElementById("c").value = c;
  21.       }
  22. </script>
  23. </head>
  24.  
  25. <body>
  26.       <form>
  27.  
  28.       Height: <input type="text" size="4" id="h1" value="" />
  29.  
  30.       Width: <input type="text" size="4" id="w1" value="" />
  31.  
  32.       <!-- input to display cost-->
  33.  
  34.     Cost: <input type="text" size="4" id="c" value="" readonly />
  35.  
  36.       <input type="button" id="button1" onclick="createCost()" value="Calculate Cost" />
  37.  
  38.  
  39.       <br /><br />
  40.  
  41.       <!-- //do not repeat id
  42.  
  43.       <input type="text" id="h1" size="50">
  44.  
  45.  
  46.       -->
  47.  
  48.       </form>
  49. </body>
  50. </html>
  51.  
  52.  
Mar 6 '08 #6
hsriat
1,654 Recognized Expert Top Contributor
I guess may be there is nothing like round($x, 2) in JS.

So you can do one thing. Divide your number with 10, do Math.round() and multiply by 10 again.
May be that can work.

Like:
Expand|Select|Wrap|Line Numbers
  1. c = (Math.round(c/10))*10;
Mar 6 '08 #7
mrking
28 New Member
I guess may be there is nothing like round($x, 2) in JS.

So you can do one thing. Divide your number with 10, do Math.round() and multiply by 10 again.
May be that can work.

Like:
Expand|Select|Wrap|Line Numbers
  1. c = (Math.round(c/10))*10;
HA, BINGO.

You're the best!!!

Thank you for all your help and code writing. You saved me days working this out.

Cheers,

Michael
Mar 6 '08 #8
hsriat
1,654 Recognized Expert Top Contributor
HA, BINGO.

You're the best!!!

Thank you for all your help and code writing. You saved me days working this out.

Cheers,

Michael
Too glad to know you got it working. Post here for any other problem.


Harpreet
Mar 6 '08 #9
mrking
28 New Member
I have one more afterthought addition I would like to add.

I would like to give the option of "inches" or "Centimeter s: to provide the values they are entering

I think it can be done with radio boxes and depending on the radio box ticked it will perform a different calculation. Correct?

Oh, and is there anyway to hide the formula from the general public. If you decide to look at the page source you will be able to see it. I'd like to not show that part if possible.
Mar 6 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
7875
by: TG | last post by:
Dear PHP Group, How can I in PHP, based on a result, move the cursor to a certain location on the page? I want the same result as Anchor tags in HTML. I want to go to a certain section of the same page based on a return result in PHP. What this is for is an order forms page - when a user fills out a section incorrectly, I want the page to display at that location in the form after the user presses a submit button. How do I do this?
21
3884
by: deko | last post by:
Is it possible to call a php script from an html page? I have a TestPage.php that I want to convert to TestPage.html - but still call a php script from it. This is how my TestPage.php looks now: <?php setcookie("mycookie",time(),3600,"/"); ?> <html>
3
1552
by: charlie fortune | last post by:
I have a simple html form and a bit of PHP in the same page, and I want the script to get the entry from the form, and write it to a file. I dont think I'm allowed to use POST on my webspace. Here is my code ; <p>&nbsp; <form method="GET"> Type in a groovy word and press enter<input type="text" name="groovyword"> <br>
5
1769
by: shafe25 | last post by:
well, when user inputs the data, i should be able to fetch the data from the database and do some numerical computaions and display the result on the same page before i submit the data to the php script, the php script takes all the posted data and dump it into the database, how to achieve this me using PHP 5.0/MySQL 4.0
1
1563
by: mela | last post by:
it seems quite stupid but i am building up my web site and i am stocked with this problem: i download this photoslider script and wanted to have two on the same page but they are interfering one each other.. now: i read that to avoid this i should change the names of the variables and those of the functions on the second script , but i do not know where to start from! can anybody explain me in a simple way (i do not know java at all!) what i...
1
3579
Fary4u
by: Fary4u | last post by:
Hi Guys i'm trying to upload a file, i've tried 3 different methods but still not work out i don't know how to over come this problem hidden file value, multiple form or popup uploading. 1- <SCRIPT TYPE="text/javascript"> function popupform(myform, windowname)
8
1662
by: mrking | last post by:
I am looking to make a script that does a calculation for me. The user is to enter two variables, height (H) and width (W), click "calculate" and the value will appear, hopefully on the same page. The formula will be (((H*W)+150)*1.65) (pretty easy) However, I would like it to round up to the nearest 10th. So if the value comes out to 411 I would like it to display 420. If the solution is 579 it will round up to 580. Sounds easy but...
3
3025
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
I have a HTML page, the data in the text field on submit button goes to a php script. And after calculations the result is needed to display in the same HTML page.(In the same text field and these textfields are generated on loading of the page). How i can achieve that?
4
1904
by: FritzGamaliel | last post by:
I have searched in google about my problem. But I don't get the best solution for my need. My Problem is: I create a form with TextBox Name, TextBox Message, Submit button, and it use POST method. I want form'process done in the same page. After the user input data to TextBox Name and TextBox Message and pressed submit button,it send data well. If the user input data to TextBox Message with word' length more than 100 words, it will...
1
8605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8677
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5704
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.