0
 <select name="courses" id="coursese" title="name"  class="input-text" type="text" data-validate="{required:true}">
  <option value="php">html</option>
  <option value="html">php</option>
  </select>
        

instead of a static option. How to give dynamic value as an option by calling PHP file.

1 Answer 1

0

.phtml file :-

<?php $customOption = $block->toOptionArray(); ?>
<select id="custom_option" name="custom_option" title="<?= $block->escapeHtmlAttr(__('Custom Options')) ?>" class="select"
                    required>
       <option value=""><?= $block->escapeHtml(__('Please Select')) ?></option>
            <?php foreach ($customOption as $option) { ?>
                    <?php if (!empty($option['value'])) { ?>
                        <option value="<?php echo $option['value'] ?>"><?= $block->escapeHtml(__($option['label'])) ?></option>
                    <?php } ?>
            <?php } ?>
</select>

block file :-

public function toOptionArray()
{
    $this->_options = [];
    $this->_options = [
        ['label' => __('Option 1'), 'value'=>'option_1'],
        ['label' => __('Option 2'), 'value'=>'option_2']
    ];

    return $this->_options;
}

if you want to add a dynamic option then you need to add foreach in toOptionArray() function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.