Skip navigation
Currently Being Moderated

Only 1 of 3 text fields is entered

Aug 31, 2012 9:12 PM

Hi,

How would I use Spry so the viewer can only enter data in 1 text field out of a total of 3 text fields in a form and validate so 1 field must be entered. Or CSS perhaps?

Thanks

BTW I'm using CS5

 
Replies
  • Currently Being Moderated
    Sep 1, 2012 1:57 PM   in reply to gofer

    How about three radio buttons and only one text field that will not validate if empty?

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 1, 2012 2:42 PM   in reply to gofer

    In what way do they mess with your calculations? Radio buttons are indeed the right way to do this.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 2, 2012 4:53 AM   in reply to gofer

    Can you say again which fields is it that are causing you problems and how would you like the form to work?

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 2, 2012 2:46 PM   in reply to gofer

    The problem is that the JavaScript function you're using works only with text fields. It's not designed to work with radio buttons or other types of form input. There's a copyright notice with your CalculateTotal() script. If it's a script that you have paid for, you should ask the creator of the script to make it compatible with radio buttons. If it's a free script, it can be adapted to work with radio buttons, but it will take a little time to work out.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 2, 2012 3:54 PM   in reply to gofer

    gofer wrote:

     

    I did pay for CalculateTotal() script but I've since found the script on another site, the chap that I paid to create the script is not the person on the Copyright! And this guy wants more money to change to the script to be compatible with radio buttons

     

    It's sad that there are such disreputable people who will steal other people's copyright and charge. I've been playing around with the script, and it wasn't all that difficult to adapt. Try out the following:

     

    <!doctype html>

    <html>

    <head>

    <meta charset="utf-8">

    <title>Add Values</title>

    <script>

    function CalculateTotal(frm) {

        var order_total = 0

     

        // Run through all the form fields

        for (var i=0; i < frm.elements.length; ++i) {

     

            // Get the current field

            form_field = frm.elements[i]

     

            // Get the field's name

            form_name = form_field.name

     

            // Is it a "product" field?

            if (form_name.substring(0,4) == "PROD" && form_field.type != 'radio') {

     

                // If so, extract the price from the name

                item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

     

                // Get the quantity

                item_quantity = parseInt(form_field.value)

     

                // Update the order total

                if (item_quantity >= 0) {

                    order_total += item_quantity * item_price

                }

            } else if (form_name.substring(0,4) == "PROD" && form_field.type == 'radio' && form_field.checked == true) {

                order_total += parseInt(form_field.value);

            }

        }

         // Display the total rounded to two decimal places

        document.getElementById("order_total").firstChild.data = "$" + round_decimals(order_total, 2)

    }

     

    function round_decimals(original_number, decimals) {

        var result1 = original_number * Math.pow(10, decimals)

        var result2 = Math.round(result1)

        var result3 = result2 / Math.pow(10, decimals)

        return pad_with_zeros(result3, decimals)

    }

     

    function pad_with_zeros(rounded_value, decimal_places) {

     

        // Convert the number to a string

        var value_string = rounded_value.toString()

     

        // Locate the decimal point

        var decimal_location = value_string.indexOf(".")

     

        // Is there a decimal point?

        if (decimal_location == -1) {

         

            // If no, then all decimal places will be padded with 0s

            decimal_part_length = 0

         

            // If decimal_places is greater than zero, tack on a decimal point

            value_string += decimal_places > 0 ? "." : ""

        }

        else {

     

            // If yes, then only the extra decimal places will be padded with 0s

            decimal_part_length = value_string.length - decimal_location - 1

        }

     

        // Calculate the number of decimal places that need to be padded with 0s

        var pad_total = decimal_places - decimal_part_length

     

        if (pad_total > 0) {

         

            // Pad the string with 0s

            for (var counter = 1; counter <= pad_total; counter++)

                value_string += "0"

            }

        return value_string

    }

    </script>

    </head>

    <body>

    <form id="form1" name="form1" method="post" action="">

        <p>

            <input type="text" name="PROD_first_20" id="PROD_first_20" onChange="CalculateTotal(this.form)">

        </p>

        <p>

            <label>

                <input type="radio" name="PROD_freight" value="10" id="PROD_freight_0" onChange="CalculateTotal(this.form)">

                North</label>

            <br>

            <label>

                <input type="radio" name="PROD_freight" value="15" id="PROD_freight_1" onChange="CalculateTotal(this.form)">

                South</label>

            <br>

            <label>

                <input type="radio" name="PROD_freight" value="0" id="PROD_freight_2" onChange="CalculateTotal(this.form)">

                Nowhere</label>

        </p>

        <div id="order_total" style="text-align: right">$0.00</div>

    </form>

    </body>

    </html>

     

    What I have done is to adapt the CalculateTotal() function to work with radio buttons.

     

    Each radio button has the same name (the actual name is unimportant). The value attribute contains the value you want to add to the total.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 3, 2012 1:23 AM   in reply to gofer

    Hi gofer, instead of radio buttons (if you can't get those to work) you could use a 'select' list option. That should work, I've tested it. The 'select list' doesn't mess with the original javascript form calculations script (although David seems to have sorted this out for you now) and they return the correct totalprice via php.

     

    So you would insert a 'Select delivery option' on the form:

     

     

    select name="PROD_freight_1" onchange="CalculateTotal(this.form)">

        <option value="0" selected="selected">Collect Order $0</option>

    <option value="10">North Island $10</option>

    <option value="15" >South Island $15 </option>

     

    </select>

     

     

    Then amend the php freight variable to retreive the correct price:

     

    $freight = $_POST['PROD_freight_1'];

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 3, 2012 1:53 AM   in reply to gofer

    Radio buttons share the same name. Look again at the sample I gave you:

     

    <p>

            <label>

                <input type="radio" name="PROD_freight" value="10" id="PROD_freight_0" onChange="CalculateTotal(this.form)">

                North</label>

            <br>

            <label>

                <input type="radio" name="PROD_freight" value="15" id="PROD_freight_1" onChange="CalculateTotal(this.form)">

                South</label>

            <br>

            <label>

                <input type="radio" name="PROD_freight" value="0" id="PROD_freight_2" onChange="CalculateTotal(this.form)">

                Nowhere</label>

        </p>

     

    They are all called PROD_freight, so the value your PHP code should be looking for is $_POST['PROD_freight']. That will contain the number stored in the value attribute.

     

    If you rename the radio buttons PROD_freight_15, PROD_freight_10, and PROD_freight_0, they become independent buttons, all of which can be selected. The whole point of a radio button group is that only one can be selected. But for that to happen, each button in the group must have the same name.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 3, 2012 2:10 AM   in reply to David_Powers

    David_Powers wrote:

     

    gofer wrote:

     

    I did pay for CalculateTotal() script but I've since found the script on another site, the chap that I paid to create the script is not the person on the Copyright! And this guy wants more money to change to the script to be compatible with radio buttons

     

    It's sad that there are such disreputable people who will steal other people's copyright and charge. I've been playing around with the script, and it wasn't all that difficult to adapt. Try out the following:

     

     

    This was a freely available script which I found on the net and passed onto Gofer. As long as the Copyright notice remains at the top of the script I think the writer is ok about it being used. Not sure what Gofer is alluding to about payment. As far as I know it went straight from me to Gofer.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 3, 2012 2:35 AM   in reply to osgood_

    osgood_ wrote:

     

    Hi gofer, instead of radio buttons (if you can't get those to work) you could use a 'select' list option. That should work, I've tested it. The 'select list' doesn't mess with the original javascript form calculations script

     

    Although a select menu would offer the same "single-choice" option as a set of radio buttons, it presents the same problem for the JavaScript function. The way the function has been designed is that it extracts the price from the input element's name, and uses it to calculate the total cost. That's why gofer had PROD_freight_10, PROD_freight_15, and PROD_freight_0. The function grabs the numbers after the second underscore.

     

    But radio button groups and select menus can have only one name, so you have to get the value from elsewhere. What I did was to adapt the script so that it handles radio buttons separately and gets the price from the value attribute of the selected button. The script could be adapted to do the same with a select menu. It would be slightly more complicated, but still easily done.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 3, 2012 2:41 AM   in reply to osgood_

    osgood_ wrote:

     

    This was a freely available script which I found on the net and passed onto Gofer. As long as the Copyright notice remains at the top of the script I think the writer is ok about it being used. Not sure what Gofer is alluding to about payment. As far as I know it went straight from me to Gofer.

     

    I don't know anything about the provenance of the script other that what I've read here. Obviously, there's a misunderstanding somewhere.

     

    I have now changed the script, but I think the copyright notice should stay in because most of the work was done by the original developer. I would suggest rewording the copyright notice like this:

     

    /* Adapted from a script that is

    Copyright (c) Paul McFedries and

    Logophilia Limited (http://www.mcfedries.com/).

    Permission is granted to use this script as long as

    this Copyright notice remains in place.*/

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 3, 2012 2:43 AM   in reply to David_Powers

    David_Powers wrote:

     

    osgood_ wrote:

     

    Hi gofer, instead of radio buttons (if you can't get those to work) you could use a 'select' list option. That should work, I've tested it. The 'select list' doesn't mess with the original javascript form calculations script

     

    Although a select menu would offer the same "single-choice" option as a set of radio buttons, it presents the same problem for the JavaScript function.

     

     

     

    Hi David,

     

    It doesn't if you use the value 1 for PROD and then give the select options 10, 15 or 0 respectively. The script then seems to x it by that total based on 1 and add the correct value to the overall form total, job done! I could not do that with the radio buttons because as you say unless the javascript is amended (which I could'nt do) it messes up the calculations.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 3, 2012 2:47 AM   in reply to osgood_

    Ah, I see what you mean. You're setting the price as 1 and then multiplying the price by the selected value. Clever stuff!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points