NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page <select> "selected" auto-pick

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 06-16-2005, 02:32 PM THREAD STARTER               #1 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



<select> "selected" auto-pick


Sorry about the vagueness of the title, but I have a problem.

I have a form that includes <select> boxes. Now when a user submits this form it goes to a php page where it either sends an email to me includng the values in the form, OR it reports an error to the user and display's the form again. Now, unlike normal text inputs (<input type="text">) the "value" attribute cannot be used here (as in <select value="<?php echo $hosting; ?>">) I assume. So is there an easy way to have the chosen option displayed automatically?
????: NamePros.com http://www.namepros.com/programming/99203-select-selected-auto-pick.html

Normally you would put <option selected value="..">...</option>, but I hoped I didn't have to create php functions to output <select> boxes with the right option selected.

Please say there is an easy way! I hope you understand!

Thanks a lot
Tom
PoorDoggie is offline  
Old 06-16-2005, 04:11 PM   #2 (permalink)
Senior Member
 
Scott's Avatar
Join Date: Jun 2003
Location: UK
Posts: 3,547
Scott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond repute
 

Member of the Month
February 2005

I don't think there's any other way other than using "selected" unless you use javascript to go back, which might work.

<html>
<head>
<title>Go back!</title>
</head>
<body onload="javascript:history.go(-1)">
<script language="Javascript">
<!--
alert("Error message...blah blah blah. Press OK to go back.");
//-->
</script>
</body>
</html>

Maybe?
Scott is offline  
Old 06-16-2005, 05:34 PM   #3 (permalink)
NamePros Regular
Join Date: Oct 2003
Location: Sweden
Posts: 395
cerebus has a spectacular aura aboutcerebus has a spectacular aura about
 


Animal Rescue Wildlife Animal Cruelty
Either you put in an conditional clause at every option, or you store the option values in an array/db and loop through it with a conditional to write out selected. "selected" is THE way to mark an option as selected.
cerebus is offline  
Old 06-16-2005, 05:36 PM   #4 (permalink)
NamePros Member
Join Date: May 2005
Posts: 28
sana is an unknown quantity at this point
 



hey i have a question wts proxy??
sana is offline  
Old 06-19-2005, 03:53 PM THREAD STARTER               #5 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



Originally Posted by sana
hey i have a question wts proxy??
huh?!

anyway - so I would have to do something like this:

PHP Code:
echo "<select name=\"name\">";
if(
$_POST['name'] = "option1"){
echo 
"<option value=\"option1\" selected>Option 1</option>";
}
else{
echo 
"<option value=\"option1\">Option 1</option>";
}

if(
$_POST['name'] = "option2"){
????: NamePros.com http://www.namepros.com/showthread.php?t=99203
echo 
"<option value=\"option2\" selected>Option 2</option>";
}
else{
echo 
"<option value=\"option2\">Option 2</option>";
}

echo 
"</select>"
is there an easier way of doing this?

Thanks
Tom
PoorDoggie is offline  
Old 06-19-2005, 08:09 PM   #6 (permalink)
NamePros Member
 
whyme953's Avatar
Join Date: Jun 2004
Posts: 92
whyme953 is an unknown quantity at this point
 



slightly easier:
PHP Code:
print '<select name="name">
        <option value="01"'
; if ($_POST['name']== 01) echo "selected=selected";  print '>01</option>
        <option value="02"'
; if ($_POST['name']== 02) echo "selected=selected";  print '>02</option>
????: NamePros.com http://www.namepros.com/showthread.php?t=99203
        <option value="03"'
; if ($_POST['name']== 03) echo "selected=selected";  print '>03</option>
</select'

__________________
Bais Menachem
whyme953 is offline  
Old 06-23-2005, 04:52 AM THREAD STARTER               #7 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



Originally Posted by whyme953
slightly easier:
PHP Code:
print '<select name="name">
        <option value="01"'
; if ($_POST['name']== 01) echo "selected=selected";  print '>01</option>
        <option value="02"'
; if ($_POST['name']== 02) echo "selected=selected";  print '>02</option>
????: NamePros.com http://www.namepros.com/showthread.php?t=99203
        <option value="03"'
; if ($_POST['name']== 03) echo "selected=selected";  print '>03</option>
</select'

great thanks!
PoorDoggie is offline  
Old 06-23-2005, 09:57 AM   #8 (permalink)
NamePros Member
 
Icespadez's Avatar
Join Date: Apr 2005
Location: Boston, MA
Posts: 26
Icespadez is an unknown quantity at this point
 



A little more easier to read version. If you want to add another option to the select you just add another value to the select_values array.

PHP Code:
    $select_values = array(
            
'01' => 'Option One',
            
'02' => 'Option Two',
            
'03' => 'Option Three'
        
);
????: NamePros.com http://www.namepros.com/showthread.php?t=99203

    echo 
'<select name="name">' "\n";

    foreach (
$select_values as $key => $value) {
        echo 
'<option value="' $key '"';
        echo (
$_POST['name'] == $key) ? ' selected="selected">' '>';
        echo 
$value '</option>' "\n";
    }

    echo 
'</select>'
Icespadez is offline  
Old 06-23-2005, 10:59 PM   #9 (permalink)
NamePros Member
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 61
Kupo? is an unknown quantity at this point
 



Heh. Why not just use javascript validation? If there's an error on submit then alert the user, the page won't post and you won't have to refill any values. If it validates then it will goto the next page and send you an email.
__________________

Kupo?
Kupo? is offline  
Old 06-24-2005, 11:37 AM   #10 (permalink)
NamePros Member
 
whyme953's Avatar
Join Date: Jun 2004
Posts: 92
whyme953 is an unknown quantity at this point
 



Originally Posted by Icespadez
A little more easier to read version. If you want to add another option to the select you just add another value to the select_values array.

PHP Code:
    $select_values = array(
            
'01' => 'Option One',
            
'02' => 'Option Two',
            
'03' => 'Option Three'
        
);

    echo 
'<select name="name">' "\n";

    foreach (
$select_values as $key => $value) {
        echo 
'<option value="' $key '"';
        echo (
$_POST['name'] == $key) ? ' selected="selected">' '>';
????: NamePros.com http://www.namepros.com/showthread.php?t=99203
        echo 
$value '</option>' "\n";
????: NamePros.com http://www.namepros.com/showthread.php?t=99203
    }

    echo 
'</select>'
great idea! thanks alot.
i should've thought of that...
__________________
Bais Menachem
whyme953 is offline  
Closed Thread


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto resize flash with html in php zander Programming 8 06-15-2005 12:40 PM
GTA / Grand Theft Auto network wants you! Ringr For Sale / Advertising Board 0 05-17-2005 08:37 PM
Buying links from PR5+ auto / sports sites mr-x For Sale / Advertising Board 0 01-17-2005 10:00 AM

Liquid Web Smart Servers  
All times are GMT -7. The time now is 09:15 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger