SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How do I get a user's input into an array?

    I'm adding the ability for the user to input his own values into 6 input fields, some or all of which may be left blank. I have already an array populated (thanks to Paul Wilkins) with default values. How do I get the user's values into that array so the calculations will be performed on it?

    I got this far, but was unable to understand the problems brought up by JSHint.com.

    User input:
    HTML Code:
                                <tr><td>
                                Alternate spur values:
                                <input class="display3" type="number" size="14" value="" name="spur1"><br>
                                <input class="display3" type="number" size="14" value="" name="spur2"><br>
                                <input class="display3" type="number" size="14" value="" name="spur3"><br>
                                <input class="display3" type="number" size="14" value="" name="spur4"><br>
                                <input class="display3" type="number" size="14" value="" name="spur5"><br>
                                <input class="display3" type="number" size="14" value="" name="spur6"><br>
                                </td></tr>
    Javascript array:

    HTML Code:
                function gearSpurs(ratio) {
                var form = document.getElementById('formGearRatio');
                var spur1 = form.elements.spur1.value,
                    spur2 = form.elements.spur2.value,
                    spur3 = form.elements.spur3.value,
                    spur4 = form.elements.spur4.value,
                    spur5 = form.elements.spur5.value,
                    spur6 = form.elements.spur6.value;
                    
                var userSpurs[spur1, spur2, spur3, spur4, spur5, spur6];
                
                    var spurs = [];
                    
                    // establish the array of spurs to calculate
                    switch (ratio) {
                        case 'sc10_4x4':
                        spurs = ['58', '60', '62', '93'];
                        break;
                        case 'sc10':
                        spurs = ['75', '78', '81', '84', '87'];
                        break;
                        case 'sc8':
                        spurs = ['50', '52', '54'];
                        break;
                        case 'b44':
                        spurs = ['72', '75', '78', '81', '84'];
                        break;
                        case 'b4t4':
                        spurs = ['72', '75', '78', '81', '84', '87'];
                        break;
                        case 'gt2':
                        spurs = ['54', '55', '56'];
                        break;
                        case 'userSpurs':
                        spurs = [spur1, spur2, spur3, spur4, spur5, spur6];
                        break;
                        default:
                        spurs = ['72', '75', '78', '81', '84', '87'];
                        break;
                    }
                    return spurs;
                }
    Thanks!

  2. #2
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make that form's action go to a php page.

    EX: <form action="action.php" method="post" >

    Have the php code simply put all the values in separate variables.

    <?php
    $spur1 = $_POST['spur1'];
    $spur2 = $_POST['spur2'];
    etc......
    ?>

    Now that you have this set up in php, make the html section. This will work because php renders/compiles before the page is loaded, so the javascript just sees the variables as straight up text or strings, and acts like you just typed them directly in there.

    <html>
    <head>
    <script type="text/javascript">


    var spur = new Array();
    spur[0] = <?php echo $spur1; ?>;
    spur[1] = <?php echo $spur2; ?>;
    etc...
    //After all this, you now have an array to use in javascript as you please.

    </script>
    </head>
    <body></body>
    </html>

    This may be too much in a different direction than you wanted, or you may not even have php running on the server you're using, so don't assume this is the only way to do this. This is just the way I would suggest you go, if you have php of course.

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, but I can't use PHP for this application. I don't see how to get the values of the array from within a Switch context so it fits in with the rest of the Switch statement.

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This issue has been solved.


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
  •