SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    prepopulate radio button

    This script prepopulates select box, state, but not radio button, tobacco.

    Code:
    <script language="javascript" type="text/javascript">
    
    function prepop()
    {
    document.long_form.state.value="<?php echo $_SESSION['state']; ?>";
    document.long_form.tobacco.value="<?php echo $_SESSION['tobacco']; ?>";
    }
    window.onload=prepop;
    
    </script>
    The value for tobacco, yes or no, shows on the page with view source, so it got to the page, but I can't find how to address a radio button so the value is preselected, and I'm out of ideas.

    Any suggestions?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.long_form.tobacco.checked = true;

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply, jimfraser.

    Adding that line to the javascript made no difference.

    Is there something else that should be done?

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,861
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    It should be:

    document.long_form.tobacco[0].checked = true;

    or if that doesn't check the right one then change the number between the [] to one less than the one that should be checked.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, felgall.

    Now at least one of the buttons shows as being checked on the receiving page.

    However, whether it should be yes or no depends on what the visitor checks in the first (short) form. The value is being passed for visitor convenience in case they also want to complete the long form.

    The value is received using

    Code:
    document.long_form.tobacco.value="<?php echo $_SESSION['tobacco']; ?>";
    so somehow that value needs to be specified.

    I tried a few alternatives based on your suggestion, but none have worked so far.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,861
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Try this (not sure if it will work and not where i can test it at the moment):

    document.long_form.tobacco["<?php echo $_SESSION['tobacco']; ?>"].checked = true;
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    felgall, that was one of the alternatives I had tried.

    It does not work because the value it delivers is either 'yes' or 'no' whereas the value needs to be either '0' or '1'' as explained in your previous reply.

    Here is the solution:

    Code:
    var tob;
    var tabac="<?php echo $_SESSION['tobacco']; ?>";
    {
    if (tabac=="yes")
    {tob="0"}
    else if (tabac=="no")
    {tob="1"}
    
    document.long_form.tobacco[tob].checked = true;
    Thanks for starting me on the right track.

    Resolved!


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
  •