/    Sign up×
Community /Pin to ProfileBookmark

HELP — Please — Converting cents to dollars

I’m suppose to have a code that has a HTML front end with a text box that will allow me to enter in a amount in Cents. Then have a php backend that will conver the cents into dollars. I have my conversion of cents / 100 i’m not sure how to read the infor from my html to the PHP script portion

Could someone please help explain what I need to do hear?? I’m a little lost at this point

Thanks,
any information would be much appreciated

to post a comment
PHP

22 Comments(s)

Copy linkTweet thisAlerts:
@duff2481authorNov 07.2006 — Okay, I have the front end made up -- now for the hard part or so I think. What should I do now that i have the front started? I know i would need to have a conversion of cents / 100 to get the dollars and cents

here is my script so far


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>

<html>

<head>

<title> Welcome to conversion of cents to dollars</title>

</head>

<body>

<center>

Enter a number then click the button: <input type=text name=input size=10>

<input type=button value="Convert" onclick= >


</center>

</body>

</html>
Copy linkTweet thisAlerts:
@The_Little_GuyNov 07.2006 — [code=php]<?
$dollars = $_POST['input'] / 100;

echo $dollars
?>[/code]
Copy linkTweet thisAlerts:
@duff2481authorNov 07.2006 — okay, do I put this in my PHP code?? I have to have 2 different documents for this. One is the HTML file and the other is the PHP file.

I'm assuming this goes in the PHP file but how do I know where to get my cents from my HTML file?? I'm not sure how to use the GET function
Copy linkTweet thisAlerts:
@The_Little_GuyNov 07.2006 — I edited your html so it would work

[B]File:[/B] index.html
[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>
<html>
<head>
<title> Welcome to conversion of cents to dollars</title>

</head>

<body>

<center>
<form action="calculate.php" method="post">
Enter a number then click the button: <input type="text" name="input" size="10">
<input type="submit" value="Convert">
</form>

</center>
</body>

</html>[/code]


[B]File:[/B] calculate.php
[code=php]<?
$dollars = $_POST['input'] / 100;

echo $dollars
?> [/code]
Copy linkTweet thisAlerts:
@carlhNov 07.2006 — you'll want to put your inputs in the html file in a form, so you can submit it and php can either get it from a GET or POST method, depending how you make the form.

edit:whoops, a little late on my part
Copy linkTweet thisAlerts:
@duff2481authorNov 07.2006 — Alright, one more question. I have both my HTML file and my PHP file and I have my $_Post with the correct name, however when I use my form and click the submit button, it sends me to a cannot find page.

Is there something that I am missing??

Appreciate all your help
Copy linkTweet thisAlerts:
@The_Little_GuyNov 07.2006 — action="calculate.php" needs to be the path to the file
Copy linkTweet thisAlerts:
@duff2481authorNov 07.2006 — Okay, do I need to put in a method or no, since it is receiving the information?
Copy linkTweet thisAlerts:
@The_Little_GuyNov 07.2006 — you need the method Because I have it set as post, if you leave the method out, the default is get.

You then would have to change [B]$_POST['input'][/B] to [B]$_GET['input'][/B]

$_POST

and

$_
GET

must be uppercase inorder to work.
Copy linkTweet thisAlerts:
@duff2481authorNov 07.2006 — I feel really dumb, but curious on one more thing. Do I need to have my form that is in my HTML code also in the PHP?? Here is what I have

PHP CODE :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org//TR/xhtml1/DTD/xhtml1-transitional.dtd">


<head>

<title>Project 4-5 - Cents to Dollar Conversion</title>

</head>

<body>

<form action = "calculate.php">

<?php


$dollars = $_POST['input'] / 100;

echo $dollars


?>


</body>

</html>


HTML Code _-


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>


<html>

<head>

<title> Welcome to conversion of cents to dollars</title>

</head>

<body>


<form action="calculate.php" method="post">

<center>

Enter a number then click the button: <input type="text" name="input" size="10">

<input type="submit" value="Convert">
</center>

</form>







</body>





</html>
Copy linkTweet thisAlerts:
@The_Little_GuyNov 07.2006 — That looks good all you need to do is remove this line from the PHP CODE:

<form action = "calculate.php">
Copy linkTweet thisAlerts:
@duff2481authorNov 07.2006 — Alright, i changed the naming convention of my forms because I have to.

here is what I have and they are in the same folder but don't work.

HTML File



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"

"http://www.w3.org//TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html>

<head>

<title> Welcome to conversion of cents to dollars</title>

</head>

<body>

<form action="m5donovanCP4-5.php" method="post">

<center>

Enter a number then click the button: <input type="text" name="input" size="10">

<input type="submit" value="Convert">
</center>

</form>

</body>

</html>


_____________________________________________________________

PHP Code -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org//TR/xhtml1/DTD/xhtml1-transitional.dtd">


<head>

<title>Project 4-5 - Cents to Dollar Conversion</title>

</head>

<body>


<?php

$dollars = $_POST['input'] / 100;

print $dollars


?>

</body>

</html>
Copy linkTweet thisAlerts:
@duff2481authorNov 07.2006 — Last update -- my file seems to read it but does not ouput anything. It just leaves it as a blank webpage after I hit the convert button. No error message, not time outs just completely blank --

Its as if it has the information but doesn't spit it back out to me.
Copy linkTweet thisAlerts:
@The_Little_GuyNov 07.2006 — you need a semi-colon after: print $dollars

print $dollars;

if you post the code, could you please use the [ HTML][/code],[ CODE][/CODE], or [ PHP][/code] in your code(don't add the space in the first [code=html],[CODE], or [code=php])
Copy linkTweet thisAlerts:
@duff2481authorNov 07.2006 — I'm not sure what i'm doing wrong. I know it is looking at my PHP code because it gives me back a string that I have put in but won't output the number. Its almost like it doesn't approve of the calculation.


[code=html]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org//TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html>
<head>
<title> Welcome to conversion of cents to dollars</title>

</head>

<body>


<form action="m5donovanCP4-5.php" method="post">
<center>
Enter a number then click the button: <input type="text" name="input" size="10">

<input type="submit" value="Convert">
</center>
</form>

</body>
</html>
[/code]


[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org//TR/xhtml1/DTD/xhtml1-transitional.dtd">


<head>

<title>Project 4-5 - Cents to Dollar Conversion</title>

</head>

<body>

<center>Your output is:
<?php

$dollars = $_POST['input'] / 100;

print $dollars;


?>
</center>

</body>

</html>
[/code]



I think I did the tags correctly for the code -- sorry about that, nobody has ever taught me that
Copy linkTweet thisAlerts:
@The_Little_GuyNov 07.2006 — [code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org//TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title> Welcome to conversion of cents to dollars</title>
</head>
<body>
<center>
<form action="m5donovanCP4-5.php" method="post"> <center> Enter a number then click the button:
<input type="text" name="input" size="10"> <input type="submit" value="Convert">
</form>
</center>
</body>
</html>[/code]


[code=php]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org//TR/xhtml1/DTD/xhtml1-transitional.dtd">


<head>

<title>Project 4-5 - Cents to Dollar Conversion</title>

</head>

<body>

<center>Your output is:
<?php
$dollars = $_POST['input'] / 100;

print $dollars;
?>
</center>

</body>

</html>[/code]


Example: http://www.d-top.org/webdeveloper/centstodollars.php
Copy linkTweet thisAlerts:
@duff2481authorNov 07.2006 — I don't know what the issue is. When I use your link it works fine, when I use my setup, I get everything except for the amount to show up after the calculation.


Thank you for all of your help!
Copy linkTweet thisAlerts:
@pcthugNov 07.2006 — Try:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org//TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html>
<head>
<title> Welcome to conversion of cents to dollars</title>

</head>

<body>
<form action="m5donovanCP4-5.php" method="post">
<center>
Enter a number then click the button: <input type="text" name="input" size="10">

<input type="submit" value="Convert">
</center>
</form>
</body>
</html>

_____________________________________________________________

PHP Code -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org//TR/xhtml1/DTD/xhtml1-transitional.dtd">


<head>

<title>Project 4-5 - Cents to Dollar Conversion</title>

</head>

<body>


<?php

$dollars = sprintf('%01.2f', ((int)$_POST['input'] / 100));

print $dollars;


?>
</body>

</html>[/code]
Copy linkTweet thisAlerts:
@duff2481authorNov 07.2006 — our version works, just doesn't seem to working on my machine. He has the same thing on his end and I use it and it is just fine.

Couple of things:

what does sprintf(%01.2f, mean?

I know the other is the integer of your input value divided by 100


$dollars = sprintf('%01.2f', ((int)$_POST['input'] / 100))??
Copy linkTweet thisAlerts:
@pcthugNov 07.2006 — Formats the string to two decimal places.
Copy linkTweet thisAlerts:
@NogDogNov 07.2006 — You might also want to look at the [url=http://www.php.net/number_format]number_format() function[/url], for instance:
[code=php]
$cents = 1234567890;
echo '$'.number_format($cents / 100, 2);
[/code]
Copy linkTweet thisAlerts:
@intriviousNov 08.2006 — Is this some sort of school assignment?

Oh well, at least you are trying.
×

Success!

Help @duff2481 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...