Results 1 to 10 of 10

Thread: Radio Button Select to complete input field

  1. #1
    Join Date
    Oct 2007
    Posts
    47
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Radio Button Select to complete input field

    Hello there

    Hope you can help.

    What is the easiest way of populating an input field with information selected via a radio button.

    ie

    Radio Button 1 = Red
    Radio Button 2 = Blue
    Radio Button 3 = Yellow

    By selecting Radio Button 3 - the text 'Yellow', in this case, would complete an input field?

    Thanks

    Hamfast

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Do you mean populate or complete? Do you want the input to be otherwise editable or not?

    To populate the field would be to overwrite any value it already had, to complete it would be to add to its current value. I'm just going to guess:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function radioPop(el, field){
    for (var i = 0, buts=el.form.elements; i < buts.length; i++)
    if(buts[i].name==el.name&&buts[i].checked)
    el.form.elements[field].value=buts[i].value;
    }
    </script>
    </head>
    <body>
    <form action="#">
    <div>
    <input type="text" name="color" readonly><br>
    <input onclick="radioPop(this, 'color');" type="radio" name="colors" value="Red"> Red<br>
    <input onclick="radioPop(this, 'color');" type="radio" name="colors" value="Blue"> Blue<br>
    <input onclick="radioPop(this, 'color');" type="radio" name="colors" value="Yellow"> Yellow
    </div>
    </form>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2007
    Posts
    47
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    John

    Thanks for replying. Yes, the field, if possible, should be otherwise editable.

    It's just that I've got a whole load of options to choose from and it would be preferable if those using the website could just click on the PANTONE colour that they're after rather than having to type in the often annoyingly long winded name.

    However, to have the option to type it in would be agreeable.

    Cheers
    Hamfast

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Just remove:

    readonly
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Oct 2007
    Posts
    47
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    That's worked a treat. Ta muchly.

    But what's the best way of turning this into a php form so that details of colour choices can be sent to my email address?

    Cheers
    hamfast

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You would do that the same way that you would do it for any other PHP form. I imagine this varies, at least slightly, from server to server. You could ask in the PHP section. I think it might also require that the host have permissions set to allow that. A mail program on the server might be required, and even if not, could be used if present.

    One thing though, without javascript enabled, you could end up with a form with one value for colors and another for color. It might be best to write out using javascript either the radio buttons or the text input, depending upon which one you want to be mandatory for the non-javascript enabled browser. Whatever is hard coded is all that the non-javascript enabled person would see.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Oct 2007
    Posts
    47
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    John

    Thanks for coming back to me about that.

    I'll ask this in the php section if you can't help

    Is it possible to get a php form to work when it's split across more than 1 div?

    That is to say:

    The page is divided into narrow right and left columns and a big fat centre column.

    The existing php form is in the right hand side but the color picker you've helped me with is too vast to fit there and therefore sits in the middle column. When i fill in the requisite sections in the php form on the right hand side it all works well but the color picker option is not being included.

    Does that make any sense?

    Many, many thanks
    hamfast

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, a split form can work, but it all needs to be inside the one set of:

    HTML Code:
    <form></form>
    tags. You can have various divisions inside that, but you should check with a validator if you try to do something like so, which I believe will mess up at least some browsers:

    HTML Code:
    <div>
    <form>
    </div>
    <div>
    </form>
    </div>
    This though, would be fine:

    HTML Code:
    <form>
    <div>
    </div>
    <div>
    </div>
    </form>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Hm... I'd've done it more like this:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>Radio Buttons to Text -- Demo</title>
        <style type="text/css">
          label {
            display: block;
          }
        </style>
        <script type="text/javascript">
          function clearRadioButtons(frm, name) {
            for(var i = 0, e = frm.elements[name], n = e.length; i < n; ++i)
              e[i].checked = false;
          }
    
          function radioToText(frm, rdio, txt) {
            var radioChangeHandler = function() {
              if(this.checked) {
                this.form.elements[txt].value = this.value;
                break;
              }
            };
    
            for(var i = 0, f = frm.elements, e = f[rdio], n = e.length; i < n; ++i)
              e[i].onchange = radioChangeHandler;
    
            f[txt].onchange = function() {
              clearRadioButtons(this.form, rdio);
            };
    
            f = frm = e = null;
          }
        </script>
      </head>
      <body>
        <form action="" id="some_form">
          <div>
            <input type="text" name="colour">
            <label>
              <input type="radio" name="colours" value="Red">
              Red
            </label>
            <label>
              <input type="radio" name="colours" value="Green">
              Green
            </label>
            <label>
              <input type="radio" name="colours" value="Blue">
              Blue
            </label>
          </div>
        </form>
        <script type="text/javascript">
          radioToText(document.getElementById("some_form"), "colours", "colour");
        </script>
      </body>
    </html>
    That's somewhat more efficient, as well as being neater (none of those obtrusive onchange events, and using labels instead of abusing <br>).
    But what's the best way of turning this into a php form so that details of colour choices can be sent to my email address?
    It's already a "PHP form" if by this you mean its results can be sent to a PHP script. A script to perform an action on form data is entirely separate from the form itself (which is just plain HTML); for the PHP to do this, you can use one of the many floating about the Web, or ask in our PHP forum if they don't suit you and we'll help you write your own.

    EDIT: As an added gimmick,
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>Radio Buttons to Text -- Demo</title>
        <style type="text/css">
          label {
            display: block;
          }
        </style>
        <script type="text/javascript">
          function clearRadioButtons(frm, name) {
            for(var i = 0, e = frm.elements[name], n = e.length; i < n; ++i)
              e[i].checked = false;
          }
    
          function radioToText(frm, rdio, txt) {
            var radioChangeHandler = function() {
              if(this.checked) {
                var e = this.form.elements[txt];
                e.value = e.style.backgroundColor = this.value;
                break;
              }
            };
    
            for(var i = 0, f = frm.elements, e = f[rdio], n = e.length; i < n; ++i)
              e[i].onchange = radioChangeHandler;
    
            f[txt].onchange = f[txt].onkeyup = function() {
              clearRadioButtons(this.form, rdio);
              this.style.backgroundColor = this.value;
            };
    
            f = frm = e = null;
          }
        </script>
      </head>
      <body>
        <form action="" id="some_form">
          <div>
            <input type="text" name="colour">
            <label>
              <input type="radio" name="colours" value="Red">
              Red
            </label>
            <label>
              <input type="radio" name="colours" value="Green">
              Green
            </label>
            <label>
              <input type="radio" name="colours" value="Blue">
              Blue
            </label>
          </div>
        </form>
        <script type="text/javascript">
          radioToText(document.getElementById("some_form"), "colours", "colour");
        </script>
      </body>
    </html>
    Last edited by Twey; 10-30-2007 at 05:29 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Oct 2007
    Posts
    47
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Thanks very much for the responses. Can't believe how helpful you guys are. Thanks again.
    Hamfast.

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
  •