0

I'm trying to insert a data/value into my database.

It works but the problem is that every time I select the second value/checkbox only its other input value gets the value of the first input.

<form action="{{url('/reservation')}}" method="post">
    @csrf
    <div class="row col-12">
        <div>
            <p class='mybox text-dark'><input type="checkbox" name="prod_name[]" value="JasminBooks"/>JasminBooks</p>
        </div>
        <div>
            <input type="number" name="prod_qty[]" min="1" value="1"class="form-control ml-2">
        </div>
    <div class="row col-12">
        <div>
            <p class='mybox text-dark'><input type="checkbox" name="prod_name[]" value="KnowHowBooks"/>KnowHowBooks</p>
        </div>
        <div>
            <input type="number" name="prod_qty[]" min="1" value="1"class="form-control ml-2">
        </div>
    </div>
</form>

This is the code for my controller function

public function reservation(Request $request)
{
    $data = new reservation;
    $data->name = $request->name;
    $data->email = $request->email;
    $data->phone = $request->phone;
    $data->address = $request->address;
    $data->date = $request->date;
    $data->time = $request->time;

    $products = null;
    $checked_array = $_POST['prod_name'];

    foreach($_POST['prod_name'] as $key => $value) {
        if (in_array($_POST['prod_name'][$key], $checked_array)) {
            $products .= $_POST['prod_qty'][$key]." ".$_POST['prod_name'][$key].", ";
        }
    }

    $data->products = $products;
    $data->save();
    return redirect()->back();
}

The result and the data is saved into the database when

  1. I select the first checkbox and input a value of quantity 5 the result is "5 JasminBooks,"
  2. When I select both checkboxes and input a value of quantity 12 in the first input beside the first checkbox and 7 for the second input/quantity besides the second checkbox the result is "12 JasminBooks, 7 KnowHowBooks, "
  3. But when I only select the second checkbox and input a value of 13 for the quantity the result is "1 KnowHowBooks, " it takes the default value of the first input rather than the second input where I inserted 13 as the quantity.

What should I add/change in my code?

3
  • 1
    why are you using $_POST when Laravel provides a powerful $request variable? Commented Sep 9, 2022 at 4:46
  • Saving multiple values into a single field as you're doing could be considered a code smell. I would recommend reconsidering your implementation (have a look at relationships, particularly the one to many relationship). Commented Sep 9, 2022 at 7:08
  • I'm still learning about laravel and I thought I could use $_POST ok like in php. Thanks for telling and giving directions though Commented Sep 9, 2022 at 12:20

1 Answer 1

1

It's because you're defining your quantity fields based on index. If a single input is missing, it could throw off your whole result. Use the value as key:

HTML:

<div>
    <p class='mybox text-dark'><input type="checkbox" name="prod_name[]" value="JasminBooks"/>JasminBooks
    </p></div><div>
    <input type="number" name="prod_qty[JasminBooks]" min="1" value="1"class="form-control ml-2">
</div>

PHP:

$products = '';
$checked_array = $request->input('prod_name', []);
$quantities = $request->input('prod_qty', []);

foreach ($checked_array as $value) {
    if (array_key_exists($value, $quantities)) {
        $products .= "{$quantities[$value]} {$value}, ";
    }
}

// Remove trailing ', '
if (! empty($products)) {
    $products = substr($products, 0, -2);
} 

$data->products = $products;

P.S. Like Bhaumik said, don't use $_POST in Laravel.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Yinci for providing a solution and I will follow what Bhaumik said. The first if statement lacks ")" so it gives an error but the solution you gave answered my question. tysm
Oh yes apologies, I'll fix the syntax error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.