1

I have this form with about 9 drop down menus. I am trying to retain the selected values after submission. My code works for 2 of those drop down menus which are not disabled, but not for the remaining 7 disabled ones. Is there a different way to write the code for those?

1 of the 2 drop downs that isn't disabled:-

<?php
$sql = "SELECT * FROM tablename1 ";
$result = mysqli_query($conn, $sql);
echo "<select name = 'account_name' id = 'idaname'>"; 
echo "<option value = ''>";
while($row = mysqli_fetch_array($result)){
    $selected = (isset($_POST['account_name']) && $_POST['account_name'] == $row['account_name']) ? 'selected = "selected"' :'';
    ?>
    <option <?php echo $selected; ?> value = "<?php echo $row['account_name']; ?>"> <?php echo $row['account_name']; ?> </option>
    <?php } ?>
    </select>

2 of the 7 disabled drop downs :-

<?php
$sql = "SELECT rsm_val FROM tablename2 ";
$result = mysqli_query($conn, $sql);
echo "<select name = 'rsm_val' id = 'rsm_val' onchange = 'runrsm()' disabled >"; 
echo "<option value = '" .$rsm_val . "'>" . $rsm_val . "</option>";
while($row = mysqli_fetch_array($result)){
    $selected = (isset($_POST['rsm_val']) && $_POST['rsm_val'] == $row['rsm_val']) ? 'selected = "selected"' :'';
    ?>
    <option <?php echo $selected; ?> value = "<?php echo $row['rsm_val']; ?>"> <?php echo $row['rsm_val']; ?> </option>
    <?php } ?>
</select>



<select name = 'boost_app' id = 'boost_app' onchange = 'runboost()' disabled>
<?php echo "<option value = '". $boost_app . "'>" . $boost_app .  "</option>"; ?>
<option <?php if($_POST['boost_app'] == 'Yes'){?> selected="true" <?php }; ?> value = "Yes">Yes</option>
<option <?php if($_POST['boost_app'] == 'No') {?> selected="true" <?php }; ?> value = "No">No</option>
</select>
abc
  • 45
  • 1
  • 6
  • 1
    Possible duplicate of [Retain field values after submit](https://stackoverflow.com/questions/26706179/retain-field-values-after-submit) – Obsidian Age Oct 16 '17 at 20:58
  • 1
    Why don't you try `readonly` instead of `disabled`, huh? – mega6382 Oct 16 '17 at 20:59
  • Possible duplicate of [HTML form readonly SELECT tag/input](https://stackoverflow.com/questions/368813/html-form-readonly-select-tag-input) – Patrick Q Oct 16 '17 at 21:08

3 Answers3

1

You can use readonly instead:

<select name = 'boost_app' id = 'boost_app' onchange = 'runboost()' readonly>
<?php echo "<option value = '". $boost_app . "'>" . $boost_app .  "</option>"; ?>
<option <?php if($_POST['boost_app'] == 'Yes'){?> selected="true" <?php }; ?> value = "Yes">Yes</option>
<option <?php if($_POST['boost_app'] == 'No') {?> selected="true" <?php }; ?> value = "No">No</option>
</select>

This will not allow user to edit values, and will send the data with form submission too. Learn more about it.

Edit: And if readonly doesn't work for the <select> tag, then you can disable all options except the selected 1. Like:

<select name = 'boost_app' id = 'boost_app' onchange = 'runboost()'>
<?php echo "<option value = '". $boost_app . "' disabled>" . $boost_app .  "</option>"; ?>
<option <?php if($_POST['boost_app'] == 'Yes'){?> selected="true" <?php }else{echo "disabled";} ?> value = "Yes">Yes</option>
<option <?php if($_POST['boost_app'] == 'No') {?> selected="true" <?php }else{echo "disabled";} ?> value = "No">No</option>
</select>

Or you can have a hidden input with the same name, Like:

<input type="hidden" name="boost_app" value="<?=$_POST['boost_app']?>">
<select name = 'boost_app' id = 'boost_app' onchange = 'runboost()' disabled>
<?php echo "<option value = '". $boost_app . "'>" . $boost_app .  "</option>"; ?>
<option <?php if($_POST['boost_app'] == 'Yes'){?> selected="true" <?php }; ?> value = "Yes">Yes</option>
<option <?php if($_POST['boost_app'] == 'No') {?> selected="true" <?php }; ?> value = "No">No</option>
</select>
mega6382
  • 9,211
  • 17
  • 48
  • 69
  • I don't think you can have readonly for select menu. i just tried it and the user can change selections in the drop down – abc Oct 16 '17 at 21:04
  • Looking at the hidden input fields, I have some javascript, that under certain situations will turn the disabled attribute to be false(enabled). If so, I was planning to have the updated selection in that drop down, but now my update query isn't working. I'm not sure how we can do that with the same name and id for the hidden field – abc Oct 16 '17 at 21:29
  • @abc you can try something like this `$('input[name=boost_app]').attr("disabled", true);` to disable the hidden input when select is enabled and vice versa. – mega6382 Oct 17 '17 at 07:19
1

If a dropdown is disabled, the value isn't sent to the server. You can add a hidden input with the same name and value.

<input type="hidden" name="boost_app" value="<?php echo $_POST['boost_app'];?>">
<select name = 'boost_app' id = 'boost_app' onchange = 'runboost()' disabled>
<?php echo "<option value = '". $boost_app . "'>" . $boost_app .  "</option>"; ?>
<option <?php if($_POST['boost_app'] == 'Yes'){?> selected="true" <?php }; ?> value = "Yes">Yes</option>
<option <?php if($_POST['boost_app'] == 'No') {?> selected="true" <?php }; ?> value = "No">No</option>
</select>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Okay I have some javascript, that under certain situations will turn the disabled attribute to be false(enabled). If it is enabled, I was planning to have the updated selection in that drop down, but now my update query is failing. I'm not sure how we can do that with the same name and id for the hidden field – abc Oct 16 '17 at 21:28
  • You should remove or rename the hidden input when you enable the dropdown. – Barmar Oct 16 '17 at 21:31
  • So you're saying I need to have 2 hidden inputs? Because I already have 1 like this ---> – abc Oct 16 '17 at 21:42
  • No, just one hidden input. But if you enable the dropdown, you need to get rid of the hidden input, so you don't have two inputs with the same name. – Barmar Oct 16 '17 at 21:46
  • Actually, I'm surprised it doesn't work. If there are two inputs with the same name, I think it uses the last one, so the hidden input should be ignored when the dropdown is enabled. – Barmar Oct 16 '17 at 21:46
  • Make sure they don't have the same `id`, though. IDs have to be unique. – Barmar Oct 16 '17 at 21:47
  • Yeah they don't have the same ID as I'm using javascript as well. But I still don't understand how can I get rid of a hidden input? – abc Oct 16 '17 at 21:49
  • Change its name: `document.getElementById("boost_app3").name = "ignore_boost_app";` – Barmar Oct 16 '17 at 21:52
0

I use this to make the dropdown displayed as if it 'disabled'

$(#boost_app).css({"background-color":"rgb(238, 238, 238)", "pointer-events":"none"}).keydown(false)

mch
  • 1