views:

327

answers:

4

Hi,

I have a quick question.

Is it possible to do something like this?

The options are generated dynamically based on foreach loop. I want the loop to stop where the $_GET['t'] equals the $k and make it selected.

<option value="http://domain.com/&lt;?php print $k; ?>/" <?php if ($k == $_GET['t']) print 'selected'; ?>><?php print $v; ?></option>

Basically is there a way to make the option selected/highlighted based on the provided $_GET[t'] value on the address bar.. I tried it and i get undefined index: t error.

am i missing something?

Thanks a lot.

+1  A: 

You almost got it - the xhtml is actually selected="selected"

<option value="http://domain.com/&lt;?php print $k; ?>/" <?php if ($k == $_GET['t']) print 'selected="selected"'; ?>><?php print $v; ?></option>
adam
Thanks. But why I keep getting Notice: Undefined index: t and it won't work the way it expected to do.
Ahmad Fouad
This is correct, though your checkbox will be selected even if you don't use the right xhtml syntax.
Andrei Serdeliuc
+2  A: 

You get the undefined error because "t" is not in your $_GET request.

make sure you are calling the right url (i.e.: example.com/script.php?t=test)

Do make sure you are checking for the value (as others suggested) with isset($_GET['t'])

Andrei Serdeliuc
Oh I am stupid! i was trying to get the $_GET['t'] of parent document in the frame.. and the frame is in another page.. stupid me. Sorry :)
Ahmad Fouad
+1  A: 

Above what adam noted, are you testing by actually passing a "t=something" on the end? This code should get rid of the error by testing to see if t was set first:

<option value="http://domain.com/&lt;?php print $k; ?>/" <?php if (isset($_GET['t']) && $k == $_GET['t']) print 'selected="selected"'; ?>><?php print $v; ?></option>
Chad Birch
+1  A: 

You shouldn't use GET's to change form values on a page. You should use POST for that :)

This way, if anyone crawls your page, they won't have 10 times the same page with just the select box changed :)

SchizoDuckie