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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post

    passing variable via url to a select box

    Good day to all:

    Typically, when one pass variables via url, they are received and displayed in a input text box.

    I'm interested in passing two variables (firstname and lastname) via url; except, I want to be able to get them on another page [key point ], in a select box. The select box is populated with data from mysql and contains the value of the variable being passed via url. I simply want to be able to have these two variables displayed in the box as if they were selected via keyboard.

    Not sure if I'm making any sense, let me strenghten my point with an example:

    url with variables:

    Code:
    /andy/admin/categories/paymentrecorder.php?sel_id=title14,title13
    My selection box (data from mysql)

    Code:
    <select>
                                                    <OPTION>Select One
    						<OPTION>title14
    						<OPTION>title13
    						<OPTION>title12
    						<OPTION>title11
    						<OPTION>...
    						<OPTION>..
    						<OPTION>.
    </select></span></td>
    Is this doable --any thoughts?
    Mossa

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,918
    Thanks
    15
    Thanked 227 Times in 227 Posts
    If you're just displaying them, why not use a button? A button looks just like a select box but is easier to code and can be inert. Here's a page that uses buttons that way.

    http://ronbeau.50webs.com/enigmaM3M4_index.html

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Thanks for the reply; however, I'm not sure I'm following your suggestion.

    I have 2 pages:
    page one has a link on it, onclick, it passes a variable that is encoded in the link (ie: somelink.php?name1=$name1). I want to receive this variable on page 2.

    Typically if I were using the variable in input box, I would simply do the following:

    PHP Code:
    <?php
    $var 
    $_REQUEST['clientID'];?>
    and receive this inside my input box like this:

    Code:
    <input name="name1" id="name1" size="15" maxlength="16"  style="background-color: #00FFFF" value="<?php echo $var?>">
    I want to do the same, but with a select box. Could you elaborate on your suggestion of buttons in relation to the above --I trust my explanation is clear.

    Mossa-
    Last edited by mbarandao; 12-05-2010 at 05:08 AM.

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Is this a single or multiple selection? I'll run with single for now as it's just plain easier:

    PHP Code:
    $items = array('name 1''name 2''name 3');
    echo(
    '<select>');
    foreach(
    $items as $item) {
      echo(
    '<option'.($item == $_REQUEST['clientID'] ? ' selected="selected"' '').'>'.$item.'</option>');
    }
    echo(
    '</select>'); 
    If you want a multiple selection, use in_array() and specify it in the URL as clientID[]=1&clientID[]=2 etc
    Last edited by Lamped; 12-05-2010 at 05:00 AM.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Lamped, thanks for joining in.

    The form's action is not set to page2. it is simply a link on page 1 with that variable attached to it. On click, it access that particular page. I want the variable in that url passed to a select box --which has the value being passed --and others in it.

    I want to avoid having the user --who having been linked to this page, to scroll the select box for that value. It would already be the first in the box.

    Hope this expound a bit on my need!

    Your thoughts!
    Last edited by mbarandao; 12-05-2010 at 05:28 AM.

  • #6
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    I'm not 100% sure of certain points. The code I've given you will take a value from the URL, and mark it selected in a <select> element, while displaying a list of entries from an array.

    If it needs to skip a page, you'll probably end up using sessions.

    Can you be more clear, or explain why my code wouldn't work?
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #7
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Lamped View Post
    I'm not 100% sure of certain points. The code I've given you will take a value from the URL, and mark it selected in a <select> element, while displaying a list of entries from an array.

    If it needs to skip a page, you'll probably end up using sessions.

    Can you be more clear, or explain why my code wouldn't work?
    Sorry, I'm testing your suggestion now. My last post was in response to a post that you replaced with the code.

    I'll advise shortly!
    Last edited by mbarandao; 12-05-2010 at 06:05 AM.

  • #8
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    I'm gonna try sleeping again, don't know what's up with me tonight. Probably large project + coffee = bad. (it's 5:30am)

    Good luck and I'll catch up with you in the morning if it's not solved by then.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #9
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Ok, here is what's happening. The code suggested by Lamped is of the right idea. It takes the variables from the url and passes them to another page into a select box. The problem is that it creates a new select box with the new data. This obviousily is not want I'm trying to do.

    I do not want to create a new box, the box already exist with these values in it -populated from mysql. I simply want to look in the box for a match with the passed variables; and if matched, simply display the value without scrolling box.

    I modified the code a bit and instead of using:
    PHP Code:
    $items = array('name 1''name 2''name 3');
    echo(
    '<select>');
    foreach(
    $items as $item) {
      echo(
    '<option'.($item == $_REQUEST['clientID'] ? ' selected="selected"' '').'>'.$item.'</option>');
    }
    echo(
    '</select>'); 
    I used only this
    Code:
    <option value='.($var ? ' selected='selected'' : '').'>$var</option>";
    The part of my code that holds the existing select box is

    Code:
     $display_block .= " <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
    
          <p><strong>Select a client to View:</strong><br> 
          <select name=\"sel_id\"> 
          <option value=\"\">-- Select One --</option>"; 
    
          while ($recs = mysql_fetch_array($get_list_res)) 
          { 
              //$clientID = $recs['clientID'];
              $id = $recs['id']; 
              $display_name = stripslashes($recs['display_name']); 
    
              $display_block .= "<option value=\"$id\"> 
                   $display_name</option>"; 
              } 
    
          $display_block .= " 
          </select>
    I modified the above section in this manner

    Code:
    $display_block .= " <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
    
          <p><strong>Select a client to View:</strong><br>
                		
         				 <select name=\"sel_id\"> 
       
       <option value='.($var ? ' selected='selected'' : '').'>$var</option>"; 
             
          while ($recs = mysql_fetch_array($get_list_res)) 
          { 
              //$clientID = $recs['clientID'];
              $id = $recs['id']; 
              $display_name = stripslashes($recs['display_name']); 
    
              $display_block .= "<option value=\"$id\"> 
                   $display_name</option>"; 
              } 
    
          $display_block .= " </select>
    Now with this modification, the variable from the url is identified and displayed as first in the existing select box; however, something else happens --which is unexpected.

    I believe the variable is being added(written) to list box; whereas, I want it to be selected as an existing value. Because it is added, the next process of retrieving additional data based on that value returns an error because the code sees that value as new --though it is exactly the same the another in the box which if I were to scroll to it and select performs as expected.

    Quite a interesting dilemmas!

    Does anyone have any thoughts on any of this!
    Mossa
    Last edited by mbarandao; 12-05-2010 at 03:20 PM.

  • #10
    New Coder
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post
    Maybe something like, $var being the posted username you want selected

    Code:
    $display_block .= " <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
    
          <p><strong>Select a client to View:</strong><br>
                		
         				 <select name=\"sel_id\"> 
             
          while ($recs = mysql_fetch_array($get_list_res)) 
          { 
              //$clientID = $recs['clientID'];
              $id = $recs['id']; 
              $display_name = stripslashes($recs['display_name']);
            if($display_name==$var){
              
              $display_block .= "<option value=\"$id\" selected=\"selected\"> 
                   $display_name</option>";
            }else{
    
              $display_block .= "<option value=\"$id\"> 
                   $display_name</option>"; 
              } 
         }
    
          $display_block .= " </select>

  • #11
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    attemping the fix from the last post by syncupsolutions produces the following error
    Code:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Apache\htdocs\andy\admin\categories\paymentrecorder.php on line 69
    the only problem is that line 69 has been commented out. and when I completely remove that commented item which ever item that is moved up on line 69 is still being pointed to as Parse error.
    Last edited by mbarandao; 12-05-2010 at 03:21 PM.

  • #12
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    I may have found the possible cause for my issue. viewing the source code on the html page, I notice how that url variable is added to the list box. like this:

    Code:
    <select name="sel_id" > 
          <option value='.(16726 ? ' selected='selected'' : '').'>16726</option><option value="36">  
                  107</option><option value="16">  
                  12451</option><option value="38">  
                  12672</option><option value="32">  
                  15818</option><option value="14">  
                  16253</option><option value="21">  
                  16726</option><option value="31">  
                  18266</option><option value="15">  
                  18719</option><option value="35">  
                  20240</option><option value="25">  
                  20821</option><option value="34">  
                  22756</option><option value="33">  
                  23986</option><option value="18">  
                  25486</option><option value="30">  
                  25553</option><option value="22">  
                  27128</option><option value="23">  
                  28389</option><option value="24">  
                  4993</option> </select>
    notice that the first option(in green) is the variable being passed in the list box where as the values below are the ones below are the ones it suppose to select. Additionally notice that option one has a different value. it supposed to be the same as the one in red. I believe there in lies my issue. any idea on how to address.

    To produce the above effect, my code is modified this way

    Code:
    $display_block .= " <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
    
          <p><strong>Select a client to View:</strong><br>
                		
         				 <select name=\"sel_id\" > 
          <option value='.($var ? ' selected='selected'' : '').'>$var</option>";
     while ($recs = mysql_fetch_array($get_list_res)) 
         { 
              //$clientID= $recs['clientID'];
              $id= $recs['id']; 
              $display_name= stripslashes($recs['display_name']); 
    
            // $display_block .= "<option value= \"' . $id . '\"' . (isset($_REQUEST['sel_id']) && trim($_REQUEST['sel_id']) == $id?' selected=\"selected\"':'') . '> ";
             $display_block .= "<option value=\"$id\">  
                  $display_name</option>"; 
              } 
    
          $display_block .= " </select>

  • #13
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    ' selected="selected"'

  • #14
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Thanks for the input MattF, that correction did not change the outcome.

  • #15
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Code:
    <option'.($var == $WHAT_YOU_WANT_TO_MATCH_AGAINST ? ' selected="selected"' : '').'>$var</option>
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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