douxian1923
2015-01-12 07:31
浏览 207
已采纳

Laravel获取请求传递输入数据

I'm trying to pass some input data from a get request to a view in my controller. I was wondering if this is possible or am I doing it incorrectly? The data I want to pass is from a text field within a form as below:

    <form id='query_form' role='form' action='{{ URL::route('getUserbase'); }}' method="get">

                <div class='form-group'>
                    <div class='input-group'>   
                        <input type='text' id='query-bar' required class='form-control' placeholder='Enter search query..' name='entered_query' />                    
                        <span class='input-group-btn'>
                            <input type='submit' class='btn btn-default btn-color' value='Go' />
                        </span>      
                    </div>
                </div>
</form>

I am submitting it with ajax:

function standardGet($action)
{
    $.get($action, function(data, status)
    {
        $("body").html(data);
    });   
}

   $('#query_form').submit(function(event)
   {
       event.preventDefault();
       var action   =   $(this).attr('action');
       standardGet(action);
   });

In my laravel controller:

public function getUserBase()
{
    if(Request::ajax())
        return View::make('admin.plugins.userbase')->with('search_request', Request::get('entered_query'));

    else
     return View::make('admin.plugins.userbase');
}

With this code Request::get('entered_query') is null, I've also tried Input::get('entered_query'). Any ideas?

EDIT: Route for getUserbase():

Route::get('/admin/panel/userbase', array('uses' => 'AdminController@getUserbase', 'as' => 'getUserbase'));

EDIT2: Also to clarify if i replace the value in with any value I can access it, I just can't access the input from the form: return View::make('admin.plugins.userbase')->with('search_request', "test");

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将一些输入数据从get请求传递到控制器中的视图。 我想知道这是否可能或我做错了吗? 我要传递的数据来自表单中的文本字段,如下所示:

 &lt; form id ='query_form'role ='form'action ='{{URL  ::路线( 'getUserbase');  }}'method =“get”&gt; 
 
&lt; div class ='form-group'&gt; 
&lt; div class ='input-group'&gt;  
&lt; input type ='text'id ='query-bar'required class ='form-control'placeholder ='Enter search query ..'name ='entered_query'/&gt;  
&lt; span class ='input-group-btn'&gt; 
&lt; input type ='submit'class ='btn btn-default btn-color'value ='Go'/&gt; 
&lt; / 跨度&GT;  
&lt; / div&gt; 
&lt; / div&gt; 
&lt; / form&gt; 
   
 
 

我使用ajax提交它:

  function standardGet($ action)
 {
 $ .get($ action,function(data,status)
 {
 $(“body”)。html(data); \  n});  
 
 
 $('#query_form')。submit(function(event)
 {
 event.preventDefault(); 
 var action = $(this).attr('action'); \  n standardGet(action); 
}); 
   
 
 

在我的laravel控制器中:

  public function getUserBase  ()
 {
 if if(request :: ajax())
返回View :: make('admin.plugins.userbase') - &gt; with('search_request',Request :: get('entered_query')  ); 
 
其他
返回View :: make('admin.plugins.userbase'); 
} 
   
 
 

使用此代码Request :: get('entered_query')为null,我也尝试过Input :: get('entered_query')。 任何想法?

编辑:getUserbase()的路由:

  Route :: get('/ admin / panel / userbase',  array('uses'=&gt;'AdminController @ getUserbase','as'=&gt;'getUserbase')); 
   
 
 

EDIT2: 另外澄清是否 我用我可以访问它的任何值替换值,我只是无法访问表单中的输入:return View :: make('admin.plugins.userbase') - &gt; with('search_request',“test “);

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dousha7904 2015-01-12 07:52
    已采纳

    You are not passing any data to your method. Try

    $('#query_form').submit(function(event)
       {
           event.preventDefault();
           var action   =   $(this).attr('action');
           var method   = 'GET';
           $.ajax({
              type: method,
              url:action,
              data: $('#query_form').serialize(),
              success: function() {
    
              }
           })
       });
    

    In your getUserbase method

    dd(Input::all());
    

    Edited::

    Pass what you wanted variable to your view like the following

    return View::make('admin.plugins.userbase', compact('search_request', Input::get('entered_query'));
    

    Check what you had passed variable is not Null in your view like the following

    @if(isset($search_request))
      {{ $search_request }} //is not NULL echo it
    @endif
    
    评论
    解决 无用
    打赏 举报
  • 查看更多回答(2条)

相关推荐 更多相似问题