Hello all,
I am using twig in my slim app.
I have a dropdown menu that I want to select a particular index based on the value passed from the url request.
here is my dropdown code
<div class="field select-box">
<select name="plan" data-placeholder="Select Your Preferred Plan">
<option>Select One</option>
<option value="5000">Basic Plan - ₦5,000</option>
<option value="10000">Advanced Plan - ₦10,000</option>
<option value="20000">Professional Plan - ₦20,000</option>
</select>
<i class="fa fa-sort"></i>
{% if errors.plan%}
<span style="color: #ffffff;" class="help-block">{{ errors.plan| first }}</span>
{% endif %}
</div>
If I’m understanding you properly… you would likely want to send the packages to the view as well as the selectedPlan, then loop through the plans and check to see if the package’s ID matches the user’s selected package , something like this…
<select name="package">
<option>Select One</option>
{% for package in packages %}
<option value="{{ package.price }}"{% if package.id == selectedPlan %} selected="selected"{% endif %}>{{ package.name }}</option>
{% endfor %}
</select>
something close to that.
the packages are already in the view but I want to select a package by default depending on the value I get from my request param, that is if the param says “5000”, select the first dropdown, if the param says “10000”, select the second dropdown and so on.
Send the parameter to the view in whatever manner you are sending other data to your view and call it something like selectedPackage. (Or is that the part you need help with?) Then your view would look something like this.
<select name="plan" data-placeholder="Select Your Preferred Plan">
<option>Select One</option>
<option value="5000"{% if selectedPlan == 5000 %} selected="selected"{% endif %}>Basic Plan - ₦5,000</option>
<option value="10000"{% if selectedPlan == 10000 %} selected="selected"{% endif %}>Advanced Plan - ₦10,000</option>
<option value="20000"{% if selectedPlan == 20000 %} selected="selected"{% endif %}>Professional Plan - ₦20,000</option>
</select>
To make debugging easier, I declared selectedPlan as a local twig variable in my twig file with a value of 10000.
You can see it in my last code post.
It still does not work.