Results 1 to 8 of 8

Thread: HTML radio button forms

  1. HTML radio button forms

    It seems like an easy question, but...

    I am creating a HTML form with selectable radio buttons that takes the options from a database via a php/MySQL query. No problems, except I seem to have no control over which radio button is pre-selected. It is always the last option in the query.

    Does anyone know a way to have the first option be pre-selected?

  2. the tag is,

    <option selected>your mom
    <option> mortal kombat

    or whatever.

    As long as the location is fixed its easy. I am grappling with something where the option MAY be Mortal kombat or MAY be your mom. You cant set a default value in the main definition (like you can with text, where its <input type=text value=whatever>.

  3. Here you go, Zerodash. The default radio button must have selected="selected" in the option tag.
    Code:
    <select>
    <option value ="yourmom">Your Mom</option>
    <option value ="mk">Mortal Kombat</option>
    <option value ="ibtn" selected="selected">IBTN</option>
    </select>
    R.I.P. Paragon Studios

  4. My problem is that the list is generated dynamically through a php function taking info from a database.

    My function forms an array with all the options in it, which is then called to create my form.

    If this is the only way to do it, then I may be stuck- unless I can alter the final loop in creating my array...

  5. You'll have to check the syntax, but the idea works. You create a flag and set it to true before you go through the array of dynamic values. It writes the selected attribute in the option tag for the first item of the array and then sets the flag to false. As you keep iterating through the array, the command to write the selected attribute is ignored.

    Code:
    <?
    $firsttime = true;
    echo "<select>";
    foreach ($dataarray as $value) {
      echo "<option value=\"$value\"";
      if ($firsttime) {
         echo " selected=\"selected\"";
         $firstime = false;
      }
      echo ">$value</option>";
    }
    echo "</select>";
    ?>
    R.I.P. Paragon Studios

  6. That's the direction I am going in. I actually tried using the counter variable for the conditional statement, but it didn't work, so I had to create a whole 'nother variable name...

  7. I'm not sure I understand how Cow's thing won't work. Is the option that's selected COMING from the database?

  8. Quote Originally Posted by Zerodash
    That's the direction I am going in. I actually tried using the counter variable for the conditional statement, but it didn't work, so I had to create a whole 'nother variable name...
    I'm pretty sure this is the only way you're gonna be able to pull it off with php. I had to do a similar thing for regular checkboxes in a webapp I need to rewrite, having two arrays (one for text input form data, one for checkbox form data) and using the latter to determine whether or not a checkbox is ticked or not. It's not pretty, and definitely adds a bit more code to the mix, but it does the job.

    ex: I have $formdata array with all my text input crap, and I have a $checkbox array that's extrapolated from the original array and only has keys of checkbox form elements that are selected upon submission (therefore, checking if the key value is not empty determines whether or not I drop a checked="checked" into the form element during post-processing.)

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
  •  
Games.com logo