mardi 6 septembre 2022

How to display value of multiple checkbox to another page

I'm having trouble where I can't seem to retrieve/display the value I selected to another page This is my route code

Route::post("/product",[HomeController::class,"product"]);
Route::get("/checkbox",[HomeController::class,"checkbox"]);

In my function in controller

    public function product(Request $request){
       return view('/product');
    }

   public function checkbox(){
       return view('/checkbox');
    }

The code from the first page where the checkbox are being displayed

<form action="" method="POST">
  @csrf
<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="books_name[]" value="Chemistry">

                <input type="number" name="books_qty[]" min="1" value="1"class="form-control">
            <div class="col-sm-3">
                <p class='mybox text-dark'><input type="checkbox" name="books[]" value="English"/>English
                <input type="hidden" name="books_name[]" value="Chemistry">

                <input type="number" name="books_qty[]" min="1" value="1"class="form-control">
            <div class="text-center m-2">
                <button class="btn btn-info btn-m text-center" type="submit">Submit</button>
            </div>
</form>

The code where the selected value should be displayed

<?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];

}
}
}
?>

The first page it displays the form where the checkbox resides ok and when I click the the submit I will be directed to the next page but the value is not being displayed that's why the page is displaying empty but when I try inserting

Hello

It displays it that's why the page is working ok but it doesn't display the value I selected from the checkbox.


Aucun commentaire:

Enregistrer un commentaire