0

I try build input dynamic with button(Add Item). and I have combobox(1-6) for limit item input. I need disable combobox for human error when input data.

E.g User select max = 2(user will be add item 2 times), After user input value in product next user click (Add Item), then combobox will be disable (when user click add item), next repeated user input until limit.

This for html Max Item(combobox) :

<tr>
    <td>
        Max Item
    </td>
    <td>
        :
    </td>
    <td>
        <select name="max" id="maxitem">
            <?php
              for($i=1; $i<=6; $i++)
              {
                  echo "<option value=".$i.">".$i."</option>";
              }
            ?>
        </select>
    </td>
</tr>
<tr>
    <td>
        PRODUCT :
    </td>
    <td>
        <input type="text" name="product" value="">
    </td>
</tr>
<tr>
    <td>
        <table>
            <tr>
                <td><input type="button" id="ADD" value="Add Item"></td>
                <td><input type="reset" id="RESET" value="Reset"></td>
            </tr>
        </table>
    </td>
</tr>

And this for javascript :

function restFormOpts()
{
    if(isSet === isAllowed) {
        $("#ADD").attr("disabled",true);
        $("#maxitem").attr("disabled",false);
    }
    else {
        $("#ADD").attr("disabled",false);
        $("#maxitem").attr("disabled",true);
    }
}

This code already work well, but when combobox disable value will be gone too.

I cant post data $max = $_POST['max']; // THIS NULL IF COMBOBOX DISABLE

Some one have any idea???

Vilthering
  • 348
  • 3
  • 16
  • 1
    one idea - have a hidden field that is populated with the value from the combobox and use that hidden field in the form submission. Add an `onchange` event handler to the dropdown that sets the hidden field value – Professor Abronsius Feb 03 '16 at 08:23
  • @RamRaider can you give me example please? – Vilthering Feb 03 '16 at 08:27

2 Answers2

0

Create one hidden field which will have value selected from dropdown through onchange().

.
.
<tr>
  <td>
      Max Item
  </td>
  <td>
      :
  </td>
  <td>
    <select name="max" id="maxitem">
          <?php
        for($i=1; $i<=6; $i++) {
            echo "<option value=".$i.">".$i."</option>";
        }?>
    </select>
  </td>
    <input type='hidden' value="" id='maxValue' name='maxValue'>
</tr>
.
.
.

JS

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<script>

    $('#maxitem').change(function(){
        var maxValue= $('#maxitem').val();
        $('#maxValue').val(maxValue);
    });

    function restFormOpts()
    {
      if(isSet === isAllowed) {
          $("#ADD").attr("disabled",true);
          $("#maxitem").attr("disabled",false);
      }
      else {
          $("#ADD").attr("disabled",false);
          $("#maxitem").attr("disabled",true);
      }
    }

</script>

SubmitPage

<?php
echo $_POST['maxValue']; //Use this maxValue
?> 

For more details, click here HTML form readonly SELECT tag/input

Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
0

Use "readonly" instead of "disabled" in $("#maxitem").attr()

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77