Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Deekshith's avatar

fetch non checked checkbox values from dynamic form with array name

i have a fields like below,

 <form method="post" id="authorinfoform" action="http://localhost/journalportal/public/submission/submit-article/first-step" enctype="multipart/form-data">
<input type="hidden" name="_token" value="aqv36AUVeNVqKtPiUgUDdWJ0zVRCZjN0hYdflfVb">
    

        <div class="list_wrapper">  
            <div class="row">

                <div class="col-xs-4 col-sm-4 col-md-4">

                    <div class="form-group">
                        First Name
                        <input name="first_name[]"  type="text" placeholder="First Name" class="form-control" required="">
                        
                    </div>
                </div>

                <div class="col-xs-4 col-sm-4 col-md-4">
                    <div class="form-group">
                        Last Name
                        <input autocomplete="off" name="last_name[]"  type="text" placeholder="Last Name" class="form-control" required="">
                    </div>
                </div> 
                <div class="col-xs-4 col-sm-4 col-md-4">
                    <div class="form-group">
                        <div class="checkbox">
                          <label for="corresponding"><input type="checkbox" name="corresponding[]" id="corresponding" value="1" checked="">Corresponding author</label>
                        </div>
                        
                    </div>
                </div> 

                <div class="col-xs-1 col-sm-1 col-md-1">
                    
                   <button class="btn btn-primary list_add_button" type="button">+</button>
                </div>
            </div>
        <div class="row">
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group">
First Name 
<input name="first_name[]" type="text" placeholder="First Name" class="form-control" required="">
</div>
</div>
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group">
Last Name 
<input name="last_name[]" type="text" placeholder="Last Name" class="form-control" required="">
</div>
</div>
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group">
<div class="checkbox">
<label for="corresponding_1">
<input type="checkbox" name="corresponding[]" id="corresponding_1" value="1">Corresponding author
</div>
</div>
</div>
<div class="clearfix"></div>
<div class="col-xs-1 col-sm-7 col-md-1"><a href="javascript:void(0);" class="list_remove_button btn btn-danger">-</a></div></div></div>


<div class="text-right">
<button type="submit" class="btn btn-info btn-sm pull-right">Save</button>
</div>
</form>

Here i have checkbox field with array name,

and i am trying to save these details in controller like below,

$updatedata = [
                    'author_contribution' => request('contribution'),
                    'third_step'   => 1,
                ];

        if($checkJournalExists->status == "I")
        {
            $updatedata['status'] = "P";

        } 

        $updatedatadb = $checkJournalExists->update($updatedata);

        $checkStatus = $checkJournalExists->third_step;

       

        $insert = array();
        foreach($request->first_name as $key => $firstname) {
            $insert[$key]['first_name'] = $firstname;
            $insert[$key]['article_submission_id'] = $checkJournalExists->id;
        }
        foreach($request->last_name as $key => $lastname) {
            $insert[$key]['last_name'] = $lastname;
            $insert[$key]['article_submission_id'] = $checkJournalExists->id;
        }
        foreach($request->email as $key => $email) {
            $insert[$key]['email'] = $email;
            $insert[$key]['article_submission_id'] = $checkJournalExists->id;
        }
        foreach($request->title as $key => $title) {
            $insert[$key]['title'] = $title;
            $insert[$key]['article_submission_id'] = $checkJournalExists->id;
        }

        foreach($request->affiliation as $key => $affiliation) {
            $insert[$key]['affiliation'] = $affiliation;
            $insert[$key]['article_submission_id'] = $checkJournalExists->id;
        }

        foreach($request->department as $key => $department) {
            $insert[$key]['department'] = $department;
            $insert[$key]['article_submission_id'] = $checkJournalExists->id;
        }

$authorinsert = ArticleAuthor::insert($insert);

Here non selected checkbox values are not coming in array and also i want non selected checkbox return as 0 like below,

[1,0]

now what is happening is it is returning only checked checkbox field values. how to get non checked and checked field both with 0 and 1 value?

0 likes
5 replies
Snapey's avatar

your problem is made worse because you don't name the fields in a way that makes it easy to use

why do you have array syntax in your form elements?

Deekshith's avatar

Thank you for the reply. I have add more button where they can add same fields more than 1 time. So I have added array as name and I solved it by adding hidden input with same name.

Snapey's avatar

in such situations you should name fields in this format;

entity[]first_name
entity[]last_name

etc

then you get an array structured in a much cleaner format

1 like

Please or to participate in this conversation.