SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Location
    Cornwall, UK
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Dynamic select box problem

    I've got a couple of functions to produce a form and it's all working fine but I want the select box to pick up a value and have that option selected. I can't see the fatal flaw in my coding though!

    Code:
     function selected($var1, $var2) 
    {
    if ($var1==$var2)
    {
    return "selected";
    }
    }
    Code:
     function optSelect($selector) 
    {
    $select="<option value=\"t\" selected=\"".$this->selected("t",$selector)."\">yes</option>";
    $select.="<option value=\"f\" selected=\"".$this->selected("f",$selector)."\">yes</option>";
    return $select;
    }
    finally my form.

    Code:
     ... 
    <label>Active:</label><select name=\"cactive\">".$this->optSelect("t")."</select> 
    ....
    It should pick up "t" as being the correct value and make that selected however it seems to make both selected and shows the "f" value in the box.

    Can anyone help?

    Many thanks,
    Alix
    Last edited by Alix23; Jul 9, 2006 at 06:53.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well the resulting html of your code must be
    Code:
    <option value="t" selected="">yes</option>
    <option value="f" selected="selected">yes</option>
    i think what you should be shooting for is
    Code:
    <option value="t">yes</option>
    <option value="f" selected="selected">yes</option>

    PHP Code:

     
    function selected($var1$var2
    {
    if (
    $var1==$var2)
    {
    return 
    ' selected="selected"';
    }
    }

     function 
    optSelect($selector
    {
    $select '<option value="t"' $this->selected("t",$selector) . ">yes</option>";
    $select .= '<option value="f"' $this->selected("f",$selector) . ">yes</option>";
    return 
    $select;


  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Location
    Cornwall, UK
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that - it was the problem and is all sorted now.

    Alix


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
  •