Jump to content

Update text as numbers are typed into input field


Smudly

Recommended Posts

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
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($mycredits, 0, '.', '');

}

}


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($mycredits, 0, '.', '');

}

}


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($mycredits, 0, '.', '');

}
}


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($mycredits, 0, '.', '');
}
}



?>
<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>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.