473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Calculation script to display on same page

28
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=((($_REQUEST["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 4241
BigDaddyLH
1,216 Expert 1GB
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 Expert 1GB
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=((($_REQUEST["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(variable) to round the number.

This is another example.
Mar 6 '08 #3
mrking
28
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 Expert 1GB
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="createCost()" value="Calculate Cost" />

<br /><br />
<!-- //do not repeat id
<input type="text" id="h1" size="50">
-->
</form>[/html]
Mar 6 '08 #5
mrking
28
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 Expert 1GB
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
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 Expert 1GB
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
I have one more afterthought addition I would like to add.

I would like to give the option of "inches" or "Centimeters: 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
hsriat
1,654 Expert 1GB
I have one more afterthought addition I would like to add.

I would like to give the option of "inches" or "Centimeters: 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?
Yeah that can be done.
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.
No, that's not possible. Only you can save the javascript in an external file. But that too is not hidden. Only you won't see the code on view source.
Mar 7 '08 #11
mrking
28
Thanks, the external file worked great. :)

I can get the radio buttons in the form no problem and have the new calculation for the centimeter option but how do i tell which one to use.

Expand|Select|Wrap|Line Numbers
  1. <input type="radio" name="inches" onclick="check(this.value)" value="units">Inches<br />
  2. <input type="radio" name="cm" onclick="check(this.value)" value="units">Centimeters<br />
  3.  
Are there IF statements in JS for this? Do I need to loop it like in PHP?

It's like I am learning Greek here. :)

I am sure I need to use something like this:
Expand|Select|Wrap|Line Numbers
  1.   document.getElementById("answer").value=units;
  2.  
Mar 7 '08 #12

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

Similar topics

5
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...
21
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...
3
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...
5
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...
1
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...
1
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-...
8
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...
3
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.