Display the selected value in dropdown without using javascript

Display the selected value in dropdown without using javascript

July 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display the selected value in dropdown without using javascript

How to Display the Selected Value when an Option is Selected in a Dropdown Menu in PHTML Without Using JavaScript

As a developer working with Magento, you might find yourself needing to display the selected value from a dropdown menu directly below or next to the dropdown itself, without relying on JavaScript. This can be particularly useful for form validation or user feedback within your PHP templates (PHTML files).

The provided code snippet is a good starting point, but it has some key issues that prevent it from working correctly:

  1. The form action URL is not properly set.
  2. The selected option’s value is not being passed to the form.
  3. The echo statement for displaying the selected value is placed outside of the form tags.

Let me walk you through how to resolve these issues and achieve the desired functionality.

Step 1: Set the Form Action URL Properly

To set the form action URL correctly, we need to use the Magento helper function getUrl(). This ensures that the correct base URL for your site is used. Here’s the corrected code:

<?php echo $this->getUrl('customer/products/simpleproduct') ?>

Step 2: Pass the Selected Option Value to the Form

To pass the selected option value to the form, we need to modify the select element and add a name attribute. The name attribute should match the variable $a used in the echo statement. Here’s how to do this:

<select id="sizeoption" name="sizeoption">
  <option value="1">Small</option>
  <option value="2">Medium</option>
  <option value="3">Large</option>
</select>

Step 3: Place the Echo Statement Inside the Form Tags

To display the selected option value properly, we need to place the echo statement inside the form tags. This ensures that the value is displayed directly below or next to the dropdown menu. Here’s the final code:

<form action="<?php echo $this->getUrl('customer/products/simpleproduct') ?>" enctype="multipart/form-data" method="post" id="form-customer-product-new">
  <table class="tableposition">
    <tr>
      <td>
        <select id="sizeoption" name="sizeoption">
          <option value="1">Small</option>
          <option value="2">Medium</option>
          <option value="3">Large</option>
        </select>
        <?php
        $a = $_POST['sizeoption'];
        echo 'Selected Option: ', $a;
        ?>
      </td>
    </tr>
  </table>
</form>

Now, when you select an option from the dropdown menu, the selected value will be displayed directly below the dropdown.

Conclusion

By carefully setting the form action URL using the getUrl() helper function and correctly passing the selected option value to the form with a matching name attribute, we can successfully display the selected value without relying on JavaScript. This approach allows us to maintain clean separation between our PHP logic and client-side scripting.

For more advanced techniques in handling form data and validation in Magento, I recommend checking out the Laravel Company’s guide on Magento Form Handling. It provides a comprehensive overview of best practices for working with forms within Magento templates.

Catalog Themes Customers Errors

Our network