#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    212
    Rep Power
    14

    Javascript Solution?


    I have an ASP script that loads with initial values for several different fields for a Javascript calculator. It is set up so when a visitor clicks the 'Calculate' button, it submits the results via e-mail then reloads the same page. I can get it to enter the visitor's values in textboxes that are empty, but the default values in the external .js file are replacing what the visitor enters, so they can't see the calculation.

    Here's an example of the code I'm using:

    Code:
    .js file: 
    
    function initialValues(){
    document.costForm.Week.value="4.00"
    }
    Code:
    .asp file: 
    
    TOP
    <% 
    Week2=request.form("Week")
    %>
    
    INSIDE HTML
    <input type="text" name="Week" style="text-align:right;" size="5" value="<%=Week2%>">
    Can somebody tell me how I can make the Javascript so it won't replace the value the visitor entered if the number is different from the initial value?

    Something like:

    detect if the value is 4.00

    if so enter initial value

    if not do not enter initial value


    Any help would be greatly appreciated.
    Last edited by JohnSaunders; June 19th, 2002 at 04:53 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2001
    Location
    Montana
    Posts
    504
    Rep Power
    14
    How do you submit results via e-mail?
    Have Eternal Life
    Learning is so unproductive...
    The more I learn the more questions I have!
    Therefore I am going backwards.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    212
    Rep Power
    14
    The results are sent using CDONTS in the .asp file.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    NJ, USA
    Posts
    91
    Rep Power
    13
    CDONTS is a server side mail application specifically for the NT Server. My understanding is it's basically used for transerring files.

    I don't see how this will help you pass a variable from a web page to a .js file.

    Walt
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    212
    Rep Power
    14
    CDONTS is just for sending mail. I need to use a javascript to detect if the value entered in the textbox is different then the default or initial value. An example of something simliar to what I would need is:

    if (document.costForm.Week.value=="") document.costForm.Week.value="4.00";

    Since I have: value="<%=Week2%>"> in the value for the textbox inside the ASP page, it would just load like value="" if the visitor entered the page for the first time. Then the Javascript would pick up the value="" so it would know to enter 4 for the value. If the visitor entered 6, the ASP code would pick it up, save it in the session and it would load 6 in the value to equal: value="6". Then when the Javascript loads it would see the value didn't equal "" and it wouldn't change it.

    Unfortunately, I could not get the code above to work but I think it just needs a minor change or two.

    Here is the function that I am using with the code above,

    function initialValues(){
    if (document.costForm.txtHrsWeek.value == ""){
    document.costForm.txtHrsWeek.value.replace("4.00")
    }
    }

    Can anybody help me?


    John
    Last edited by JohnSaunders; June 20th, 2002 at 12:23 AM.
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    25
    Bit confused by why people kept harking on about the email... but...

    possibly you shouldn't directly write the value into the box but use this as a function, and call it onLoad of the page.

    function setValue() {
    txtHrsWeekVal = "<%=Week2%>";
    if (txtHrsWeekVal == "") {
    document.costForm.txtHrsWeek.value = "4.00";
    } else {
    document.costForm.txtHrsWeek.value = txtHrsWeekVal;
    }
    }
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    NJ, USA
    Posts
    91
    Rep Power
    13
    The confusion about using email is because that's how John said he wants to pass the value to his function. I don't think it's confusing. I think it's just wrong.

    The proper way would be to use the action method of the form.

    Walt
  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    25
    Nah, what he said was:

    It is set up so when a visitor clicks the 'Calculate' button, it submits the results via e-mail then reloads the same page.
    I read that as that when the form is submitted there is an email sent and then the page with the form reloads. He's not passing any values to a function via the email at all. It's just a standard email sent from a form, sent from the page that is in the action attribute of the form.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    NJ, USA
    Posts
    91
    Rep Power
    13
    Originally posted by JohnSaunders
    The results are sent using CDONTS in the .asp file.
    And I read this to mean he's trying to submit the form values via e-mail.

    - Walt
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    NJ, USA
    Posts
    91
    Rep Power
    13

    Re: Javascript Solution?


    John,

    I think what you want to do is use an IF statement in your .js file to check the value being submitted. You should check for a Null or any other value that you set as the default.

    If the default value has changed use the new value. If the default hasn't changed use whatever you want.

    (You should always validate the values being submitted.)

    Assuming the default is Null and you want to use 4.00 if the user doesn't supply a value:

    .js file:

    function initialValues(){
    IF (document.costForm.Week.value == null)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// If the value is null the next line will assign 4.00 to the value
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// If the value is not null the value will not be changed.
    document.costForm.Week.value = 4.00
    }


    I hope it helps.

    Walt
  20. #11
  21. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    function initialValues() {
    var f = document.costForm;
    if (!f.txtHrsWeek.value) f.txtHrsWeek.value = '4.00';
    }

    Be sure to call these after page load (or embed your <script src="...."></script> tags below the form).

    How come you're not entering the initial (default) values with ASP instead of JS? Just curious....
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    212
    Rep Power
    14
    I just decided to have all the values in the same file that contained all the rest of the calculations/numbers so I could easily change them if I had to.

    Thanks to all who have replied. I was able to get it working using the code above. I really appreciate all of your wonderful help!


    John

IMN logo majestic logo threadwatch logo seochat tools logo