Update text as numbers are typed into input field

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>


View full post on Webmaster-Talk.com

, , , , , ,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get Adobe Flash playerPlugin by wpburn.com wordpress themes


RSS Site FeedRSS Site Feed

© 1992-2011 DC2NET™, Inc. All Rights Reserved