Jump to content

Recommended Posts

<form name="statCompany" method="post" action="statCompanies.php">
      				<label>Sort by:</label>
                    
                    <select name='criteria'>
						<option value='nameAsc'>Name (Asc)</option>
                        <option value='nameDesc'>Name (Desc)</option>
  						<option value='ceoAsc'>CEO (Asc)</option>
  						<option value='ceoDesc'>CEO (Desc</option>
  						<option value='moneyAsc'>Money (Asc)</option>
  						<option value='moneyDesc'>Money (Desc)</option>
				</select>
                    
                	<label><button type="submit" name="sort" id="sort" value="Sort"> Sort  </button>
              	</form> 

 

How can I dynamically set one of these in the drop down list as the default? I.e. the top one is fine originally, but then after the user has selected the third one say and the page reloads, I want the third one to be the default selected then (suing PHP if possible).

Link to comment
https://forums.phpfreaks.com/topic/208736-php-listbox-default/
Share on other sites

Something like this:

 

<?php

$critera = $_REQUEST['criteria'];

?>

<form name="statCompany" method="post" action="statCompanies.php">
      				<label>Sort by:</label>
                    
                    <select name='criteria'>
						<option <?php if($criteria == "nameAsc") echo "selected"; ?> value='nameAsc'>Name (Asc)</option>
                        <option value='nameDesc'>Name (Desc)</option>
  						<option value='ceoAsc'>CEO (Asc)</option>
  						<option value='ceoDesc'>CEO (Desc</option>
  						<option value='moneyAsc'>Money (Asc)</option>
  						<option value='moneyDesc'>Money (Desc)</option>
				</select>
                    
                	<label><button type="submit" name="sort" id="sort" value="Sort"> Sort  </button>
              	</form> 

 

Look at the first one as an example and then add this, with the proper value for that option, it its place:

 

<?php if($criteria == "value") echo "selected"; ?> 

Link to comment
https://forums.phpfreaks.com/topic/208736-php-listbox-default/#findComment-1090517
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.