SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Drop down box

Hybrid View

  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Drop down box

    Hey,

    I am using the following drop down box to pull out values from a database:-

    PHP Code:
            <?php
                
    include ("conn.php");
                            
                
    $catsql "SELECT * FROM threads";
                
    $result mysql_query($catsql);
                
                
    $options="";
                
                while (
    $row=mysql_fetch_array($result)) 
                {
                    
    $cat_id=$row["thread_id"];
                    
    $cat_name=$row["thread_name"];
                    
    $options.="<option value=\"$cat_id\">".$cat_name;
                }
                
    ?>
                <select name="thing">
                <option value="0">Choose
                <?=$options?>
                </option>
            </select>
    I dont want to have a button to click to take users to a page. I want the user to select an option and the be taken to the specific page.

    However what i want is to send the user to page like this:-

    posts.php?thread_id=19&thread_name=Heaton Park Goals Development
    This information will come from teh drop down box, but i dont know how to do this?

    Regards

  2. #2
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I am now trying with this code:-

    PHP Code:
            <?php
                
    include ("conn.php");
                            
                
    $catsql "SELECT * FROM threads";
                
    $result mysql_query($catsql);
                
                
    $options="";
                
                while (
    $row=mysql_fetch_array($result)) 
                {
                    
    $thread_id=$row["thread_id"];
                    
    $thread_name=$row["thread_name"];
                    
    $options.="<option value=\"$cat_id\">".$thread_name;
                }
                
                echo 
    "<select name='thing' onchange='window.open(posts.php?thread_id=$row[thread_id]&thread_name=$row[thread_name])'>";
                echo 
    "<option value='0'>Choose";
                echo 
    "$options";
                echo 
    "</option>";
                echo 
    "</select>";
                
    ?>
    I receive no errors but when i change a selection on the drop down box the page does not change..

    Does anyone know how i can fix this?

    Regards

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    Beautiful Rhode Island, USA
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd suggest fixing up your HTML and see if that works. With that code, you've got a bunch of opening <option> tags but only one closing </option> tag, so that could be screwing up the javascript.

    PHP Code:
    while ($row=mysql_fetch_array($result)) 
                { 
                    
    $thread_id=$row["thread_id"]; 
                    
    $thread_name=$row["thread_name"]; 
                    
    $options.="<option value=\"$cat_id\">".$thread_name."</option>"
                } 
                 
                echo 
    "<select name='thing' onchange='window.open(posts.php?thread_id=$row[thread_id]&thread_name=$row[thread_name])'>"
                echo 
    '<option value="0">Choose</option>'
                echo 
    "$options"
                echo 
    "</select>"
    That might work, though the inline javascript isn't really a best practice. Let me know if it works out.

  4. #4
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thanks for the code, but its not working..

    When i select an option nothing happens..

    Any ideas why this is happening?

    Regards

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    Beautiful Rhode Island, USA
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you using Firefox and Firebug to see any javascript problems?

    The problem might be that you not passing the new url as a string to the window.open() function. Also, you are missing a parameter to the window.open() function. A window name is required.

    Try this (note the changes in ' and "):

    PHP Code:
    echo "<select name='thing' onchange=\"window.open('posts.php?thread_id={$row['thread_id']}&thread_name={$row['thread_name']}','newLocation')\">"

  6. #6
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    We're getting there now

    Whats happening now is that it takes me to the posts.php page but the URL shows like this:-

    So it does not recognize the thread_id and thread_name..

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    echo "<select name='thing' 
           onchange=\"window.open('posts.php?thread_id=" 
               
    $row['thread_id'
               . 
    "&hread_name=" 
               
    .  urlencode$row['thread_name'] )
               . 
    "' , 'newLocation')\">"
    Concatonate the strings of values when dealing with array elements.

    You should urlencode() the threadname too, it contains spaces which is not going to give you the exact result you expect, you'll needto urldecode it at the other end too.

  8. #8
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thanks for the reply, I have tried with the code you have suggested like so:-

    PHP Code:
            <?php
                
    include ("conn.php");
                            
                
    $catsql "SELECT * FROM threads";
                
    $result mysql_query($catsql);
                
                
    $options="";
                
                while (
    $row=mysql_fetch_array($result)) 

                { 
                    
    $thread_id=$row["thread_id"]; 
                    
    $thread_name=$row["thread_name"]; 
                    
    $options.="<option value=\"$cat_id\">".$thread_name."</option>"
                } 
                            
                echo 
    "<select name='thing' onchange=\"window.open('posts.php?thread_id=" $row['thread_id'] . "&thread_name=" .  urlencode$row['thread_name'] ). "' , 'newLocation')\">"
                echo 
    '<option value="0">Choose</option>'
                echo 
    "$options"
                echo 
    "</select>";

            
    ?>
    When i select something i still see blank values in the querystring. However if i put the Select in the "While" statement it works but not properly because it repeats the number of rows from the database..

    How can i make this work?

    Regards

  9. #9
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I've been trying to do this all day but cant seem to work it out, can anyone help?

    Basically i keep getting this querystring:-

    So the values are blank..

    Can anyone help?

    Regards

  10. #10
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to let you know,

    this code works:-

    PHP Code:
            <?php
                
    include ("conn.php");
                            
                
    $catsql "SELECT * FROM threads";
                
    $result mysql_query($catsql);
               
                
    $options="";
             
                
    $row=mysql_fetch_array($result);

                
    $thread_id=$row["thread_id"]; 
                
    $thread_name=$row["thread_name"]; 
                
    $options.="<option value=\"$thread_id\">".$thread_name."</option>"
                            
                echo 
    "<select name='thing' onchange=\"window.open('posts.php?thread_id=" $row['thread_id'] . "&thread_name=" .  urlencode$row['thread_name'] ). "' , 'newLocation')\">"
                echo 
    '<option value="0">Choose</option>'
                echo 
    "$options"
                echo 
    "</select>";

            
    ?>
    BUT it only shows on value from the database, obviously this is because its not in a while statement.. But if i do put it in a while statement then it stops working..

    Regards

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    $rows=mysql_fetch_array($result);

    PHP Code:

    foreach( $rows as $row ){

     
    $options.= "<option value='" 
                      
    $row['thread_id'
                      . 
    "'>" 
                      
    .$row['thread_name'
                      . 
    "</option>" PHP_EOL


  12. #12
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Right when i use the following code:-

    PHP Code:
            <?php
                
    include ("conn.php");
                            
                
    $catsql "SELECT * FROM threads";
                
    $result mysql_query($catsql);
               
                
    $options="";
             
                
    $rows=mysql_fetch_array($result);

                foreach( 
    $rows as $row ){
                
                 
    $options.= "<option value='" 
                                  
    $row['thread_id'
                                  . 
    "'>" 
                                  
    .$row['thread_name'
                                  . 
    "</option>" PHP_EOL
                } 
                            
                echo 
    "<select name='thing' onchange=\"window.open('posts.php?thread_id=" $row['thread_id'] . "&thread_name=" .  urlencode$row['thread_name'] ). "' , 'newLocation')\">"
                echo 
    '<option value="0">Choose</option>'
                echo 
    "$options"
                echo 
    "</select>";
            
    ?>
    The drop down values appear like this:-

    1
    1
    H
    H
    d
    d
    C
    C
    g
    g
    2
    2
    These dont even exist in the database..

  13. #13
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Some things to try.

    Look at the html source code.
    var_dump( $rows ) ; // see what that actually contains


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
  •