Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    need coding help

    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?

    Code:
    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;
      } 
         }

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    if(amtpaid.value < balance.value)
    balance should be an integer:

    if( amtpaid.value < balance )

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Jun 2010
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Below is the entire program:
    Code:
    <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 Code:
    <?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>>

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Jun 2010
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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:
    Code:
    //*
      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???
    Last edited by ckdoublenecks; 12-09-2010 at 11:27 PM. Reason: clarity

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Did you miss LogicAli's answer? You must at least do that.

    balance is a *variable*. It doesn't have a ".value" property.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New Coder
    Join Date
    Jun 2010
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Code:
    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.
    Last edited by ckdoublenecks; 12-10-2010 at 12:06 AM.

  • #9
    New Coder
    Join Date
    Jun 2010
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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

    Code:
    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;
      } 
          }

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    ckdoublenecks (12-10-2010)

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • Users who have thanked Philip M for this post:

    ckdoublenecks (12-10-2010)

  • #12
    New Coder
    Join Date
    Jun 2010
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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";

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    *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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    New Coder
    Join Date
    Jun 2010
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts
    The amtpaid is the only field I key values into. Everything works fine & changes as I enter the digits except prevbal :

    Code:
    <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>

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •