8 Replies - 856 Views - Last Post: 21 July 2014 - 05:50 AM

#1 12Strings  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 47
  • Joined: 19-April 14

is this a JS problem

Posted 19 July 2014 - 07:57 PM

Hi guys, my programming is a pasttime but still important to me. The following
code is a payment doc. allowing partial payments. Balances are put in "prevbal"
field. Complete payments get a "P" code. When the paidamt is keyed in, the
current date is autoinserted. If a recurring debt the amtdue stays the same.
If it is, the amtdue is calculated. I'm unsure if the failure is in the
javascript or not? The code using the JS follows:

==============================================================================
The javascript follows:
function $_(IDS) { return document.getElementById(IDS); }
function calculate_paid()
  {
     var status = document.getElementById("status");
             var pd = document.getElementById("pd");
       var payec = document.getElementById("payec");   
   var paidamt = document.getElementById("paidamt");
     var amtdue = document.getElementById("amtdue");
   var prevbal = document.getElementById("prevbal");
   var shipamt = document.getElementById("shipamt");
 var datepaid = document.getElementById("datepaid");
 
  var dateNow = new Date();
  var dayNow = dateNow.getDate();
  var datePaid = (dateNow.getMonth()+1)+"-"+dateNow.getDate()+"-"+dateNow.getFullYear();
datepaid.value = datePaid;
var owed.value == parseFloat(amtdue.value) + parseFloat(shipamt.value) +  parseFloat(prevbal.value) - parseFloat(paidamt.value);
if(status=="N"){amtdue.value = owed.value;}
if(owed=="0.00") { pd.value = "P"; shipamt="0.00", prevbal="0.00";}
  }
========================================================================

<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=7 id='amtdue' name='amtdue' value='" . $row['amtdue'] . "' ></td>
<td><input type='text' size=7 id='prevbal' name='amtdue' value='" . $row['prevbal'] . "' ></td>
<td><input type='text' size=10 id='datepaid' name='datepaid' value='" . $row['datepaid'] . "' ></td>
<td><input type='text' size=3 id='pd' name='pd' value='" . $row['pd'] . "' ></td><br />
<td><input type='submit' name='update' value='Make Payment' />
</tr>";
   }
echo "</table>
<input type='submit' name='update' value='Update Record' />
</form>";
  }
else{echo "invalid entry<br />Select another?<br />";}
  }
if(!empty($_POST["update"]))
  {
    $sql = "UPDATE oocust SET  
    amtdue = '" . mysql_real_escape_string($_POST['amtdue']) . "',
  paidamt = '" . mysql_real_escape_string($_POST['paidamt']) . "', 
datepaid = '" . mysql_real_escape_string($_POST['datepaid']) . "', 
            pd = '" . mysql_real_escape_string($_POST['pd']) . "'
  WHERE acctno='".$_POST['acctno']."'"; 
mysql_query($sql) or die(mysql_error());
 echo "Success!"; 
  } 
   ?>
<form method="post" action="#"><br /> 
 <input type="text" name="acctno"/> <p> 
<input type="submit" name="submit" value="Select"/>
</form></body></html>


Is This A Good Question/Topic? 0
  • +

Replies To: is this a JS problem

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,403
  • Joined: 08-June 10

Re: is this a JS problem

Posted 20 July 2014 - 12:30 AM

Quote

I'm unsure if the failure is in the javascript or not?

one way is to check the Error Console and in the dev tools what data have been sent to the server. if either of that applies, there’s something with the JS.


besides that, the mysql functions are deprecated, use PDO or mysqli instead (see my signature)
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,020
  • Joined: 12-December 12

Re: is this a JS problem

Posted 20 July 2014 - 03:07 AM

One thing to note is that it is not a good idea to use variables that are only distinguished by case, cf datepaid and datePaid.
Was This Post Helpful? 1
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,606
  • Joined: 23-August 08

Re: is this a JS problem

Posted 20 July 2014 - 03:02 PM

var owed.value == parseFloat(amtdue.value) + parseFloat(shipamt.value) +  parseFloat(prevbal.value) - parseFloat(paidamt.value);


That == is wrong. You're trying to assign to the owed.value variable, but == is used for comparison.
Was This Post Helpful? 2
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,020
  • Joined: 12-December 12

Re: is this a JS problem

Posted 20 July 2014 - 03:05 PM

You also cannot declare a variable like this:
var owed.value

You can create an object named owed and give it a value property:
var owed = {};
owed.value = 12;

but I doubt that this is what you intend.

This post has been edited by andrewsw: 20 July 2014 - 03:10 PM

Was This Post Helpful? 2
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,606
  • Joined: 23-August 08

Re: is this a JS problem

Posted 20 July 2014 - 03:19 PM

Good point; missed that. Probably just want to lose the .value in that and the usage after. I'll assume the OP got used to typing .value after all the textbox retrievals.
Was This Post Helpful? 1
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,020
  • Joined: 12-December 12

Re: is this a JS problem

Posted 20 July 2014 - 03:20 PM

I missed the double = ;)
Was This Post Helpful? 1
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,020
  • Joined: 12-December 12

Re: is this a JS problem

Posted 20 July 2014 - 06:08 PM

if(owed=="0.00") { pd.value = "P"; shipamt="0.00", prevbal="0.00";}

Once owed is sorted you won't need the quotes around 0.00 (assuming you are still using parseFloat), although it will still work. But, more importantly, shipamt and prevbal are currently references to page-elements (getElementById) and you cannot assign them these values.
Was This Post Helpful? 1
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

Re: is this a JS problem

Posted 21 July 2014 - 05:50 AM

You know, you made a helper function: function $_(IDS) but never used it. A few more helper functions might, ahem, help. Actually, more functions in general might help.

e.g
// not the best name choice, but what the hell
function $_(IDS) { return document.getElementById(IDS); }

function getValue(id) { return $_(id).value; }
function getValueF(id) { return parseFloat(getValue(id)); }
function setValue(id, value) { $_(id).value = value; }

function calcOwed(amtDue, shipAmt, prevBal, paidAmt) {
    return amtDue + shipAmt + prevBal - paidAmt;
}

function updateDate() {
    var dateNow = new Date();
    var dayNow = dateNow.getDate();
    var datePaid = (dateNow.getMonth()+1)+"-"+dateNow.getDate()+"-"+dateNow.getFullYear();
    setValue("datepaid", datePaid);
}

function calcPaid() {
    updateDate();
    var owed = calcOwed(getValueF('amtdue'), getValueF('shipamt'), getValueF('prevbal'), getValueF('paidamt'));
    setValue("owed", owed);
    if(getValue("status")=="N") { setValue("amtdue", owed); }
    if(owed==0.00) { setValue("pd", "P"); setValue("shipamt","0.00"), setValue("prevbal","0.00"); }
}



Hope this helps.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1