...

View Full Version : passing variable via url to a select box



mbarandao
12-05-2010, 04:41 AM
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

DrDOS
12-05-2010, 05:22 AM
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

mbarandao
12-05-2010, 05:46 AM
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
$var = $_REQUEST['clientID'];?>

and receive this inside my input box like this:


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

Lamped
12-05-2010, 05:56 AM
Is this a single or multiple selection? I'll run with single for now as it's just plain easier:



$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

mbarandao
12-05-2010, 06:06 AM
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!

Lamped
12-05-2010, 06:16 AM
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?

mbarandao
12-05-2010, 06:23 AM
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!

Lamped
12-05-2010, 06:39 AM
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.

mbarandao
12-05-2010, 08:39 AM
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:


$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

<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

syncupsolutions
12-05-2010, 09:15 AM
Maybe something like, $var being the posted username you want selected



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

mbarandao
12-05-2010, 03:41 PM
attemping the fix from the last post by syncupsolutions produces the following error



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.

mbarandao
12-05-2010, 05:23 PM
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>

MattF
12-05-2010, 05:25 PM
' selected="selected"'

mbarandao
12-05-2010, 05:38 PM
Thanks for the input MattF, that correction did not change the outcome.

Lamped
12-05-2010, 06:34 PM
<option'.($var == $WHAT_YOU_WANT_TO_MATCH_AGAINST ? ' selected="selected"' : '').'>$var</option>

mbarandao
12-05-2010, 06:57 PM
<option'.($var == $WHAT_YOU_WANT_TO_MATCH_AGAINST ? ' selected="selected"' : '').'>$var</option>

I'm trying to match with items in the existing select box


<select name=\"sel_id\" >


so your suggestion thus translates to:


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

with this, the outcome is still not selecting the value of $var. this is my html source code output


<select name="sel_id" >
<option'.(18719 == 18719 ? ' selected="selected"' : '').'>18719</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>
The option selected on load is value
107</option><option value="16"> when it suppose to be
18719</option><option value="35">

Lamped
12-05-2010, 07:50 PM
<?php

$items = array(
'UNKNOWN' => 18719,
36 => 107,
16 => 12451,
38 => 12672,
32 => 15818,
14 => 16253,
21 => 16726,
31 => 18266,
15 => 18719,
35 => 20240,
25 => 20821,
34 => 22756,
33 => 23986,
18 => 25486,
30 => 25553,
22 => 27128,
23 => 28389,
24 => 4993
);
echo('<select name="sel_id">');
foreach($items as $val => $item) {
echo('<option value="'.$val.'"'.($sel_id == $val ? ' selected="selected"' : '').'>'.$item.'</option>');
}
echo('</select>');

?>


You need to generate the whole select in PHP to find and match the right value. I didn't get what the value for 18719 should be, so I put in UNKNOWN - replace that with the right value.

mbarandao
12-05-2010, 08:10 PM
My only concern is that the values in the select box are dynamically populated from mysql. So at any given time that info can change. What is the implication of such condition with respect to Ur last suggestion?

Lamped
12-05-2010, 10:07 PM
Not a lot. You replace the array with a SQL query, and replace $val and $item with fields from the table.

mbarandao
12-06-2010, 01:51 AM
Testing Lamped's suggestion as an array with those preset is producing some abnormal outcome. Onload, the variable is passed to the 2nd page; however, the value selected in the box is always the last value and item(23=>28389) in the array list, when in fact the variable passed is 35=>20240. As can be seen in the html source code below. Firstly, the array list


$items = array(

36 => 107,
16 => 12451,
38 => 12672,
32 => 15818,
14 => 16253,
21 => 16726,
31 => 18266,
15 => 18719,
35 => 20240,
25 => 20821,
34 => 22756,
33 => 23986,
18 => 25486,
30 => 25553,
22 => 27128,
23 => 28389,// whatever number is listed as last in the list is the one selected in the selection box!!!

);

Here is a source code:



<select name="sel_id" >
<option value="'.23.'"'.(20240 == 23 ? ' selected="selected"' : '').'>28389</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>


Denoting: red is the passed variable
green is the wrongly selected variable and 23 its value.

I puzzled as to why it is selecting from the bottom --same outcome on multiple test.

Again, I'm trying to match the passed variable with the correct one in the list. I'm hoping that there is another way of doing this other than the array route.

Any thouhgts!
Mossa

mbarandao
12-06-2010, 04:26 AM
Issue resolved! After two days and much conversing with many of you on this forum, solution attained. It was a combination of multiple ideas suggested by lamped and syncupsolutions. Much oblige! here is the final solution:

Firstly, I had to pass both the variable and its id--this was one part that was missing initially.
Secondy, using a combination of syncupsolutions and lamped suggestions and some restructuring of the code the following was the silver bullet to the issue



$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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum