functions, forms, and user entered data

kronis72

I'm a little lost here. What I have is a basic menu with an item and a price, set up as table within a form. I have a text input box for the user to enter a value — the number of that item wanted.

I want to know how I use the data entered into the input text box after the user clicks the calculate button, so that I can validate it and use it in an equation. If the user wants "2" of the item, I need to bring that value into my function so I can validate it as a numeric value. I'll then use it as an integer to multiply it by the price associated with that item...

Here is a sample of my code:

        function calculate(){
        var error = false; 
        if (isNaN(*"what would I put here??"*){
            alert("A numeric value is required when ordering steak");
            error = true;
        }
        else{
            var numbercrabcakes = parseInt(-------);
        }
        if (!error)
        {
            total = numbercrabcakes * 15 * 1.0825;
            window.alert("Your order totals: " + total.toFixed(2))
        }
    } 


.........


<form name="myform">
 <script type="text/javascript">
 /* <![CDATA[ */
   document.write("<table border = '1'>")
   document.write("<tr><th>Item Picture</th><th>Item Description</th><th>Item Price</th> \
        <th>Quanity</tr>");
   document.write("<tr><td><img src = 'images/crabcakes.png'></td><td>Cirtus infused crab \                      
   cakes with a sweet and savory sauce</td><td>$" + price[0] + "</td> \
   <td><input type='text' id='crabcakes' size='10' value='0' style='text-align:right;' /></td>
   </tr>");
  /* ]]> */
  </script>
  <p id="submit_button"><input type="submit" value="Calculate" onclick="calculate()" \
       style="width:150px;height:30px;text-align:center;font-weight:bold;" /></p>
</form>

The function section of the code was given as a sample/guideline to be used and edited for our specific page. Do I need to give the input box an id or name and use getElementbyId so I can check it against the isNaN then use the parseInt to convert it as the function shows. I did try that, but it didn't seem to work for me.

user3589536

If you just want to access the value of your textbox with the id "crabcakes" you could do it like so:

function calculate(){
        var value = document.getElementById("crabcakes").value;
        var error = false; 
        if (isNaN(value)){
            alert("A numeric value is required when ordering steak");
            error = true;
        }
        else{
            var numbercrabcakes = parseInt(value);
        }
        if (!error)
        {
            total = numbercrabcakes * 15 * 1.0825;
            window.alert("Your order totals: " + total.toFixed(2))
        }
    } 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing user entered data upon submit in google forms

From Dev

Update Access Tables with Data entered in forms

From Dev

How to determine data type entered by user?

From Dev

VBA Hide User form but retain data entered into it

From Dev

String split issue with user entered data

From Dev

Python Tkinter - processing data entered by a user

From Dev

Check if Data was entered by the User or by a macro [VBA]

From Dev

Validating Entered Data with custom constraints in Django Forms in __init__

From Dev

Xamarin Forms - How to populate ListView from the data you entered?

From Dev

Keeping the data that user entered in textbox even after submit button is clicked

From Dev

Storing user-entered data from a Wix installer

From Dev

How to get the data in the format of user entered not in the database stored format

From Dev

Going back and forth between intents without losing user entered data

From Dev

Storing user-entered data from a Wix installer

From Dev

Orbeon Form Builder- Saving User entered data in mySQL

From Dev

How to count the number of user entered data in each column except null?

From Dev

How to track in Google Analytics what data the user entered in the input?

From Dev

Selfhosted Nancy: NullReferenceException occurred in Nancy.Authentication.Forms.dll When correct user/password is entered

From Dev

Save All user data in Forms authentication cookies

From Dev

User specific data in automatic generated forms

From Dev

Django forms selecting data based on request user

From Dev

symfony2 forms dynamically Generate Forms Based on user Data

From Dev

Check if user entered a digit

From Dev

checking value entered by user

From Dev

Validate URL entered by user

From Dev

In C++ for the use of arrays within functions, where data is entered into arrays from a .txt file using ifstream

From Dev

program to read data from a text file and display the output in abbreviated form with all punctuation marks as entered my the user

From Dev

Is it possible to make JSF restore user entered values regardless of the data being valid?

From Dev

what is a recommended way to redisplay a form with user-entered data after an exception?

Related Related

  1. 1

    Accessing user entered data upon submit in google forms

  2. 2

    Update Access Tables with Data entered in forms

  3. 3

    How to determine data type entered by user?

  4. 4

    VBA Hide User form but retain data entered into it

  5. 5

    String split issue with user entered data

  6. 6

    Python Tkinter - processing data entered by a user

  7. 7

    Check if Data was entered by the User or by a macro [VBA]

  8. 8

    Validating Entered Data with custom constraints in Django Forms in __init__

  9. 9

    Xamarin Forms - How to populate ListView from the data you entered?

  10. 10

    Keeping the data that user entered in textbox even after submit button is clicked

  11. 11

    Storing user-entered data from a Wix installer

  12. 12

    How to get the data in the format of user entered not in the database stored format

  13. 13

    Going back and forth between intents without losing user entered data

  14. 14

    Storing user-entered data from a Wix installer

  15. 15

    Orbeon Form Builder- Saving User entered data in mySQL

  16. 16

    How to count the number of user entered data in each column except null?

  17. 17

    How to track in Google Analytics what data the user entered in the input?

  18. 18

    Selfhosted Nancy: NullReferenceException occurred in Nancy.Authentication.Forms.dll When correct user/password is entered

  19. 19

    Save All user data in Forms authentication cookies

  20. 20

    User specific data in automatic generated forms

  21. 21

    Django forms selecting data based on request user

  22. 22

    symfony2 forms dynamically Generate Forms Based on user Data

  23. 23

    Check if user entered a digit

  24. 24

    checking value entered by user

  25. 25

    Validate URL entered by user

  26. 26

    In C++ for the use of arrays within functions, where data is entered into arrays from a .txt file using ifstream

  27. 27

    program to read data from a text file and display the output in abbreviated form with all punctuation marks as entered my the user

  28. 28

    Is it possible to make JSF restore user entered values regardless of the data being valid?

  29. 29

    what is a recommended way to redisplay a form with user-entered data after an exception?

HotTag

Archive