SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2001
    Location
    San Francisco
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    summing variables problem

    This should be simple enough, but it doesn't seem to be working. I just have some fields for people to enter the number of tickets they want in four categories. This number of tickets in each category (member, nonmember, etc.) is multiplied by the ticket cost, which is being pulled out of a database, and then the total of the four categories is meant to be added up.

    I have checked that the database values are correct and they are displaying. In fact, the script works fine when it operates just on any one input field - multiplies correctly and returns the right result.

    The problem seems to come up whe I try to add the four input box totals after multiplying them by the ticket price. It gives me 'NaN' in the output field. Here's the script i'm using:

    <script language="Javascript">

    function calculate(){
    if (window.document.authenticate.mem_ticket.value != 0) {
    var amt1 = window.document.authenticate.mem_ticket.value * <?php print $s[mem_val]; ?>;
    }
    if (window.document.authenticate.student_ticket != 0) {
    var amt2 = window.document.authenticate.student_ticket.value * <?php print $s[student_val]; ?>;
    }
    if (window.document.authenticate.studentnon_ticket != 0) {
    var amt3 = window.document.authenticate.studentnon_ticket.value * <?php print $s[studentnon_val]; ?>;
    }
    if (window.document.authenticate.nonmember_ticket.value != 0) {
    var amt4 = window.document.authenticate.nonmember_ticket.value * <?php print $s[nonmember_val]; ?>;
    }
    var total=eval(amt1) + eval(amt2) + eval(amt3) + eval(amt4)
    window.document.authenticate.amount.value = total;
    }


    // here is the table it operates on


    <form method="post" action="reservelinkurl.com" name="authenticate">

    <input type="text" name="amount" value="">

    <input size=5 name="mem_ticket" onChange = "calculate();">

    <input size=5 name="nonmember_ticket" onChange = "calculate();">

    <input size=5 name="student_ticket" onChange = "calculate();">

    <input size=5 name="studentnon_ticket" onChange = "calculate();">

    </FORM>


    anybody know what might be going on here? huge thanks to anyone who can help!

    ian
    signature

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think your problem is that you will multiply the value in the text box by the ticket price if it is not equal to zero. This would take place even if the box is empty or had some other character in it such as a letter. That would give you the "nan". You probably could do a couple of things - one is set the initial value in the text boxes to zero and also you should do a check before you do the math to insure that the value in the text box is a number. some thing like this:
    if(isNaN(document.authenticate.mem_ticket.value))
    {
    alert("Must be a number!")
    document.authenticate.mem_ticket.focus()
    document.authenticate.mem_ticket.select()
    }
    else
    {
    if (window.document.authenticate.mem_ticket.value != 0) {
    var amt1 = window.document.authenticate.mem_ticket.value * <?php print $s[mem_val]; ?>;
    }
    }

    The term isNaN checks if it is-Not-a-Number. Hope that helps and good luck.

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2001
    Location
    San Francisco
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks - I plugged that script in - and while it did do well in checking the values for the number boxes to make sure they didn't contain non-numeric data, it didn't, unfortunately, solve the NaN problem. I'm still getting it in the output field. I'm a bit stumped, hopefully you're not though ; )

    If you'd like to look at in in action, please go over to http://www.itsyourworld.org/authenticate.php?page=58

    use login: guest@mysite.com
    use password: guest

    The code, with your additions, looks as follows:

    <script language="Javascript">

    function calculate(){
    if(isNaN(document.authenticate.mem_ticket.value))
    {
    alert("Must be a number!")
    document.authenticate.mem_ticket.focus()
    document.authenticate.mem_ticket.select()
    }
    else
    {
    if (window.document.authenticate.mem_ticket.value != 0) {
    var amt1 = window.document.authenticate.mem_ticket.value * <?php print $s[mem_val]; ?>;
    }
    }
    if(isNaN(document.authenticate.student_ticket.value))
    {
    alert("Must be a number!")
    document.authenticate.student_ticket.focus()
    document.authenticate.student_ticket.select()
    }
    else
    {
    if (window.document.authenticate.student_ticket.value != 0) {
    var amt2 = window.document.authenticate.student_ticket.value * <?php print $s[student_val]; ?>;
    }
    }
    if(isNaN(document.authenticate.studentnon_ticket.value))
    {
    alert("Must be a number!")
    document.authenticate.studentnon_ticket.focus()
    document.authenticate.studentnon_ticket.select()
    }
    else
    {
    if (window.document.authenticate.studentnon_ticket.value != 0) {
    var amt3 = window.document.authenticate.studentnon_ticket.value * <?php print $s[studentnon_val]; ?>;
    }
    }
    if(isNaN(document.authenticate.nonmember_ticket.value))
    {
    alert("Must be a number!")
    document.authenticate.nonmember_ticket.focus()
    document.authenticate.nonmember_ticket.select()
    }
    else
    {
    if (window.document.authenticate.nonmember_ticket.value != 0) {
    var amt4 = window.document.authenticate.nonmember_ticket.value * <?php print $s[nonmember_val]; ?>;
    }
    }

    var total=eval(amt1) + eval(amt2) + eval(amt3) + eval(amt4)
    window.document.authenticate.amount.value = total;
    }

    any more ideas? they are extremely welcome. thanks for your help.

    ian
    signature

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi ian,

    try using some alerts, as in alert(amt1), to see if you are getting what you expect. If, for example, the above comes out with NaN (after you've input a valid number) -- you should check what your php is sending.

    you can also try using parseInt or parseFloat to force strings into numbers.

    finally, if the amtX vars are numeric, there is no need for 'eval'

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

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