简体   繁体   中英

Get the id of selected option of select tag in symfony2 controller

I have a form with only one select tag. I want to get the id of the selected option tag in my controller when a user submit the form. My form is

 <div class="one_third">
        <form name="portfolios" action="{{ path('MunichInnovationGroupBundle_portfolio') }}" method="post" >
            <h2>Select your portfolio</h2>
            <p>Change the portfolio to be managed:<br />
                <select name="portfolio" style="width: 265px; height:28px;">
                    <option selected="selected" value="default">Switch Your Portfolio</option>
                    {% for portfolio in portfolios %}
                        <option id={{ portfolio.id }}>{{ portfolio.portfolioName }}</option>
                        {% endfor %}
                </select>
            </p>
            <p>
                <input style="margin-left:100px; height:26px; cursor:pointer; background-color: #2D9AD3 !important; border: 1px solid #067EBD; color: #FFFFFF; text-shadow: 0 -1px 0 #067EBD;" type="submit"class="button2 tooltip" value="Switch">
            </p> 
        </form>
    </div>

In my controller

if($request->getMethod() == 'POST'){
        $data = $request->request->all();
        //$portfolio_id = $data['portfolio_id'];
        var_dump($data);
        echo $data;
        exit();
    }

the var_dump($data) gives me something like this

array
 'portfolio' => string 'Umair Portfolio 1' (length=17)

Array

How can I get the id ?

Any suggestions?

Thanks in advance

您必须将属性ID更改为值

<option value={{ portfolio.id }}>{{ portfolio.portfolioName }}</option>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM