Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Server Side Programming > PHP Programming
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 10-13-2009, 11:56 AM
  #1
minus273
Warrior (Level 8)
 
minus273's Avatar
 
Join Date: Nov 2003
Location: UK
Posts: 71
iTrader: (0)
minus273 is on a distinguished road
PHP variables in SELECT options

Hi there,

I am building a form that contains select options; all is well and when the form is submitted the values come across correctly to the recipient.

However, when the form picks up an error like an invalid email address (and reloads itself) these values are reset.

The normal (static) input values are still populated, but the select boxes reset to 'none'.

How can I ensure that these values remain as the user has selected and they don't have to fill the whole form out again?

All variables are declared correctly before the form is encountered.

Here is my normal input line:

PHP Code:
<input type="text" size="17" tabindex="1" id="name" name="name" value="<?=$name;?>" />
And here is my select box:

PHP Code:
select id="select1" name="select1" class="dropper" value="<?=$select1;?>">
<option id="none" name="none" value="none">None</option>
<option id="main" name="main" value="option1">Option1</option>
<option id="main" name="main" value="option2">Option2</option>
<option id="main" name="main" value="option3">Option3</option>
</select>
Many thanks in advance, am I missing something REALLY obvious?
__________________
Blaze Concepts - Web Design & Development, Consultancy, Hosting

"Never be afraid to do something new. Remember, amateurs built the Ark; professionals built the Titanic."
minus273 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-13-2009, 12:07 PM
  #2
Vege
Super Deity (Level 18)
 
Join Date: Sep 2004
Location: Finland
Posts: 3,410
iTrader: (0)
Vege is just really niceVege is just really niceVege is just really niceVege is just really nice
Quote:
<option selected="selected" value=option1">Option1</option>
select tag don't have value itself.
Vege is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-13-2009, 12:30 PM
  #3
minus273
Warrior (Level 8)
 
minus273's Avatar
 
Join Date: Nov 2003
Location: UK
Posts: 71
iTrader: (0)
minus273 is on a distinguished road
Vege, thanks for your reply.

True, but removing it still does not help at all.
__________________
Blaze Concepts - Web Design & Development, Consultancy, Hosting

"Never be afraid to do something new. Remember, amateurs built the Ark; professionals built the Titanic."
minus273 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-13-2009, 01:08 PM
  #4
Vege
Super Deity (Level 18)
 
Join Date: Sep 2004
Location: Finland
Posts: 3,410
iTrader: (0)
Vege is just really niceVege is just really niceVege is just really niceVege is just really nice
you also need to add the selected="selected" on the correct option

Comments on this post
Horus_Kol disagrees: stating the obvious doesn't help things
Vege is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-13-2009, 01:21 PM
  #5
Pegasus
Extremely Flighty Admin
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 21,472
iTrader: (0)
Pegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to all
Silly question, but aren't id's and names supposed to be unique per page? I'm seeing 3 "main" ids.
__________________


Decaf is the root of all evil...
HTMLForums Awards 2008
Pegasus is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-13-2009, 01:33 PM
  #6
minus273
Warrior (Level 8)
 
minus273's Avatar
 
Join Date: Nov 2003
Location: UK
Posts: 71
iTrader: (0)
minus273 is on a distinguished road
Yes you are correct Pegasus, they are actually different, I just substituted them for this forum.

This is driving me insane.
__________________
Blaze Concepts - Web Design & Development, Consultancy, Hosting

"Never be afraid to do something new. Remember, amateurs built the Ark; professionals built the Titanic."

Last edited by minus273 : 10-13-2009 at 01:34 PM. Reason: Typo
minus273 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-14-2009, 05:58 PM
  #7
uze
Aspirant (Level 2)
 
Join Date: Jul 2008
Posts: 14
iTrader: (0)
uze is on a distinguished road
The following code may be 'OTT' but it's future proof IMO:

Code:
<?php

// Set Choice variable
$choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : 'none';

// Check value of param against $thisValue
function checkSelected($thisValue) {
	if($choice == $thisValue) {
		return 'selected="selected"';	
	}
}

// Our drop down options
$options = array(
	'none' => 'None',
	'option1' => 'option1',
	'option2' => 'option2',
	'option3' => 'option3'
);

// Loop through each option assigning the html to $return
foreach($options as $key=>$value) {
	$return .= '<option value="'.$key.'" '.checkSelected($key).'>'.$value.'</option>';	
}

?>

<select id="select1" name="select1" class="dropper" value="<?=$select1;?>"> 
<?php 
	// We could just loop here but I prefer to output the $return variable
	echo($return); 
?>
</select>
In order to add more options, you just need to add more values to the $options array.

I removed the name & id on each option as I couldn't see the point in them being there.

In order for the above code to work, you need to capture the value from the select1 on your error page then pass it back via GET or POST as 'choice', the script will take care of the rest.

I hope this helps, if not, let me know the issues.

Comments on this post
Horus_Kol agrees: good solution
uze is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-14-2009, 09:51 PM
  #8
Horus_Kol
Mod of the Underlay
 
Horus_Kol's Avatar
 
Join Date: Jun 2002
Location: At a desk, hooked up and ready to rock
Posts: 17,242
iTrader: (0)
Horus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of light
only one thing with your code Uze - <select> elements don't take a value attribute... other than that - pretty close to what I'd do...
__________________
Personal Blog (and photos): HorusKol
Articles on Programming and Development (PHP/HTML/CSS, C/C++, more): RandomTweak

The great secret that no SEO agent wants you to hear: if you build your website using w3c accessibility guidelines and your content is written for people, you will do better for longer in search engines than any other method...
Horus_Kol is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-15-2009, 03:37 AM
  #9
Vege
Super Deity (Level 18)
 
Join Date: Sep 2004
Location: Finland
Posts: 3,410
iTrader: (0)
Vege is just really niceVege is just really niceVege is just really niceVege is just really nice
Quote:
Horus_Kol disagrees: stating the obvious doesn't help things
But AFAIK when it's the only thing to do to to ensure crossbrowser combability i think it's in place to mention.
After page refresh you need to change the selected="selected" by yourself as some browsers will remember the selected value and some will not.

He was obviously missing this.
Vege is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-15-2009, 07:12 PM
  #10
Horus_Kol
Mod of the Underlay
 
Horus_Kol's Avatar
 
Join Date: Jun 2002
Location: At a desk, hooked up and ready to rock
Posts: 17,242
iTrader: (0)
Horus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of light
Quote:
Originally Posted by Vege View Post
But AFAIK when it's the only thing to do to to ensure crossbrowser combability i think it's in place to mention.
After page refresh you need to change the selected="selected" by yourself as some browsers will remember the selected value and some will not.

He was obviously missing this.
maybe - but without showing a PHP code snippet for how to determine which of the options should be selected, your statement is next to useless...
__________________
Personal Blog (and photos): HorusKol
Articles on Programming and Development (PHP/HTML/CSS, C/C++, more): RandomTweak

The great secret that no SEO agent wants you to hear: if you build your website using w3c accessibility guidelines and your content is written for people, you will do better for longer in search engines than any other method...
Horus_Kol is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-16-2009, 12:16 PM
  #11
Vege
Super Deity (Level 18)
 
Join Date: Sep 2004
Location: Finland
Posts: 3,410
iTrader: (0)
Vege is just really niceVege is just really niceVege is just really niceVege is just really nice
Quote:
Originally Posted by Horus_Kol View Post
maybe - but without showing a PHP code snippet for how to determine which of the options should be selected, your statement is next to useless...
So exact statement what needs to be done is useless and only amount of lines i wrote or code i produce matters?
Vege is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-17-2009, 12:38 AM
  #12
Horus_Kol
Mod of the Underlay
 
Horus_Kol's Avatar
 
Join Date: Jun 2002
Location: At a desk, hooked up and ready to rock
Posts: 17,242
iTrader: (0)
Horus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of light
Quote:
Originally Posted by Vege View Post
So exact statement what needs to be done is useless and only amount of lines i wrote or code i produce matters?
no - but when that exact statement isn't supported by any suggestions on how to do the thing that you say should be done, then that is useless.

now, please, drop this off-topic conversation...
__________________
Personal Blog (and photos): HorusKol
Articles on Programming and Development (PHP/HTML/CSS, C/C++, more): RandomTweak

The great secret that no SEO agent wants you to hear: if you build your website using w3c accessibility guidelines and your content is written for people, you will do better for longer in search engines than any other method...
Horus_Kol is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-17-2009, 01:00 AM
  #13
Pegasus
Extremely Flighty Admin
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 21,472
iTrader: (0)
Pegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to all
Okay, gentlemen, back in your corners so you can help out an old woman trying to learn PHP and other such mind-benders at 11 at night.

Let me see if I understand the code correctly. Option can have a value, but select can't. Would it not work out with this code:

<select id="select1" name="select1" class="dropper" value="<?=$select1;?>">

To have the value replaced with selected?

<select id="select1" name="select1" class="dropper" selected="<?=$select1;?>">
__________________


Decaf is the root of all evil...
HTMLForums Awards 2008
Pegasus is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-17-2009, 06:34 AM
  #14
Vege
Super Deity (Level 18)
 
Join Date: Sep 2004
Location: Finland
Posts: 3,410
iTrader: (0)
Vege is just really niceVege is just really niceVege is just really niceVege is just really nice
PHP Code:
select id="select1" name="select1" class="dropper" >
<option <?php echo ($select1=="none") ?" selected=\"selected\" ":""?>  value="none">None</option>
<option <?php echo ($select1=="option1") ?" selected=\"selected\" ":""?> value="option1">Option1</option>
<option <?php echo ($select1=="option2") ?" selected=\"selected\" ":""?>  value="option2">Option2</option>
<option <?php echo ($select1=="optio3") ?" selected=\"selected\" ":""?>  value="option3">Option3</option>
</select>
Select don't have a value or selected property. Those are option propertys.
Name property belogns to select, not option.
And as peg stated no same ID:s

This should make it work, im done with this thread.

Comments on this post
Pegasus agrees: Thanks for clearing my confusion
Vege is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-17-2009, 06:44 AM
  #15
Pegasus
Extremely Flighty Admin
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 21,472
iTrader: (0)
Pegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to all
**Now** it makes sense. $select1 has to be in the options in order to be selected, not in the select part of the code. Thanks!
__________________


Decaf is the root of all evil...
HTMLForums Awards 2008
Pegasus is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart
 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 04:05 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.