Re: Generating an option list with a default

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 01/25/2014 12:45 PM, Jennifer wrote:
Hello all,

	We have a PHP form with a static <option> list, where people can request a product sample.

http://www.superiorshelving.com/mfg/aigner/sample.php

	There are links on multiple pages that call this page.  I would like to send a different default option to that form from each of those pages so that the customer doesn't have to pop-up the list to choose it manually.  Of course, if they go to that form directly, there shouldn't be a default.

	What would be the best way to handle this?  I'm thinking to have code generate the list with the default undefined, and then have the links to that page pass a variable to the form to set a default.  Does that sound like it would work?

	Any other ideas are appreciated!

Thank you,
Jenni

	Superior Shelving Systems::::....
	http://www.SuperiorShelving.com

	The (Storage|Office|Home|Warehouse) Shelving Specialists
	Since 1984

Wire LAN Shelving:
http://www.superiorshelving.com/mfg/nexel/pages/wire-shelving-chrome.php



I would suggest having either an anchor or a button on the first page linking to the second page with the form.

So, on the first page, you would have something like this:

Universal way
<a href="sample.php?d=4">Go to Sample page</a>
or
<form action="sample.php" method="get">
<input type="hidden" name="d" value="4" />
<input type="submit" value="Got to Sample page" />
</form>

or an HTML5 method
<button formmethod="get" formaction="sample.php?d=4">Go to Sample page</button>


Then, once you get to generating the form -> select box options, do something like this. I assume you are looping through some sort of list to generate the options. In my example, I am using an ID instead of a label. But you could use a label instead of an ID, you would simply match it to the value of the option instead of the ID of the option.

<?php

... Get your options together into an array ...

$id = intval($_GET['d']);

foreach ( $options AS $k => $option ) {
  $k = htmlspecialchars($k);
  $option = htmlspecialchars($option);

  $sel = ( $id == $k ? ' selected="selected"' : '' );

  echo "\t<option value=\"{$k}\"{$sel}>{$option}</option>\n";
}

--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux