简体   繁体   English

Symfony2表单:如何获取url中的提交值?

[英]Symfony2 form : how to get the submited value in the url?

what I need is "onSubmit" to get the "query" value, and put it in the "_search_all" route, normally this would look like => {{ path('_search_all', {'slug': query_val} ) }} this way I can get a route like this in my browser localhost/search-all/symfony2 我需要的是“ onSubmit”来获取“查询”值,并将其放在“ _search_all”路由中,通常这看起来像是=> {{ path('_search_all', {'slug': query_val} ) }}这样,我可以在我的浏览器localhost/search-all/symfony2获得这样的路由

The current form 当前形式

    <form class="searchbar" method="POST" action="{{ path('_search_all') }}">
        <input name="submit" type="submit" value="Search"/>
        <span>
         <input type="text" name="query" required/>
        </span>
    </form>

Solutions so far: 到目前为止的解决方案:

  1. method="GET" however this will create the "ugly" looking url method="GET"但是这将创建看起来“丑陋”的网址
  2. use the friendsofsymfony/jsrouting-bundle 使用friendsofsymfony/jsrouting-bundle

any inputs are welcome 欢迎任何输入

You need to use onSubmit event and set a javascript variable which contains the path; 您需要使用onSubmit事件并设置一个包含路径的javascript变量; as sample try something like: 作为示例尝试类似:

var frm_action = "{{ path('_search_all', {'keyword' : ''}) }}";
$(document).ready(function() {
   $('#myForm').attr('action', frm_action + $('input[name="query"]').val());
});

and your need to add the ID to your form: 并且您需要将ID添加到表单中:

<form class="searchbar" method="POST" id="myForm" action="" >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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