www.webdeveloper.com
Results 1 to 5 of 5

Thread: function technique

  1. #1
    Join Date
    Apr 2013
    Posts
    74

    function technique

    Hi, I'm really confused. I've been advised that I should be calling:
    Code:
    function $_(IDS)
    from this function
    Code:
    function $_(IDS) { return document.getElementById(IDS); }
    function calculate_paid()  
      {
    This is the call
    HTML Code:
     <td><input type='text' size=7 id='paidamt' name='paidamt' value='" . $row['paidamt'] ."' 
    onBlur='calculate_paid(this)'></td>
    Any advice?

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    It is unclear what you are really trying to do.

    You pass an argument to the 'calculate_paid(this)' function, but you don't seem to use it in the function definition
    (which is also undefined). 'this' is the input element and it's id is 'paidamt' so at the very least you would need
    to call the function as 'calculate_paid(this.id)' and then actually use the information in the called function.

    Further analysis of your problem would require more of the (missing?) code.

  3. #3
    Join Date
    Apr 2013
    Posts
    74
    JMRKER, thanks for your response. Half of the time I'm told not to be so verbose, Half want see more code. Forgive me but I'm first going to show you code I developed about 4 years
    ago (property mgt system), I know it is functioning today.: I'm getting on in years so , you know ...the memory. Now it's a pastime(Really always was):
    <html><head>
    <script>
    function $_(IDS) { return document.getElementById(IDS); }
    function calculate_paid() {
    var amtpaid = document.getElementById("amtpaid");
    var rentdue = document.getElementById("rentdue");
    var prevbal = document.getElementById("prevbal");
    var hudpay = document.getElementById("hudpay");
    var tentpay = document.getElementById("tentpay");
    var datepaid = document.getElementById("datepaid");
    var late = document.getElementById("late");
    var paidsum = document.getElementById("paidsum");
    var dateNow = new Date();
    var dayNow = dateNow.getDate();
    var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
    datepaid.value = datePaid;
    paidsum.value = parseInt(paidsum.value) + parseInt(amtpaid.value);
    tentpay.value = parseInt(tentpay.value) + parseInt(amtpaid.value) - parseInt(hudpay.value);
    if(dayNow > 5) { late.value = "L";}
    }
    </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','cookie');
    mysql_select_db('homedb') 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>Name</th>
    <th>Apt</th>
    <th>Paid</th>
    <th>Due</th>
    <th>Prev Bal</th>
    <th>Hud Pay</th>
    <th>Tent Pay</th>
    <th>Date Paid</th>
    <th>L</th>
    <th>Comments</th>
    <th>Total</th>
    </tr>";
    while($row = mysql_fetch_assoc($result))
    {
    echo "<tr>
    <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'] ."'
    onBlur='calculate_paid(this)'></td>
    <td><input type='text' size=4 id='rentdue' name='rentdue' value='" . $row['rentdue'] . "'></td>
    <td><input type='text' size=4 id='prevbal' name='prevbal' value='" . $row['prevbal'] ."'
    onBlur='calculate_paid(this)'></td>
    <td><input type='text' size=4 id='hudpay' name='hudpay' value='" . $row['hudpay'] ."' ></td>
    <td><input type='text' size=4 id='tentpay' name='tentpay' value='" . $row['tentpay'] . "'></td>
    <td><input type='text' size=10 id='datepaid' name='datepaid' value='" . $row['datepaid'] . "'></td>
    <td><input type='text' size=1 id='late' name='late' value='" . $row['late'] . "' ></td>
    <td><input type='text' size=25 name='comments' value='" . $row['comments'] . "'></td>
    <td><input type='text' size=4 name='paidsum' value='" . $row['paidsum'] . "' ></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
    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']) . "',
    hudpay = '" . mysql_real_escape_string($_POST['hudpay']) . "',
    tentpay = '" . mysql_real_escape_string($_POST['tentpay']) . "',
    datepaid = '" . mysql_real_escape_string($_POST['datepaid']) . "',
    late = '" . mysql_real_escape_string($_POST['late']) . "',
    comments = '" . mysql_real_escape_string($_POST['comments']) . "',
    paidsum = '" . mysql_real_escape_string($_POST['paidsum']) . "'
    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>
    Now the current code(the function):
    function $_(IDS) { return document.getElementById(IDS); }
    function calculate_paid()

    {
    var pd = parseFloat(document.getElementById("pd").value;
    var shipamt = parseFloat(document.getElementById("shipamt").value;
    var datepaid = parseFloat(document.getElementById("datepaid").value;
    var paidamt = parseFloat(document.getElementById("paidamt").value;
    var checkno = parseFloat(document.getElementById("checkno").value;
    var prevbal = parseFloat(document.getElementById("prevbal").value;
    var amtdue = parseFloat(document.getElementById("amtdue").value;

    var mm = dateNow.getMonth() + 1;
    mm = (mm < 10) ? '0' + mm : mm;
    var dd = dateNow();
    dd = (dd< 10) ? '0' + dd : dd;
    var datePaid = (dateNow.getFullYear()+"-"+mm+"-"+dd);


    amtdue = amtdue + prevbal + shipamt - paidamt;

    var sum = amtdue - paidamt;
    if (sum==0)
    { pd = "P"; prevbal = 0; shipamt = 0;}
    prevbal = sum; pd = "N";
    checkno = checkno + 1;
    }
    The current code:
    <html><head>
    <!--when the paidamt is keyed in, the current date & paid code are autoinserted-->
    <script type="text/javascript" src="payment.js"></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 bgcolor="#ccffff"><b><center>
    <?php
    // error_reporting(0);
    error_reporting(E_ALL ^ E_NOTICE);
    mysql_connect('localhost','root','cookie');
    mysql_select_db('homedb') or die( "Unable to select database");
    if(!empty($_POST["submit"]))
    {
    $acctno = $_POST['acctno'];
    $query="SELECT * FROM oocust Where acctno='$acctno'";
    $result=mysql_query($query);
    if(mysql_num_rows($result))
    {
    echo date('m/d/y');
    ?>

    <form action='#' method='post'>Invoice Payment :<p>
    <table cellspacing=0 cellpadding=0 border=1>
    <th colspan=3></th>
    <th bgcolor="#ccffff" colspan=6>Edit fields</th>
    <tr>
    <th colspan=3></th>
    <th bgcolor="#ffccff" colspan=4>Amount</th>
    <th bgcolor="#ccffff">Date</th>
    <th bgcolor="#ccffff"></th>
    <tr>
    <th bgcolor="#ccffff">Acct#</th>
    <th bgcolor="#ccffff">Name</th>
    <th bgcolor="#ccffff">Descr</th>
    <th bgcolor="#ffccff">Paid</th>
    <th bgcolor="#ccffff">Check#</th>
    <th bgcolor="#ccffff">Prevbal</th>
    <th bgcolor="#ffccff">Due</th>
    <th bgcolor="#ccffff">Paid</th>
    <th bgcolor="#ccffff">Pd?</th>
    </tr>
    <?php
    while($row = mysql_fetch_assoc($result))
    {
    echo "<tr>
    <td><input type='text' readonly size=15 name='acctno' value='" . $row['acctno'] . "' ></td>
    <td><input type='text' readonly size=25 name='bname' value='" . $row['bname'] . "'></td>
    <td><input type='text' readonly size=25 name='purpose' value='" . $row['purpose'] . "'></td>

    <td><input type='text' size=7 id='paidamt' name='paidamt' value='" . $row['paidamt'] ."'
    onBlur='calculate_paid(this)'></td>


    <td><input type='text' size=5 id='checkno'name='checkno' value='" . $row['checkno'] . "' ></td>
    <td><input type='text' size=7 id='prevbal'name='prevbal' value='" . $row['prevbal'] . "' ></td>
    <td><input type='text' size=7 id='amtdue' name='amtdue' value='" . $row['amtdue'] . "'></td>
    <td><input type='text' size=10 id='datepaid' name='datepaid' value='" . $row['datepaid'] . "'></td>
    <td><input type='text' size=1 id='pd' name='pd' value='" . $row['pd'] . "' ></td>
    </tr>";
    }
    echo "</table>
    <input type='submit' name='update' value='make payment' />
    </form>";
    }
    else{echo "invalid entry for account# $acctno.<br />Select another?<br />";}
    }
    if(!empty($_POST["update"]))
    {
    $sql = "UPDATE oocust SET
    datepaid = '" . mysql_real_escape_string($_POST['datepaid']) . "',
    paidamt = '" . mysql_real_escape_string($_POST['paidamt']) . "',
    checkno = '" . mysql_real_escape_string($_POST['checkno']) . "',
    prevbal = '" . mysql_real_escape_string($_POST['prevbal']) . "',
    amtdue = '" . mysql_real_escape_string($_POST['amtdue']) . "',
    pd = '" . mysql_real_escape_string($_POST['pd']) . "'
    WHERE acctno='".$_POST["acctno"]."'";
    mysql_query($sql) or die("Update query failed: " . mysql_error());
    echo "Record for acct# ".$_POST["acctno"]." has been updated";
    }
    ?>
    <form method="post" action="#">
    <br />Click and select<img src="arrow14.gif">
    <input type="text" name="acctno"/> <p>
    Then
    <input type="submit" name="submit" value="Submit"/><p>
    </form>
    </body></html>

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb

    Two part comment below. Part #2 will be sent later

    Part #1 (you working version) does not run when I "cut and paste".
    If you have a working version that is running,
    it would be better to analyze that for the problems you are having.


    Code:
    <html><head>
    <script>
    function $_(IDS) { return document.getElementById(IDS); }
    function calculate_paid() {
    var amtpaid = document.getElementById("amtpaid");
    var rentdue = document.getElementById("rentdue");
    var prevbal = document.getElementById("prevbal");
    var hudpay = document.getElementById("hudpay");
    var tentpay = document.getElementById("tentpay");
    var datepaid = document.getElementById("datepaid");
    var late = document.getElementById("late");
    var paidsum = document.getElementById("paidsum");
    var dateNow = new Date();
    var dayNow = dateNow.getDate();
    var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
    datepaid.value = datePaid;
    paidsum.value = parseInt(paidsum.value) + parseInt(amtpaid.value);
    tentpay.value = parseInt(tentpay.value) + parseInt(amtpaid.value) - parseInt(hudpay.value);
    if(dayNow > 5) { late.value = "L";}
    }
    </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>
    .......
    Some suggestions for above since
    1. You define $_ but you don't use it.
    2. Extra <script...> tags can be eliminated

    For example, see modified code below:
    Code:
    <html><head>
    <script type="text/javascript">
    function $_(IDS) { return document.getElementById(IDS); }
    function calculate_paid() {
      var amtpaid = $_("amtpaid");
      var rentdue = $_("rentdue");
      var prevbal = $_("prevbal");
      var hudpay = $_("hudpay");
      var tentpay = $_("tentpay");
      var datepaid = $_("datepaid");
      var late = $_("late");
      var paidsum = $_("paidsum");
      var dateNow = new Date();
      var dayNow = dateNow.getDate();
      var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
      datepaid.value = datePaid;
      paidsum.value = parseInt(paidsum.value) + parseInt(amtpaid.value);
      tentpay.value = parseInt(tentpay.value) + parseInt(amtpaid.value) - parseInt(hudpay.value);
      if(dayNow > 5) { late.value = "L";}
    }
    
    window.google_analytics_uacct = "UA-256751-2";
    window.google_analytics_uacct = "UA-256751-2";
    </script>
    </head><body>
    .......
    Note that the use of the [ code] and [ /code] tags (without the spaces)
    maintains your formatting (if you had any) better than your first post.

    I don't use PHP or mySQL, so I will be of no help concerning those aspects of your code.

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb

    Part #2 comments:

    As before, the current code could be scripted like this.
    It also assumes that the element 'id' values match in the <body> of your program.

    Code:
    function $_(IDS) { return document.getElementById(IDS); }
    
    function calculate_paid() {
      var pd = parseFloat($_("pd").value);
      var shipamt = parseFloat($_("shipamt").value);
      var datepaid = parseFloat($_("datepaid").value);
      var paidamt = parseFloat($_("paidamt").value);
      var checkno = parseFloat($_("checkno").value);
      var prevbal = parseFloat($_("prevbal").value);
      var amtdue = parseFloat($_("amtdue").value); 
    
      var mm = dateNow.getMonth() + 1; 
      mm = (mm < 10) ? '0' + mm : mm;
      var dd = dateNow();
      dd = (dd< 10) ? '0' + dd : dd;
      var datePaid = (dateNow.getFullYear()+"-"+mm+"-"+dd);
    
      amtdue = amtdue + prevbal + shipamt - paidamt;
    
      var sum = amtdue - paidamt;
      if (sum==0) { pd = "P"; prevbal = 0; shipamt = 0;}
      prevbal = sum; pd = "N";
      checkno = checkno + 1;
    }
    Note that EVERY ONE of your parseFloat functions were using incorrect syntax. Each was missing a final ')' character.

    Note also that you should be using the error console if you are using FF or Chrome browsers
    to find the easy syntax errors.

    Note too that your 'calculate_paid()' function does a whole lot of nothing.
    Each of the variables you so carefully collect to use in the formulas right after appear to be local.
    Changing local variables does not effect any global variables which I assume you have with similar names.
    As soon as the function completes, the values and calculations are lost.

    The remainder of you "current code" post has so many errors that I stopped analyzing the problem
    and will wait for you to clean up you code some more before I make any more discouraging comments .

    Good Luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles