Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Update text as numbers are typed into input field
Old 07-21-2010, 10:17 PM Update text as numbers are typed into input field
Skilled Talker

Posts: 83
Trades: 0
I'm creating a page that calculates a number depending on what value is inputted into a text field. I need to create some javascript that updates this new calculated value and outputs it next to the input field. Anytime the user changes the number, it recalculates the value. My calculation is currently being produced by PHP, however if I need to, I can create javascript as well to recalculate these numbers.

My formulas for all 4 calculations are as follows:

First: Inputted Value * 10
Second: Inputted Value * 20
Third: Inputted Value / 2
Fourth: Inputted Value * 2

Here is my code I'm working with:

PHP Code:
<?php
session_start
();
$username $_SESSION['username'];

include_once(
'inc/connect.php');

$credquery mysql_query("SELECT * FROM userstats WHERE username='$username'");
$row mysql_fetch_assoc($credquery);

$credits $row['credits'];
$bannercredits $row['bannercredits'];
$textcredits $row['textcredits'];

$sitetobanner $_POST['sitetobanner'];
$sitetotext $_POST['sitetotext'];
$bannertotext $_POST['bannertotext'];
$texttobanner $_POST['texttobanner'];

if (isset(
$sitetobanner)){

$bannerimp round($_POST['bannerimp']);
    if (
$bannerimp 1){
        
$s "s";
    }
    if (
$bannerimp <= $credits){
        
$newcredits $credits $bannerimp;
        
$newbannercredits $bannerimp 10;
        
$totalbannercredits $bannercredits $newbannercredits;
        
        
$updatebanner mysql_query("UPDATE userstats SET credits='$newcredits', bannercredits='$totalbannercredits' WHERE username='$username'");
        
        
$credits $row['credits'];
        
$bannercredits $row['bannercredits'];
        
$textcredits $row['textcredits'];

        
$convertedbanner "You converted ".$bannerimp." credit".$s." into ".$newbannercredits." banner impressions";
        
    }
    else{
    if (
$credits>1){
    
$mycredits $credits 1;}
    
$errorbanner number_format($mycredits0'.''');

    }

}


if (isset(
$sitetotext)){

$textimp round($_POST['textimp']);
    if (
$textimp 1){
        
$s "s";
    }
    if (
$textimp <= $credits){
        
$newcredits $credits $textimp;
        
$newtextcredits $textimp 20;
        
$totaltextcredits $textcredits $newtextcredits;
        
        
$updatebanner mysql_query("UPDATE userstats SET credits='$newcredits', textcredits='$totaltextcredits' WHERE username='$username'");
        
        
$credits $row['credits'];
        
$bannercredits $row['bannercredits'];
        
$textcredits $row['textcredits'];
        
        
$convertedtext "You converted ".$textimp." credit".$s." into ".$newtextcredits." text ad impressions";
        
    }
    else{
    
    if (
$credits>1){
    
$mycredits $credits 1;}
    
$errortext number_format($mycredits0'.''');
    
    }

}


if (isset(
$texttobanner)){

$texttobannerimp round($_POST['texttobannerimp']);
    if (
$texttobannerimp 1){
        
$s "s";
    }

    if (
$texttobannerimp <= $textcredits){
        if (
$texttobannerimp>=2&&$texttobannerimp>""){
        
$newcredits $textcredits $texttobannerimp;
        
$newbannercredits $texttobannerimp 2;
        
$totalbannercredits $bannercredits $newbannercredits;
        
        
$updatebanner mysql_query("UPDATE userstats SET textcredits='$newcredits', bannercredits='$totalbannercredits' WHERE username='$username'");
        
        
$credits $row['credits'];
        
$bannercredits $row['bannercredits'];
        
$textcredits $row['textcredits'];
        
        
$convertedtextimp "You converted ".$texttobannerimp." text ad impression".$s." into ".$newbannercredits." banner impressions";
        }
        else{
            
$convertedtextimp "You Hag";
        }
    }
    else{
    
    if (
$textcredits>1){
    
$mycredits $textcredits 1;}
    
$errortextimp number_format($mycredits0'.''');
    
    }
}


if (isset(
$bannertotext)){

$bannertotextimp round($_POST['bannertotextimp']);
    if (
$bannertotextimp 1){
        
$s "s";
    }
    if (
$bannertotextimp <= $bannercredits){
        
$newcredits $bannercredits $bannertotextimp;
        
$newtextcredits $bannertotextimp 2;
        
$totaltextcredits $textcredits $newtextcredits;
        
        
$updatebanner mysql_query("UPDATE userstats SET bannercredits='$newcredits', textcredits='$totaltextcredits' WHERE username='$username'");
        
        
$credits $row['credits'];
        
$bannercredits $row['bannercredits'];
        
$textcredits $row['textcredits'];

        
$convertedbannerimp "You converted ".$bannertotextimp." banner impression".$s." into ".$newtextcredits." text impressions";
        
    }
    else{
    
    if (
$bannercredits>1){
    
$mycredits $bannercredits 1;}
    
$errorbannerimp number_format($mycredits0'.''');
    }
}



?>
<html>
<head>
<title>Convert Credits</title>
<script type="text/javascript" language="javascript">
function inputLimiter(e,allow) {
var AllowableCharacters = '';
if (allow == 'NumbersOnly'){AllowableCharacters='0123456789.';}

var k;
k=document.all?parseInt(e.keyCode): parseInt(e.which);
if (k!=13 && k!=8 && k!=0){
if ((e.ctrlKey==false) && (e.altKey==false)) {
return (AllowableCharacters.indexOf(String.fromCharCode(k))!=-1);
} else {
return true;
}
} else {
return true;
}
}

</script>
</head>
<body>
<h1>Convert Credits</h1><br />
Credits: <?php echo $credits?><br />
Banner Impressions: <?php echo $bannercredits?><br />
Text Ad Impressions: <?php echo $textcredits?><br />

<form action="convert.php" method="POST">
    <h3>Credits To Banner Impressions</h3>
    Convert <input type="text" name="bannerimp" id="NumbersOnly" onkeypress="return inputLimiter(event,'NumbersOnly')" value="<?php echo $errorbanner?>" onChange=""> credits into BLANK Banner Impressions! 
    <input type="submit" name="sitetobanner" value="Convert"><br />
    <?php echo $convertedbanner?>
</form>

<form action="convert.php" method="POST">
    <h3>Credits To Text Ad Impressions</h3>
    Convert <input type="text" name="textimp" id="NumbersOnly" onkeypress="return inputLimiter(event,'NumbersOnly')" value="<?php echo $errortext?>"> credits into BLANK Text Ad Impressions! 
    <input type="submit" name="sitetotext" value="Convert"><br />
    <?php echo $convertedtext?>
</form>

<form action="convert.php" method="POST">
    <h3>Text Ad To Banner Impressions</h3>
    Convert <input type="text" name="texttobannerimp" id="NumbersOnly" onkeypress="return inputLimiter(event,'NumbersOnly')" value="<?php echo $errortextimp?>"> Text Ad Impressions into BLANK Banner Impressions! 
    <input type="submit" name="texttobanner" value="Convert"><br />
    <?php echo $convertedtextimp?>
</form>

<form action="convert.php" method="POST">
    <h3>Banner To Text Ad Impressions</h3>
    Convert <input type="text" name="bannertotextimp" id="NumbersOnly" onkeypress="return inputLimiter(event,'NumbersOnly')" value="<?php echo $errorbannerimp?>"> Banner Impressions into BLANK Text Ad Impressions! 
    <input type="submit" name="bannertotext" value="Convert"><br />
    <?php echo $convertedbannerimp?>
</form>

</body>
</html>
Smudly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-22-2010, 06:31 AM Re: Update text as numbers are typed into input field
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
use the onblur event on the input field to trigger the update, so it will recalculate when the input field loses focus
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Update text as numbers are typed into input field
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.44694 seconds with 12 queries