0

Basically I want to insert into database the data according to input value. I have multiple checkbox and input field. When I store data only " A " then its working and others are null values. If I stored others with A then stored all the values. User can be check and input values any time and any number of check box. Here are the code snippet,

<form action="">
    <input type="checkbox" name="txt_check[]" id="">A
    <label for="fname">Win1:</label>
    <input type="text" id="fname" name="txt_win[]"><br><br>
    <label for="fname">Win2:</label>
    <input type="text" id="fname" name="txt_loss[]"><br><br>
    <label for="fname">Win3:</label>
    <input type="text" id="fname" name="txt_draw[]"><br><br>

    <input type="checkbox" name="txt_check[]" id="">B
    <label for="lname">Loss1:</label>
    <input type="text" id="fname" name="txt_win[]"><br><br>
    <label for="lname">Loss2:</label>
    <input type="text" id="fname" name="txt_loss[]"><br><br>
    <label for="lname">Loss3:</label>
    <input type="text" id="fname" name="txt_draw[]"><br><br>

    <input type="checkbox" name="txt_check[]" id="">C
    <label for="lname">Draw1:</label>
    <input type="text" id="fname" name="txt_win[]"><br><br>
    <label for="lname">Draw2:</label>
    <input type="text" id="fname" name="txt_loss[]"><br><br>
    <label for="lname">Draw3:</label>
    <input type="text" id="fname" name="txt_draw[]"><br><br>
    <input type="submit" value="Submit">
</form>

Here is the Controller:

       $win = $request->txt_win;
        $loss = $request->txt_loss;
        $draw = $request->txt_draw;
        $checkValue =  $request->txt_check ;
foreach $checkValue as $key => $value) {

$result[] = Model::create([
                    
     'checkValue' => $value, 
     'winning' => $win [$key],
     'lost' => $loss [$key],
     'draw' => $draw [$key],
                   
         ]);
   }

This code works well for the first insert but when I try to second insert then insert null value like this-

I want to insert into database this way,

 checkval  win  loss  draw
       A       2    3     4
       B       3    4     3
       C       4    5     5

How can I do this. I need help. Advanced thanks.

1 Answer 1

0

Your code is missing a circular brace on the foreach loop.

Change this

foreach $checkValue as $key => $value) {
     $result[] = Model::create([
                    
     'checkValue' => $value, 
     'winning' => $win [$key],
     'lost' => $loss [$key],
     'draw' => $draw [$key],
      ]);
   }

to this

foreach ($checkValue as $key => $value) {
    $result[] = Model::create([
                    
     'checkValue' => $value, 
     'winning' => $win [$key],
     'lost' => $loss [$key],
     'draw' => $draw [$key],
     ]);
   }
Sign up to request clarification or add additional context in comments.

6 Comments

It doesn't working . Still insert null value. If I select 2 checkbox only checkbox value inserted but others value null.
Remove the id on your elements. You have the same id duplicating on the page.
removed all the ids from the text input field but same nothing change at all.
If I use "Json_encode" then outcomes like this - A ["2","3","null"] B ["2","3","null"] Its look like - if I select first 2 box then 3rd box value inserted as null.
I am unable to locate where the error might be arising from at this point.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.