Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts

    Setting Selected attribute in HTML dropdown Forms

    Hello,
    I am want to set the selected attribute in one of my forms dynamicly. Users get to the form by clicking on a link, and I want that link they click on to be set as the default value in the form.

    ex:
    Building the tables of LINKS:
    PHP Code:
    <table>
      <
    tr>
       <
    td>Rank</td>
       <
    td>Empire</td>
       <
    td>Villagers</td>
       <
    td>Score</td>
       <
    td></td>
       </
    tr>

    echo 
    '
    <tr>
    <td>'
    .$num.'</td>
    <td>'
    .$row['username'].'</td>
    <td>'
    .$row['villagers'].'</td>
    <td>'
    .$row['score'].'</td>
    <td><a href="attack.php?id='
    .$row['username'].'">Attack</a></td>
    </tr>'
    ;

    </
    table

    The Forum:
    PHP Code:
    <form name="attack_form" action="attack.php" method="get">
                
    Empire To Attack: <select size="1" class="search" name="id" id="jump-button">
    <option selected="selected"><?php //WANT THIS TO CHANGE DEPENDING ON LINK CLICKED BY USER ?></option>

            <?php 
            $sql 
    "SELECT username FROM resources";
            
    $result mysql_query($sql) or die(mysql_error());
            
    $row mysql_fetch_assoc($result);
            
    //do...while loop to dispay users in range of templates
            
    do {
            echo 
    '<option value="'.$row['username'].'">'.$row['username'].'</option>';
            
    $row mysql_fetch_assoc($result);    
            } while (
    $row
            
    ?>
            </select></p>    
            
    <input type="submit" name="attack" value="Attack!" />
    </p>
    </form>
    Last edited by srule_; 01-07-2008 at 04:44 AM.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,092
    Thanks
    2
    Thanked 322 Times in 314 Posts
    Set up a variable $selected that is either "selected="selected"" or an empty string "" depending on the logical conditions you want to test and then echo it in the option tag -

    PHP Code:
    $selected = (your_conditional_test_here) ? "selected=\"selected\"" ""// form the string

    echo "<option value=\"your_value\" $selected>your_label</option>"
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    additional to what CFMaBiSmAd say, you can find what user clicks using $_SERVER['HTTP_REFERER'], but you must check if the user don't disable referer in the browser, that means it is a valid url not '-' or ''.

    PS: in your code, don't do ... while, because the code inside is executed once even the while condition is false, and you don't want that,

    best regards
    Last edited by oesxyl; 01-07-2008 at 05:18 AM.

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    thx for the tip, I have been looking over my script along with what you posted but I can not seem to get my head around this topic.

    I need whatever username is selected by the user VIA the links to be the selected username in the form

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by srule_ View Post
    thx for the tip, I have been looking over my script along with what you posted but I can not seem to get my head around this topic.

    I need whatever username is selected by the user VIA the links to be the
    selected username in the form
    that means that this part:

    Code:
    <table>
      <tr>
       <td>Rank</td>
       <td>Empire</td>
       <td>Villagers</td>
       <td>Score</td>
       <td></td>
       </tr>
    is different for each user?

    username I suppose you pass thru via POST or GET but I don't understand the relation between username and links. More detail could help.

    best regards

  • #6
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    Yes, Im sorry. I typed the orginal code wrong. I forgot a loop!

    function forming links that is being called on ranks.php
    PHP Code:
        function displayUserRanks() {
            
    $sql "SELECT * FROM resources ORDER BY score DESC";
            
    $result mysql_query($sql) or die(mysql_error());
            
    $row mysql_fetch_assoc($result);
            
    $num=1;
                do {
        
         echo 
    '    <tr>
                   <td>'
    .$num.'</td>
                   <td><a href="profile.php?id='
    .$row['username'].'">'.$row['username'].'</a></td>
                   <td>'
    .$row['villagers'].'</td>
                   <td>'
    .$row['score'].'</td>
                   <td><a href="attack.php?id='
    .$row['username'].'">Attack</a></td>
                   </tr>'
    ;
                         
    //attack.php?id=123&attack=Attack%21         
            
    $row mysql_fetch_assoc($result); 
            
    $num++;   
          } while (
    $row);
        
        } 
    (Yes I know, that should not be a DO loop either, thanks for the help i will change it asap)

    form on page link sends user to via GET:
    PHP Code:
    <form name="attack_form" action="attack.php" method="get">
                <p>
                
                Empire To Attack: <select size="1" class="search" name="id" id="jump-button">
                                  <option selected="dddd"><?php $_GET['username'?></option>
        
                    <?php   
                    
                    $sql 
    "SELECT username FROM resources";
                    
    $result mysql_query($sql) or die(mysql_error());
                    
    $row mysql_fetch_assoc($result);
                    
    //do...while loop to dispay users in range of templates
                        
    do 
                        {
                        echo 
    '<option value="'.$row['username'].'">'.$row['username'].'</option>';
                        
    $row mysql_fetch_assoc($result);    
                        } while (
    $row
                    
    ?>
                                </select></p>    
                </p>
                <input type="submit" name="attack" value="Attack!" />
                </p>
                </form>
    and then I have more code that processes the the form, but I wont post it because it has nothing to do with trying to set the "selected" attribute.
    Last edited by srule_; 01-07-2008 at 05:44 AM.

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    split the code, make query, compute but not output

    this is displayUserRanks modified:

    PHP Code:
    function getUsersStuffByRank(){
       
    $users = array();
       
    $query "select * form resources order by score desc";
       
    $results mysql_query($query);
       if(
    $results){
          
    $num 1;
          while(
    $row mysql_fecth_assoc($results)){
             
    // you could also check for username as ''
             
    $users[$num++] = $row;
          }
       }
       return 
    $users// empty - no user, filled - ...

    you must insert this piece of code where you need after you call getUserStuffByRank ( I don't see where you call displayUserRanks ).

    PHP Code:
    foreach($users as $num => $user){
      echo 
    '    <tr>
       <td>'
    .$num.'</td>
       <td><a href="profile.php?id='
    .$user['username'].'">'.$user['username'].'</a></td>
       <td>'
    .$user['villagers'].'</td>
       <td>'
    .$user['score'].'</td>
       <td><a href="attack.php?id='
    .$user['username'].'">Attack</a></td>
       </tr>'
    ;
        
    //attack.php?id=123&attack=Attack%21

    output what you want, but more clear:

    Code:
    PHP Code:
    // other processing, GET's, and what you need <php $users getUsersStuffByRank(); ?> 
    <form name="attack_form" action="attack.php" method="get"> <p>Empire To Attack: <select size="1" class="search" name="id" id="jump-button"> <option selected="dddd"><?php $_GET['username'] ?></option> <?php foreach($users as $user){ echo '<option value="'.$user.'">'.$user.'</option>'; } ?> </select></p> </p> <input type="submit" name="attack" value="Attack!" /> </p> </form>
    I don't test anything the code is just to make clear how I see the solution.

    best regards
    Last edited by oesxyl; 01-07-2008 at 06:53 AM.

  • #8
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    Thank you, It works!

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by srule_ View Post
    Thank you, It works!
    I'm glad.

    Could be improved the code and the design. A better solution, I think, is to use ajax, but the pain is you must deal with php, javascript and html in the same time. The happy part is that both javascript and php support oop,

    PS: I talk about your app not only this module.

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •