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

JakeG's avatar
Level 1

Checkbox

Hi, I'm currently trying to display multiple checkbox to another page and it displays Page Expired My route is Route::get("/nextpage",[PageController::class,"nextpage"]); below is my code

    <form action="{{url('/nextpage')}}" method="post">
    <div class="row">
                  <div class="col-sm-3">
                    <p class='mybox text-dark'><input type="checkbox" name="books[]" value="Chemistry"/>Chemistry
                <input type="hidden" name="book_name[]" value="Chemistry">
                </p>
                <input type="number" name="book_qty[]" min="1" value="1"class="form-control"></div>
                <div class="col-sm-3">
                    <p class='mybox text-dark'><input type="checkbox" name="books[]" value="English"/>English
                <input type="hidden" name="book_name[]" value="English">
                </p>
                <input type="number" name="book_qty[]" min="1" value="1"class="form-control"></div>
    </div>
                <div class="text-center m-2">
                    <button class="btn btn-info btn-m text-center" type="submit">Submit</button>
                </div>
0 likes
6 replies
JakeG's avatar
Level 1

I think the issue is already solved. But my next error I encountered is Array to String conversion and this is my code

<?php
if(isset($_POST['submit'])){
$checked_array = $_POST['books'];
foreach($_POST['book_name'] as $key => $value){
if(in_array($_POST['book_name'][$key], $checked_array)){
  echo $_POST['book_name'][$key];
  echo $_POST['book_qty'][$key];

}
}
}
?>
Sinnbeck's avatar

@Jake_G I'm confused. You use Route::get() but this isn't laravel?

Which line gives the error?

1 like
JakeG's avatar
Level 1

@Sinnbeck It doesn't show which line on my code is the error. This shows in the stack trace though

        foreach ($segments as $segment) {
        $result .= (array_shift($replace) ?? $search).$segment;

        }

where the line $result is highlighted and this is the code for my controller

        public function nextpage(){
        return view('nextpage');
        }
Sinnbeck's avatar

@JakeG can or show the full view? I cannot see what $replace or $search is

And maybe share the error page (there should be a share button)

1 like
JakeG's avatar
Level 1

@Sinnbeck I was wrong about my page being displayed okay, turns out I just changed the action to another page. I read something from another page where I can use php artisan route:clear or php artisan route:cache now the page I want is both being displayed however the problem I encountered now is that the selected value from checkbox is not being displayed to the nextpage. Where my code inside the nextpage is still the same

		<?php
		if(isset($_POST['submit'])){
		$checked_array = $_POST['books'];
		foreach($_POST['books_name'] as $key => $value){
		if(in_array($_POST['books_name'][$key], $checked_array)){
		echo $_POST['books_name'][$key];
		echo $_POST['books_qty'][$key];
		}
		}
		}
		?>

Should I insert the code from nextpage inside the function intead? where the code in my function from controller is but I don't know how I would display it to the other page by doing that

		public function nextpage(Request $request){
		return view('nextpage');
		}
Snapey's avatar

if you rename your inputs you will find the data much easier to deal with

<input type="hidden" name="books[][name]" value="Chemistry">

<input type="number" name="books[][qty]" min="1" value="1"class="form-control">

when you do it like this, you get an array of books, each containing a name and a qty key

this means you don't need to have synchronised stepping between two arrays

1 like

Please or to participate in this conversation.