What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Dynamically changing text box, based on a form input?

Pages: (2): « First [ 1 ] 2 » Last »
Dynamically changing text box, based on a form input?
Author: Message:
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
31 / Male / Flag
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
Profile E-Mail PM Find Quote Report
Chrissy
Senior Member
****

Avatar

Posts: 850
Reputation: 5
29 / Male / Flag
Joined: Nov 2009
RE: Dynamically changing text box, based on a form input?
Using PHP?
02-07-2010 10:07 PM
Profile E-Mail PM Web Find Quote Report
stoshrocket
Senior Member
****

Avatar
formerly methos

Posts: 748
Reputation: 31
33 / Male / Flag
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 :P

This post was edited on 02-07-2010 at 10:26 PM by stoshrocket.
formerly methos
02-07-2010 10:14 PM
Profile PM Web Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
31 / Male / Flag
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 :P
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
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
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" :zippy:
02-07-2010 11:44 PM
Profile PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
31 / Male / Flag
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
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
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" :zippy:
02-08-2010 12:22 AM
Profile PM Find Quote Report
stoshrocket
Senior Member
****

Avatar
formerly methos

Posts: 748
Reputation: 31
33 / Male / Flag
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?
formerly methos
02-08-2010 12:36 AM
Profile PM Web Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
31 / Male / Flag
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
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
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" :zippy:
02-08-2010 12:40 AM
Profile PM Find Quote Report
Pages: (2): « First [ 1 ] 2 » Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On