...

View Full Version : need coding help



ckdoublenecks
12-09-2010, 09:17 PM
The code below is part of a working program which inserts "tentpay" and date paid. I'm trying to add the code which is below the comment line(//*). It does nothing? can someone look at it?


function calculate_paid(v)
{
var rentdue = document.getElementById("rentdue");
var prevbal = document.getElementById("prevbal");
var misc = document.getElementById("misc");
var late = document.getElementById("late");
var amtpaid = document.getElementById("amtpaid");
var tentpay = document.getElementById("tentpay");
var hudpay = document.getElementById("hudpay");
var datepaid = document.getElementById("datepaid");
var late = document.getElementById("late");
var dateNow = new Date();
var dayNow = dateNow.getDate();
var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
switch(v)
{
case amtpaid:
var tpay = amtpaid.value - hudpay.value;
if(tpay >= 0){tentpay.value = tpay;}
if(amtpaid.value > 0){datepaid.value = datePaid;}
break;
case tentpay:
if(!hudpay.value || hudpay.value == " "){hudpay.value = 0;}
if(!tentpay.value || tentpay.value == " "){tentpay.value = 0;tentpay.select();}
amtpaid.value = parseInt(tentpay.value) + parseInt(hudpay.value);
if(tentpay.value > 0){datepaid.value = datePaid;}
break;
case hudpay:
if(!tentpay.value || tentpay.value == " "){tentpay.value = 0;}
if(!hudpay.value || hudpay.value == " "){hudpay.value = 0;hudpay.select();}
amtpaid.value = parseInt(tentpay.value) + parseInt(hudpay.value);
if(hudpay.value > 0){datepaid.value = datePaid;}
break;
//*
case prevbal:
var balance = parseInt(rentdue.value) + parseInt(prevbal.value) + parseInt(misc.value) - parseInt(hudpay.value);
if(amtpaid.value < balance.value){
prevbal.value = parseInt(balance.value) - parseInt(amtpaid.value);}
if(dayNow.value > 5){late.value = "L";
prevbal.value = prevbal.value + 10;}
break;
}
}

Logic Ali
12-09-2010, 09:49 PM
if(amtpaid.value < balance.value)
balance should be an integer:

if( amtpaid.value < balance )

Old Pedant
12-09-2010, 09:49 PM
Can you show this live someplace? We are kind of whistling in the dark without the <form> to play with.

There's a BUNCH of code in there that looks to me like it won't work.

Example:
if(!hudpay.value || hudpay.value == " "){hudpay.value = 0;}

Ummm...so what about if hudpay.value == "" ?????? Or maybe it equals "ABCDEFG". Why are you looking specifically for just a single space and converting *THAT* to zero? Instead of converting *anything* invalid to zero?

Oh, and by the way, if this is a form field then the only way !hudpay.value will occur is if you omitted the field hudpay from the <form>. Form fields *ALWAYS* have a value. It might be "", but it will always be there.

Other weirdness:
var tpay = amtpaid.value - hudpay.value;
if(tpay >= 0){tentpay.value = tpay;}
Ummm...what happens if tpay is < 0 ???? You just ignore it? That's not even an error of some kind?

ckdoublenecks
12-09-2010, 10:53 PM
Below is the entire program:

<html><head>
<script>
function calculate_paid(v)
{
var rentdue = document.getElementById("rentdue");
var prevbal = document.getElementById("prevbal");
var misc = document.getElementById("misc");
var late = document.getElementById("late");
var amtpaid = document.getElementById("amtpaid");
var tentpay = document.getElementById("tentpay");
var hudpay = document.getElementById("hudpay");
var datepaid = document.getElementById("datepaid");
var late = document.getElementById("late");
var dateNow = new Date();
var dayNow = dateNow.getDate();
var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
switch(v)
{
case amtpaid:
var tpay = amtpaid.value - hudpay.value;
if(tpay >= 0){tentpay.value = tpay;}
if(amtpaid.value > 0){datepaid.value = datePaid;}
break;
case tentpay:
if(!hudpay.value || hudpay.value == " "){hudpay.value = 0;}
if(!tentpay.value || tentpay.value == " "){tentpay.value = 0;tentpay.select();}
amtpaid.value = parseInt(tentpay.value) + parseInt(hudpay.value);
if(tentpay.value > 0){datepaid.value = datePaid;}
break;
case hudpay:
if(!tentpay.value || tentpay.value == " "){tentpay.value = 0;}
if(!hudpay.value || hudpay.value == " "){hudpay.value = 0;hudpay.select();}
amtpaid.value = parseInt(tentpay.value) + parseInt(hudpay.value);
if(hudpay.value > 0){datepaid.value = datePaid;}
break;
//*
case prevbal:
var balance = parseInt(rentdue.value) + parseInt(prevbal.value) + parseInt(misc.value) - parseInt(hudpay.value);
if(amtpaid.value < balance.value){
prevbal.value = parseInt(balance.value) - parseInt(amtpaid.value);}
if(dayNow.value > 5){late.value = "L";
prevbal.value = prevbal.value + 10;}
break;
}
}
</script>
<script type="text/javascript">
window.google_analytics_uacct = "UA-256751-2";
</script>

<script type="text/javascript">
window.google_analytics_uacct = "UA-256751-2";
</script>

</head><body>

<?php
mysql_connect(localhost,root,"");
mysql_select_db(prerentdb) or die( "Unable to select database");
if(!empty($_POST["submit"]))
{
$apt = $_POST['apt'];
$query="SELECT * FROM payments Where apt='$apt'";
$result=mysql_query($query);
if(mysql_num_rows($result))
{
echo "<form action='#' method='post'><b>Rent Payment :<br /><br />
<table cellspacing=0 cellpadding=0 border=1>
<tr>
<th>dep</th>
<th>tenant</th>
<th>apt</th>
<th>paid</th>
<th>due</th>
<th>prev</th>
<th>misc</th>
<th>tent</th>
<th>hud</th>
<th>date</th>
<th>late</th>
<th>comments</th>
</tr>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>
<td><input type='text' size=5 name='dep' value='" . $row['dep'] . "'></td>
<td><input type='text' size=25 name='name' value='" . $row['name'] . "'></td>
<td><input type='text' size=2 name='apt' value='" . $row['apt'] . "' ></td>
<td><input type='text' size=4 id='amtpaid' name='amtpaid' value='" . $row['amtpaid'] . "'
onkeyup='calculate_paid(this)'></td>
<td><input type='text' size=4 name='rentdue' value='" . $row['rentdue'] . "'></td>
<td><input type='text' size=4 name='prevbal' value='" . $row['prevbal'] ."'
onkeyup='calculate_paid(this)'></td>
<td><input type='text' size=4 name='misc' value='" . $row['misc'] . "'></td>
<td><input type='text' size=4 id='tentpay' name='tentpay' value='" . $row['tentpay'] . "' onkeyup='calculate_paid(this)'></td>
<td><input type='text' size=4 id='hudpay' name='hudpay' value='" . $row['hudpay'] . "'
onkeyup='calculate_paid(this)'></td>
<td><input type='text' size=10 id='datepaid' name='datepaid' value='" . $row['datepaid'] . "'></td>
<td><input type='text' size=1 name='late' value='" . $row['late'] . "'
onkeyup='calculate_paid(this)'></td>
<td><input type='text' size=25 name='comments' value='" . $row['comments'] . "'></td>
</tr>";
}
echo "</table>
<input type='submit' name='update' value='Make Payment' />
</form>";
}
else{echo "No listing for apartment $apt.<br />Please select another.<br />";}
}
if(!empty($_POST["update"]))
{
$sql = "UPDATE payments SET
dep = '" . mysql_real_escape_string($_POST['dep']) . "',
name = '" . mysql_real_escape_string($_POST['name']) . "',
amtpaid = '" . mysql_real_escape_string($_POST['amtpaid']) . "',
rentdue = '" . mysql_real_escape_string($_POST['rentdue']) . "',
prevbal = '" . mysql_real_escape_string($_POST['prevbal']) . "',
misc = '" . mysql_real_escape_string($_POST['misc']) . "',
tentpay = '" . mysql_real_escape_string($_POST['tentpay']) . "',
hudpay = '" . mysql_real_escape_string($_POST['hudpay']) . "',
datepaid = '" . mysql_real_escape_string($_POST['datepaid']) . "',
late = '" . mysql_real_escape_string($_POST['late']) . "',
comments = '" . mysql_real_escape_string($_POST['comments']) . "'
WHERE apt='".$_POST["apt"]."'";
mysql_query($sql) or die("Update query failed.");
echo "Record for apartment ".$_POST["apt"]." has been updated";
}
?>
<form method="post" action="#">
<br />
<input type="text" name="apt"/> <p>
<input type="submit" name="submit" value="select apartment"/>
</form>
<script type="text/javascript"><!--
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//-->
</script>

<script type="text/javascript"><!--
try {
var pageTracker = _gat._getTracker("UA-256751-2");
pageTracker._trackPageview();
} catch(err) {}
//-->
</script>

<script type="text/javascript"><!--
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//-->
</script>

<script type="text/javascript"><!--
try {
var pageTracker = _gat._getTracker("UA-256751-2");
pageTracker._trackPageview();
} catch(err) {}
//-->
</script>
</body></html>>

Old Pedant
12-10-2010, 12:09 AM
Well, I don't even have PHP installed on my machine.

And even if I did, I don't have YOUR database installed.

So the PHP code is useless to me.

Now, if you want to bring up the page in your browser and then click on the VIEW menu and then on the SOURCE or PAGE SOURCE menu item... If you copy/paste the HTML you see from doing that, then we'd have a chance of seeing the actual HTML code.

ckdoublenecks
12-10-2010, 12:22 AM
thanks for the input, Old Pedant, all the program has been posted and as I said the program works, I just can't get the little bit of code right:

//*
case prevbal:
var balance = parseInt(rentdue.value) + parseInt(prevbal.value) + parseInt(misc.value) - parseInt(hudpay.value);
if(amtpaid.value < balance.value){
prevbal.value = parseInt(balance.value) - parseInt(amtpaid.value);}
if(dayNow.value > 5){late.value = "L";
prevbal.value = prevbal.value + 10;}
break;
}
}
no errors - just no results. I take that code out & it works, just no "L" & no preval results. it's in the new code???

Old Pedant
12-10-2010, 12:31 AM
Did you miss LogicAli's answer? You must at least do that.

balance is a *variable*. It doesn't have a ".value" property.

ckdoublenecks
12-10-2010, 12:45 AM
case prevbal:
var balance = parseInt(rentdue.value) + parseInt(prevbal.value) + parseInt(misc.value) - parseInt(hudpay.value);
if(amtpaid.value < balance){
prevbal.value = parseInt(balance) - parseInt(amtpaid.value);}
if(dayNow.value > 5){late.value = "L";
prevbal.value = prevbal.value + 10;}
break;

Thanks, does that look right - it still doesn't work?

I almost forgot, Old Pedant - in answer to your questions:
if(!hudpay.value || hudpay.value == " "){hudpay.value = 0;}

Ummm...so what about if hudpay.value == "" ?????? Or maybe it equals "ABCDEFG". Why are you looking specifically for just a single space and converting *THAT* to zero? Instead of converting *anything* invalid to zero?

Other weirdness:
var tpay = amtpaid.value - hudpay.value;
if(tpay >= 0){tentpay.value = tpay;}
Ummm...what happens if tpay is < 0 ???? You just ignore it? That's not even an error of some kind?
__________________
Someone on another forum was helping me and wrote that bit of code. I wondered about it but when I asked, he got snotty so when (if) I get this finished. I'll try to refine it. All I'm doing here is entering a payment (amtpaid). The amtpaid - hudpay is inserted into the tentpay & the current date (datepaid) is inserted. Right now I just need to finish this last bit to finish this system & am glad for your help.

ckdoublenecks
12-10-2010, 03:51 AM
I changed the code to the below & now it inserts a value into prevbal and even though its wrong, it's an improvement. It still doesn't insert the "L" when late. I'd appreciate suggestions


function calculate_paid()
{
var rentdue = document.getElementById("rentdue");
var prevbal = document.getElementById("prevbal");
var misc = document.getElementById("misc");
var late = document.getElementById("late");
var amtpaid = document.getElementById("amtpaid");
var tentpay = document.getElementById("tentpay");
var hudpay = document.getElementById("hudpay");
var datepaid = document.getElementById("datepaid");
var late = document.getElementById("late");
var dateNow = new Date();
var dayNow = dateNow.getDate();
var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
var balance = parseInt(rentdue.value) + parseInt(prevbal.value) + parseInt(misc.value) - parseInt(hudpay.value);
datepaid.value = datePaid;
tentpay.value = amtpaid.value - hudpay.value;
if(amtpaid.value < balance)
{
prevbal.value = balance - amtpaid.value;
}
if(dayNow.value > 5)
{
late.value = "L";
prevbal.value = balance + 10;
}
}

Old Pedant
12-10-2010, 08:41 AM
dayNow is *ALSO* a variable, so you can't use ".value" with it, either.

".value" can *ONLY* be used with variables that are reference to form field objects.

Philip M
12-10-2010, 09:19 AM
The core of the problem is that you do not distinguish between an object and the value of that object.

var rentdue = document.getElementById("rentdue");
rentdue is an object.

You should capture the numeric values of the objects with:-

var rentdue = parseFloat(document.getElementById("rentdue").value);
var prevbal = parseFloat(document.getElementById("prevbal").value);
and so on
and then simply e.g.
tentpay = amtpaid - hudpay;
if(dayNow > 5){late = "L"}




Quizmaster: What variety of large white bear can be found in the Arctic region?
Contestant: Penguin.

ckdoublenecks
12-11-2010, 12:37 AM
with the below code, when I enter the first digit of the amtpaid, the date & the "L" are inserted as well as the correct (at that time) prevbal & misc. When I enter the second & third digits it goes to hell.


function calculate_paid()
{
var rentdue = document.getElementById("rentdue");
var prevbal = document.getElementById("prevbal");
var misc = document.getElementById("misc");
var amtpaid = document.getElementById("amtpaid");
var tentpay = document.getElementById("tentpay");
var hudpay = document.getElementById("hudpay");
var datepaid = document.getElementById("datepaid");
var late = document.getElementById("late");
var dateNow = new Date();
var dayNow = dateNow.getDate();
var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
var total = parseInt(rentdue.value) + parseInt(prevbal.value) + parseInt(misc.value) - parseInt(hudpay.value);
datepaid.value = datePaid;
tentpay.value = amtpaid.value - hudpay.value;
prevbal.value = total - amtpaid.value;
if(amtpaid.value >= misc.value)
{
misc.value = 0;
}
if(dayNow > 5)
{
late.value = "L";

Old Pedant
12-11-2010, 01:05 AM
*PLEASE* don't tell me you are calling calculate_paid() on every keystroke!

You shouldn't be calling until all the form fields have been collected.

I can't see any other reason that the number of digits in amount_paid would matter, other than maybe creating bogus values in other fields.

ckdoublenecks
12-11-2010, 03:49 AM
The amtpaid is the only field I key values into. Everything works fine & changes as I enter the digits except prevbal :


<td><input type='text' size=5 name='dep' value='" . $row['dep'] . "'></td>
<td><input type='text' size=25 name='name' value='" . $row['name'] . "'></td>
<td><input type='text' size=2 name='apt' value='" . $row['apt'] . "' ></td>
<td><input type='text' size=4 id='amtpaid' name='amtpaid' value='" . $row['amtpaid'] ."' onkeyup='calculate_paid(this)'></td>
<td><input type='text' size=4 name='rentdue' value='" . $row['rentdue'] . "'></td>
<td><input type='text' size=4 name='prevbal' value='" . $row['prevbal'] ."'></td>
<td><input type='text' size=4 name='misc' value='" . $row['misc'] . "'></td>
<td><input type='text' size=4 id='tentpay' name='tentpay' value='" . $row['tentpay'] . "'></td>
<td><input type='text' size=4 id='hudpay' name='hudpay' value='" . $row['hudpay'] ."' ></td>
<td><input type='text' size=10 id='datepaid' name='datepaid' value='" . $row['datepaid'] . "'></td>
<td><input type='text' size=1 name='late' value='" . $row['late'] . "'></td>
<td><input type='text' size=25 name='comments' value='" . $row['comments'] . "'></td>

Old Pedant
12-11-2010, 04:11 AM
Pardon me, but that's a bad decision. What happens if you accidentally type in "3W5"?? It would make more sense to simply wait for the entire entry. You could do the calculation based on an onblur or onchange event on the field.

Anyway, without seeing the <form> in action, I have no idea what the bug is.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum