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:

/andy/admin/categories/paymentrecorder.php?sel_id=title14,title13

My selection box (data from mysql)

<select>
                                                <OPTION>Select One
						<OPTION>title14
						<OPTION>title13
						<OPTION>title12
						<OPTION>title11
						<OPTION>...
						<OPTION>..
						<OPTION>.
</select></span></td>

Is this doable --any thoughts?
Mossa

here you go:
url = http://yourdomain.com?selid[]=one&selid[]=two&selid[]=3

<?php

if(isset($_GET['selid']) && is_array($_GET['selid']) && count($_GET['selid']) > 0)
{
    echo '<select id="selid" name="selid">
    ';

    foreach($_GET['selid'] as $key=>&$value)
    {   
        echo '<option value="' . $key . '">' . $value . '</option>
        ';
    }   

    echo "</select>
    ";
        
}
?>

Much oblige, R0bb0b! Kind of you!
I'll attempt and advise...

Best!
Mossa

Not much luck with the earlier suggestion --script become unresponsive on implementation.

I used different method suggested by another forumer --getting close, but not there. Here is my issue currently:
the code below was suggested:

$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>');

Though 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 suggested code a bit and only used this

<option value='.($var ? ' selected='selected'' : '').'>$var</option>";

The part of my code that holds the existing select box is

$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

$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

I don't know what $var is but if you want the select box to preselect a value that is in the url then it should look like this:

$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']); 

          $display_block .= '<option value="' . $id . '"' . (isset($_GET['sel_id']) && trim($_GET['sel_id']) == $id?' selected="selected"':'') . '> 
               $display_name</option>'; 
          } 

      $display_block .= " </select>";

Thanks for the reply. $var is the value from the url obtained as:

$var = $_REQUEST['sel_id'];

Below is the modified 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']); 
    
         $display_block .= '<option value="' . $id . '"' . (isset($_GET['sel_id']) && trim($_GET['sel_id']) == $id?' selected="selected"':'') . '> 
         //$display_block .= "<option value=\"$id\"> 
              $display_name</option>"; 
          } 

      $display_block .= " </select>

I'm getting an error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Apache\htdocs\adds\apath\categories\paymentrecorder.php on line 68

Line 68 is the commented out line from above as:

//$clientID= $recs['clientID'];

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:

<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

$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>

Issue resolved! Here is the final solution:

I had to pass both the variable and its id--this was one part that was missing initially.

$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']); 


if($display_name==$var)
{
          
          $display_block .= "<option value=\"$id\" selected=\"selected\"> 
               $display_name</option>";
        }else
        {



        // $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>

I defined my variable being passed via url as:

$var = $_REQUEST['sel_id'];
$varid = $_REQUEST['id'];

And voila; the desired result as initial articulated is attained.

I'm most appreciative for the guidance and suggestions
Best,
Mossa

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.