简体   繁体   中英

Laravel request()->all() doesn't get all inputs

Sending data via POST form. Why doesn't the controller see all of the values? I've been playing around with the validation, so even setting those to 'required' doesn't change anything...

Form snippet:

 <form action="{{ Protocol::home() }}/offers/make" method="POST" id="sendOffer">
        <div class="modal-body">
            <meta name="csrf-token" content="{{ csrf_token() }}">
            <!-- Price -->
            <div class="form-group" style="display:none;">
                <label>{{ Lang::get('ads/show.lang_your_price') }}</label>
                <input type="text"  id="price_id" name="price" value="0">
                <span class="help-block">{{ Lang::get('ads/show.lang_the_amount_required') }} <b>{{ Helper::getPriceFormat($ad->price, $ad->currency) }}</b></span>
            </div>
            <!-- location -->
            <div class="form-group">
                <label>location label</label>
                <input type="text" placeholder="Andover MA" id="location_id" class="form-control" name="location_name">
            </div>
            <!-- Email Address -->
            <div class="form-group">
                <label>email label</label>
                <input type="email" required="" placeholder="email" id="email_name" class="form-control" name="email_name">
            </div>
            <!-- Phone -->
            <div class="form-group">
                <label>phone label</label>
                <input type="text" maxlength="12" placeholder="555-867-5309" id="friendNumber" class="form-control" name="phone_name">
            </div>
            <!--Time section-->
            <div class="form-group">
                <label>The time</label>
                <input type="time" id="time_id" name="time_name"
                       min="9:00" max="18:00" required>
            </div>
            <!-- Post ID -->
            <div class="form-group">
                <label>{{ Lang::get('ads/show.lang_post_id') }} (for reference)</label>
                <input type="text" readonly="" placeholder="{{ Lang::get('ads/show.lang_post_id') }}" id="postID" value="{{ $ad->ad_id }}" class="form-control" name="ad_id">
            </div>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-success">{{ Lang::get('ads/show.lang_send_offer') }}</button>
            </div>
        </form>

Controller:

/**
 *  Make New Offer
 */
public function make(Request $request)
{
    // Check ajax request
    if ($request->ajax()) {

            $rules = array(
            'location_name' => '',
            'email_name' => '',
            'phone_name' => '',
            'time_name' => '',
            'ad_id' => 'required'
        );
        //run rules
        $validator = Validator::make($request->all(), $rules);

        if ($validator->fails()) {
            // error
            $response = array(
                'status' => 'error',
                'msg'    => __(print_r($request->all())),
            );
            return Response::json($response);
        }else{
            // Get Inputs
            $price = $request->get('price');
            $location_name  = $request->input('location_name');
            $email_name = $request->input('email_name');
            $phone_name = $request->input('phone_name');
            $time_name = $request->input('time_name');
            $ad_id = $request->input('ad_id');
            $input = $request->all();
            //let's figure it out:
            dd($input);
        // Success test
          $response = array(
          'status' => 'success',
          'msg'    => __('return/success.lang_offer_submitted'),
           );
return Response::json($response);

Output in console (only showing price and ad_id for some reason):

 array:2 [
  "price" => "0"
  "ad_id" => "1988726232"
]

Route:

// Make an Offer
Route::post('offers/make', 'Ads\OffersController@make');

The error was a result of me forgetting to include these fields in the ajax request smh

    var _root = $('#root').attr('data-root');
    var offerPrice = document.getElementById('offerPrice').value;
    var postID = document.getElementById('postID').value;
    var location_name = document.getElementById('location_name').value;
    var email_name = document.getElementById('email_name').value;
    var phone_name = document.getElementById('phone_name').value;
    var time_name = document.getElementById('time_name').value;
    $.ajax({
        type: "POST",
        url: _root + '/offers/make',
        data: {
            price: offerPrice,
            ad_id: postID,
            location_name: location_name,
            email_name: email_name,
            phone_name: phone_name,
            time_name: time_name
        },

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