Jump to content

[SOLVED] Reload selected option after form reloads


thesaleboat

Recommended Posts

How can I get this to go back to option the user selected after the form reloads?

 

This is the only thing that my form currently does not reload when the user has not filled in all fields, which causes the form to show errors and reload with the information the user has already filled in.

 

<span class="mainTextNoIndent"><strong>*Security Clearance:</strong><br/>
List current security clearance.</span><br/>
<select name="requiredSecurityClearance" id="requiredSecurityClearance" value="<?php echo isset($_POST['requiredSecurityClearance']) ? $_POST['requiredSecurityClearance'] : '';?>" size="1">
  <option>None</option> 
  <option>Confidential</option> 
  <option>Secret</option> 
  <option>Top Secret</option> 
  <option>SCI</option> 
</select>

It's not the select tag that takes the value option it's the options themselves. For example it should be:

 

<select name="select_box">

<option value="1">1</option>

<option value="2">2</option>

</select>

 

One way of getting the select box to show the selected option would be to turn the whole select part into a string and then use str_replace to add a selected="selected"  attribute.

 

<?php

$select = '<select name="select_box">
		<option value="1">1</option>
		<option value="2">2</option>
	</select>';

$selected_value = '2';

$select = str_replace($selected_value . '">', $selected_value . '" selected="selected">', $select);

echo $select;
?>

Ok, I tried doing this, and it no longer shows up on my form, im sure im just missing something small here.

 

 <?php 
$requiredSecurityClearance= '<select name="requiredSecurityClearance" id="requiredSecurityClearance" size="1">
    <option value ="None">None</option> 
    <option value ="Confidential">Confidential</option> 
    <option value ="Secret">Secret</option> 
    <option value ="TopSecret">Top Secret</option> 
    <option value ="SCI">SCI</option> 
  </select>'
  ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.