FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP GET... select an option on the page





ftv_flung
Hey,

I'm trying to do something where the URL goes:

http://www.mysite.com/contact?query=account

And when the page loads, the option for account is selected...

So it would find <option value="account"> on the page from a PHP GET ??? I think... can anyone help me please?

Thanks
Star Wars Fanatic
You would have to check to see if the get variable is set for that option and then display it yourself. There is no way (at least that I know of) for it to automatically change it.
You will have to use an if else or a select statement. Which one is up to you.
sonam
I am not sure what do you need exactly but if you want to change value in option and if you are using all the time same id (query) then you can try something like that:
Code:
<?php
$query = $_POST['query'];
$option = "<option value=\"$query\"> ";
?>


Now, you need just to echo in your form $option.

Sonam
James007
Code:

function is_selected($selected_argument) {

$get_argument = $_GET['query'];
if ($get_argument == $selected_argument) {
$return = ' SELECTED';
} else {
$return = '';
}

return $return;
}

print '<select>';
print '<option value="blah"'.is_selected('blah').'>Blah</option>';
print '<option value="account"'.is_selected('account').'>Account</option>';
print '<option value="other"'.is_selected('other').'>Other option</option>';
print '</select>;



It is probably quite a noobish way to do it, but it works...
ftv_flung
Thanks for the quick replies!! I'll try out those and get back about it soon XD
mathiaus
I tidied James' code up a little.

Code:
<?php
function is_selected($arg) {
   if (isset($_GET['query']) && $arg == $_GET['query']) {
      echo ' selected="selected"';
   }
}
?>

<select>
<option value="blah"<?php is_selected('blah'); ?>>Blah</option>
<option value="account"<?php is_selected('account'); ?>>Account</option>
<option value="other"<?php is_selected('other'); ?>>Other option</option>
</select>
James007
You should always listen to Mathiaus, he is the master.

Although, if you use php to generate the options too, it might be easier to use my version. (Naughty me).
mathiaus
I'm not the master, just better than James Razz (Only joking)

If you were to generate the options in PHP, you would be more likely to store the values in an array and loop through to create the options. For only a few values though, it's not worth it and it starts getting overly complicated when you want option groups etc.
ftv_flung
Thanks again guys Very Happy

I'll test those out later today Cool

Fantastic! It works - thanks for helping Smile
Related topics
How to create a dynamic PHP website.
The new Account page and Request Mod
[*READ THIS FIRST!*] Commonly asked questions
[Resolved] Object Orientated PHP errors
VBulletin Portal Installing
Changing Styles on the Fly! - DynamicDrive
Site Members' System
mobile programmers codes
This Page Was Last Modified On -------
Windows and Linux bootable from pendrive
Where do i get a Free PHP supported Home page
PHP variable problem
php get current page name
PHP Dynamic tree-grid
how to underline a select option?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.