The Dead Man
The Dead Man

Reputation: 5566

Request does not return all data from the form

I have a section in which a user can add input values and can add an input field as many as he/she wants. the first input field is default added to the DOM.

The form looks like this

enter image description here

Here is HTML for this form assume user added one input field

<dvi class="container h-100">
    <div class="d-flex justify-content-center">
        <div class="card mt-5 col-md-12 animated bounceInDown myForm" id="multiple-container">
            <div class="card-header">
                <h4>Bidders Information</h4>
            </div>
            <div class="card-body" id="add_info">

                <div id="dynamic_container">
                    <small id="bidder">Bidder 1</small>
                    <span class="bmd-form-group"><div class="input-group">
                            <div class="input-group-prepend">
                              <span class="input-group-text br-15"><i class="fa fa-tags"></i></span>
                </div>
                <input type="text" placeholder="Bidders Name" name="bidder_name" class="form-control">
            </div>
            </span>
            <span class="bmd-form-group"><div class="input-group mt-3">
                            <div class="input-group-prepend">
                              <span class="input-group-text br-15"><i class="fa fa-tags"></i></span>
        </div>
        <input type="text" placeholder="atribute name" name="params_name" id="field1" class="form-control">
        <input type="text" placeholder="atribute value" name="params_value" id="field2" class="form-control">
        <a class="btn btn-secondary btn-sm moreinput_field" id="add_more_input">
            <i class="fa fa-plus-circle"></i>
        </a>
    </div>
    </span>
    </div>

    </div>
    <div class="card-body" id="add_info0">

        <div id="dynamic_container">
            <small id="bidder">Bidder 2</small>
            <span class="bmd-form-group"><div class="input-group">
                            <div class="input-group-prepend">
                              <span class="input-group-text br-15"><i class="fa fa-tags"></i></span>
        </div>
        <input type="text" placeholder="Bidders Name" name="bidder_name" class="form-control">
    </div>
    </span>
    <span class="bmd-form-group"><div class="input-group mt-3">
                            <div class="input-group-prepend">
                              <span class="input-group-text br-15"><i class="fa fa-tags"></i></span>
    </div>
    <input type="text" placeholder="atribute name" name="params_name" id="field1" class="form-control">
    <input type="text" placeholder="atribute value" name="params_value" id="field2" class="form-control">
    <a class="btn btn-secondary btn-sm more_input moreinput_field" id="add_more_input0">
        <i class="fa fa-plus-circle"></i>
    </a>
    </div>
    </span>
    <a class="btn btn-danger btn-sm" id="remove-more"><i class="fa fa-trash"></i></a>
    </div>
    </div>
    <div class="card-footer" id="card-footer">
        <a class="btn btn-success btn-sm" id="add_more"><i class="fa fa-plus-circle"></i> Add<div class="ripple-container"></div></a>

        <button class="btn btn-success btn-sm float-right submit_btn"><i class="fas fa-arrow-alt-circle-right"></i> Submit</button>
    </div>
    </div>
    </div>
</dvi>

On my controller, I have this.

   public function store(Request $request){

            dd($request);
    }

Which return me the following

enter image description here

as you can see it return only the second input fields

What is wrong with my code???

Upvotes: 3

Views: 474

Answers (4)

devpro
devpro

Reputation: 16117

One more suggestion, you can also defined bidder id inside the array something like:

name="bidder_name[b1]" or just name="bidder_name[1]"

This will help you to understand which index related to which bidder where you have multiple bidder.

Example:

<input type="text" placeholder="Bidders Name" name="bidder_name[b1]" class="form-control">

or

<input type="text" placeholder="Bidders Name" name="bidder_name[1]" class="form-control">

Upvotes: 1

N69S
N69S

Reputation: 17206

This has nothing to do with laravel specifically, that's how HTTP requests work. The two inputs have the same name parameter so the second one overwrite the first one.

If you want multiple values in the same variable, use it as a table by adding brackets.

<input type="text" placeholder="Bidders Name" name="variable_name[]" class="form-control">

Upvotes: 1

ascsoftw
ascsoftw

Reputation: 3476

The reason you are getting only 1 value is because your HTML attributes have the same name

<input type="text" placeholder="Bidders Name" name="bidder_name" class="form-control">

In the above the name attribute is bidder_name which will be same for both the rows. So you are only getting the 2nd Row.

You can change it like below

<input type="text" placeholder="Bidders Name" name="bidder_name[]" class="form-control">

Same changes needs to be made for other 2 HTML Form Elements.

Upvotes: 3

Maraboc
Maraboc

Reputation: 11083

You have to tell laravel that it's a collection of names for example this :

<input type="text" placeholder="Bidders Name" name="bidder_name" class="form-control">

Should be like this :

<input type="text" placeholder="Bidders Name" name="bidder_name[]" class="form-control">

Upvotes: 4

Related Questions