簡體   English   中英

將隱藏的輸入從表單發送到Laravel中的控制器

[英]Sending a hidden input from a form to a controller in Laravel

我有一個

建立表格

{!! Form::open(array('url' => '/cpe/store', 'class' => 'form-horizontal', 'role' =>'form')) !!}

              <h3 class="nomargin">Create CPE </h3>

              <div class="mb10">
                  <label class="control-label">Account ID </label>
                  <input type="text" class="form-control" name="account_id" value="{{$account->account_id}}">
              </div>


              <div class="mb10">
                  <label class="control-label">Max Up</label>
                  <input type="text" class="form-control" name="max_up" value="30000">
              </div>

              <div class="mb10">
                  <label class="control-label">Max Down</label>
                  <input type="text" class="form-control" name="max_down" value="50000" >
              </div>



              <a href="/account/" class="btn btn-danger ">Go back to account </a>

              {!! Form::hidden('$id', $account->account_id )!!}

              <button type="submit" class="btn btn-success ">Create</button>


            {!! Form::close();!!}

存儲功能

public function store() {

        $inputs = Input::all();
        unset($inputs['_token']);


        $cpe_mac = $inputs['mac1'].$inputs['mac2'].$inputs['mac3'].$inputs['mac4'].$inputs['mac5'].$inputs['mac6'];

        $cpe = [];

        $cpe['cpe_mac'] = $cpe_mac;
        $cpe['bandwidth_max_up'] = (int)$inputs['max_up'];
        $cpe['bandwidth_max_down'] = (int)$inputs['max_down'];

        $json = json_encode($cpe);

        try {

            $url = 'http://172.16.139.129:1234/vse/vcpe';
            $ch = curl_init($url);

            if (FALSE === $ch)
                throw new Exception('failed to initialize');

            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 1);
            $result = curl_exec($ch);
            curl_close($ch);

            if (FALSE === $result)
                throw new Exception(curl_error($ch), curl_errno($ch));

            // ...process $result now
        } catch(Exception $e) {

            trigger_error(sprintf(
                'Curl failed with error #%d: %s',
                $e->getCode(), $e->getMessage()),
                E_USER_ERROR);

        }

        $result_json =  json_decode($result, true);


        dd($id); // Undefined variable: id


        if ( $result_json['status'] == '200') {
            return Redirect::to('/account/'.$id.'#hardware') ->with('success','The CPE was created succesfully!');
        } else {
            return Redirect::to('/account/'.$id.'#hardware') ->with('error', $result_json['message']);
        }

    }

我正在嘗試通過{!! Form::hidden('$id', $account->account_id )!!} {!! Form::hidden('$id', $account->account_id )!!}到我的商店控制器,以便可以在我的控制器的store()函數內部訪問它。

但我不斷得到Undefined variable: id

有人可以告訴我從表單向控制器發送隱藏輸入的正確方法嗎?

您的代碼有兩件事:

  1. 您的隱藏輸入應使用id ,而不是$id
  2. 您的store方法應使用$inputs['id'] ,而不是直接訪問$id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM