Dynamically changing text box, based on a form input? |
Author: |
Message: |
Jimbo
Veteran Member
Posts: 1650 Reputation: 18
32 / /
Joined: Jul 2006
|
O.P. Dynamically changing text box, based on a form input?
Hey,
How would I be able to retrieve the amount entered into a form, eg. $5.00, and then divide that number by 0.05, and the finally rounding it to the nearest whole number, and display the result?
Maybe this will help:
This is the form I am trying to get the amount from.
HTML code:
<form name="MyForm" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<fieldset>
<label for="amount">$ </label>
<input id="amount" name="amount" value="5.00" />
<label for="custom">Steam ID: </label>
<input id="custom" name="custom" value="<?php echo strstr(curPageURL(), 'STEAM_');?>" />
<input name="business" type="hidden" value="******************" />
<input name="cmd" type="hidden" value="_xclick" />
<input name="currency_code" type="hidden" value="USD" />
<input name="item_name" type="hidden" value="vip" />
<input name="no_note" type="hidden" value="1" />
<input name="no_shipping" type="hidden" value="1" />
<input src="http://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" type="image" alt="Click here to donate">
</fieldset>
</form>
I am trying to get the amount entered by the user, which as you can see above, defaults to $5.
Many thanks.
|
|
02-07-2010 09:37 PM |
|
|
Chrissy
Senior Member
Posts: 850 Reputation: 5
29 / /
Joined: Nov 2009
|
RE: Dynamically changing text box, based on a form input?
Using PHP?
|
|
02-07-2010 10:07 PM |
|
|
stoshrocket
Senior Member
formerly methos
Posts: 748 Reputation: 31
34 / /
Joined: Aug 2005
|
RE: Dynamically changing text box, based on a form input?
PHP code:
$am = $_POST['amount'];
$ret = ceil($am/0.05);
EDIT:
NB: ceil rounds up to nearest integer value, which I assumed from your post. However, if you needed to round up such that you end up to the nearest pence/cent, you'd have to use round() and an appropriate precision value, along with adding 0.005 to ensure you end up with the correct rounded up value...
PHP code:
$am = $_POST['amount'];
$ret = round(($am/0.05)+0.005, 2);
EDIT 2:
Thinking about it, dividing an integer with no more than 2 dp by 0.05 (effectively multiplying by 20) will mean there will be at most 1dp, so that last edit can be ignored, but kept for reference
This post was edited on 02-07-2010 at 10:26 PM by stoshrocket.
|
|
02-07-2010 10:14 PM |
|
|
Jimbo
Veteran Member
Posts: 1650 Reputation: 18
32 / /
Joined: Jul 2006
|
O.P. RE: Dynamically changing text box, based on a form input?
quote: Originally posted by krissy-afc
Using PHP?
Using anything really, PHP, Java, I don't mind.
quote: Originally posted by stoshrocket
PHP code:
$am = $_POST['amount'];
$ret = ceil($am/0.05);
EDIT:
NB: ceil rounds up to nearest integer value, which I assumed from your post. However, if you needed to round up such that you end up to the nearest pence/cent, you'd have to use round() and an appropriate precision value, along with adding 0.005 to ensure you end up with the correct rounded up value...
PHP code:
$am = $_POST['amount'];
$ret = round(($am/0.05)+0.005, 2);
EDIT 2:
Thinking about it, dividing an integer with no more than 2 dp by 0.05 (effectively multiplying by 20) will mean there will be at most 1dp, so that last edit can be ignored, but kept for reference
Thanks, but wont that $_POST variable only be retrieved after the form is submitted? or am I being stupid?
I'm trying to make it change instantly, as a new value is entered into the amount box, or if that isn't possible, a button or something to update the total.
And also, I am not trying to round the amount entered, but the amount produced. For example, say someone donated $6.23 (unlikely, yes, but it could happen), that / 0.05 is 124.6. What I would want produced is 125, the nearest whole number. Sorry if I didn't make myself clear.
|
|
02-07-2010 11:03 PM |
|
|
Spunky
Former Super Mod
Posts: 3658 Reputation: 61
36 / /
Joined: Aug 2006
|
RE: Dynamically changing text box, based on a form input?
Use a javascript event...
Javascript code:
//define this function somewhere
function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
// x = amount (6.23 for example)
this.value = roundNumber(x / 0.05, 2);
I think that should do it... You should know roughly what to do with the code
EDIT: An untested example...
Javascript code:
<form name="MyForm" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<fieldset>
<label for="amount">$</label>
<input id="amount" name="amount" value="5.00" onchange="var id=document.getElementById('result');id.value='Result: $'+Math.round(((this.value*1)/0.05)*Math.pow(10,2))/Math.pow(10,2)"/> <label for="custom">Steam ID:</label>
<input id="custom" name="custom" value="<?php echo strstr(curPageURL(), 'STEAM_');?>" />
<input name="business" type="hidden" value="******************" />
<input name="cmd" type="hidden" value="_xclick" />
<input name="currency_code" type="hidden" value="USD" />
<input name="item_name" type="hidden" value="vip" />
<input name="no_note" type="hidden" value="1" />
<input name="no_shipping" type="hidden" value="1" />
<input src="http://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" type="image" alt="Click here to donate">
</fieldset>
</form>
<div id="result"></div>
This post was edited on 02-08-2010 at 12:21 AM by Spunky.
<Eljay> "Problems encountered: shit blew up"
|
|
02-07-2010 11:44 PM |
|
|
Jimbo
Veteran Member
Posts: 1650 Reputation: 18
32 / /
Joined: Jul 2006
|
O.P. RE: Dynamically changing text box, based on a form input?
Thanks a lot for the help Spunky, I really appreciate it. However, I tried messing with your code in a number of ways, and still cannot get it to display anything in the "result" element.
|
|
02-08-2010 12:15 AM |
|
|
Spunky
Former Super Mod
Posts: 3658 Reputation: 61
36 / /
Joined: Aug 2006
|
RE: Dynamically changing text box, based on a form input?
Edited silly mistake, but I still need to do more testing
<Eljay> "Problems encountered: shit blew up"
|
|
02-08-2010 12:22 AM |
|
|
stoshrocket
Senior Member
formerly methos
Posts: 748 Reputation: 31
34 / /
Joined: Aug 2005
|
RE: Dynamically changing text box, based on a form input?
quote: Originally posted by Jimbo
Thanks, but wont that $_POST variable only be retrieved after the form is submitted? or am I being stupid?
I'm trying to make it change instantly, as a new value is entered into the amount box, or if that isn't possible, a button or something to update the total.
And also, I am not trying to round the amount entered, but the amount produced. For example, say someone donated $6.23 (unlikely, yes, but it could happen), that / 0.05 is 124.6. What I would want produced is 125, the nearest whole number. Sorry if I didn't make myself clear.
Sorry, didn't realised you wanted it to calculate on the fly (should've guessed from the title...). As a note, the ceil(..) will produce a rounded value after the calculation.
As for the javascript, shouldn't "result" be either an input as your are trying to change it's "value", or change value to "innerText" so the text is placed within the div?
|
|
02-08-2010 12:36 AM |
|
|
Jimbo
Veteran Member
Posts: 1650 Reputation: 18
32 / /
Joined: Jul 2006
|
O.P. RE: Dynamically changing text box, based on a form input?
Thanks again, but I still can't get it to work, its probably just me though. I don't have to define that function anymore right, since I can't see you using that in the form?
|
|
02-08-2010 12:37 AM |
|
|
Spunky
Former Super Mod
Posts: 3658 Reputation: 61
36 / /
Joined: Aug 2006
|
RE: Dynamically changing text box, based on a form input?
This is tested... For some reason I can't update a div text so I've just used the tooltip for now.... No, you no longer need to define the function as I've put it all in line for you (but doesn't filter out NaN errors etc.
Javascript code:
<form name="MyForm" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<fieldset>
<label for="amount">$</label>
<input id="amount" name="amount" value="5.00" onkeyup="this.title='$'+Math.round((this.value*1)/0.05)"/>
<label for="custom">Steam ID:</label>
<input id="custom" name="custom" value="" />
<input name="business" type="hidden" value="******************" />
<input name="cmd" type="hidden" value="_xclick" />
<input name="currency_code" type="hidden" value="USD" />
<input name="item_name" type="hidden" value="vip" />
<input name="no_note" type="hidden" value="1" />
<input name="no_shipping" type="hidden" value="1" />
<input src="http://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" type="image" alt="Click here to donate">
</fieldset>
</form>
EDIT: Keep it in the onkeyup event else backspaces won't work and the script won't update after the user deletes a character. You may also need to add a 0 to the end of the tooltip in the event the number rounds to 1 (or even 0) decimal places as zeros get omitted
EDIT2: Big misunderstanding about the rounding. Fixed.
This post was edited on 02-08-2010 at 01:01 AM by Spunky.
<Eljay> "Problems encountered: shit blew up"
|
|
02-08-2010 12:40 AM |
|
|
Pages: (2):
« First
[ 1 ]
2
»
Last »
|
|
|