Dynadot โ€” .com Transfer

Select "selected" auto-pick

NamecheapNamecheap
Watch

PoorDoggie

Soon to be RICHdoggie!VIP Member
Impact
18
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?

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! :lol: I hope you understand! :)

Thanks a lot
Tom
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
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?
 
0
•••
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.
 
0
•••
hey i have a question wts proxy??
 
0
•••
sana said:
hey i have a question wts proxy??
huh?!

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

PHP:
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"){
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
 
0
•••
slightly easier:
PHP:
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>
	    <option value="03"'; if ($_POST['name']== 03) echo "selected=selected";  print '>03</option>
</select';
 
0
•••
whyme953 said:
slightly easier:
PHP:
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>
	    <option value="03"'; if ($_POST['name']== 03) echo "selected=selected";  print '>03</option>
</select';
great :) thanks! :):):)
 
0
•••
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:
	$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">' : '>';
		echo $value . '</option>' . "\n";
	}

	echo '</select>';
 
0
•••
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.
 
0
•••
Icespadez said:
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:
	$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">' : '>';
		echo $value . '</option>' . "\n";
	}

	echo '</select>';

great idea! thanks alot.
i should've thought of that...
 
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
Appraise.net
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back