triest23
triest23

Reputation: 127

Check request on field

The form has a dynamic checkbox.

<form method="POST" action="{{route('articleStore')}}" enctype="multipart/form-data">    
    <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
        <span class="glyphicon glyphicon-cog"></span> 
        <span class="caret"></span>
    </button>

    <ul class="dropdown-menu">
        @foreach($tags as $tag)
            <input checked="checked" name="{{$tag->tagname}}" type="checkbox" value="no">
            {{$tag->tagname}}
            <br>
        @endforeach
    </ul>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

The query looks like this:

"_token" => "CnBUXq8f3mJebscKSThyTZ1vlAhPnPsDD0ymZnQI"
"title" => ";"
"desc" => ";"
"text" => "<p>иммими</p>"
"wifi" => "yes"
"kll" => "no"

Where "kll" => "no" and, onter dynamick part from :

@foreach($tags as $tag)
    <input checked="checked" name="{{$tag->tagname}}"    type="checkbox" value="no">
    {{$tag->tagname}}
    <br>
@endforeach

How can I check which fields are selected?

Upvotes: 2

Views: 49

Answers (1)

kate-s2d
kate-s2d

Reputation: 36

For a related group of checkboxes, it works better to give all the inputs the same name (i.e., name="checked-tags"), and set the value of each input to include $tag->tagname. Then the submitted form data will contain a list of checked fields in the variable $_POST["checked-tags"].

Then your form handler function can cycle through the array to build a list of values to include in your query.

I see you are setting them all as checked by default. Any checkbox inputs that are unchecked prior to form submission will not be included in $_POST["checked-tags"].

Upvotes: 1

Related Questions