0

Through checkbox I want to pass & values. how can I do that. I can only pass one value. I am trying but its not working

while($row_tbl = mysqli_fetch_array($query))
      {
        <tr class="success" style="font-size:12px;">

         <td > <input type="checkbox" name="check[]" class="chk_val" value=" <?php echo $row_tbl['Course_ID'] ?> " id="in" onclick="test()" /></td>

            <td > <?php echo $row_tbl['Course_ID'] ?> </td>
            <td> <?php echo $row_tbl['Course_Title'] ?> </td>
            <td> <?php echo $row_tbl['Section'] ?> </td>
            <td> <?php echo $row_tbl['Time'] ?> </td>
            <td> <?php echo $row_tbl['Day'] ?> </td>
            <td> <?php echo $row_tbl['Dept'] ?> </td>
            <td> <?php echo $row_tbl['Capacity'] ?>&nbsp/&nbsp 0 </td>


                    <?php

                  }
2
  • What have you tried that is not working? Commented Mar 9, 2017 at 4:50
  • Use proper tags. This has nothing to do with Laravel nor CakePHP. Commented Mar 9, 2017 at 8:21

1 Answer 1

1

To pass multiple values, you need to create multiple checkbox with the same name (as an array)

 <input type="checkbox" name="check[]" class="chk_val" value="value1"/>
 <input type="checkbox" name="check[]" class="chk_val" value="value2"/>
 <input type="checkbox" name="check[]" class="chk_val" value="value3"/>
 <input type="checkbox" name="check[]" class="chk_val" value="value4"/>

In your controller,

$values = $request->check; //the array of checked inputs.

Then you can loop through it

foreach($values as $value) {
    ...
}

Update

Accodring to the chat discussion you want to send 2 different fields per checkbox. In your checkboxes, put both fields like this

@foreach($courses as $course)
    <input type="checkbox" 
        name="check[]" 
        class="chk_val" 
        value="{{ $course->id }}-{{ $course->section }}"/>
@endforeach

Pay attention to the value here. I added a delimiter - that will be useful in the controller. In controller now

public function selectedCourses(Request $request)
{
    ... //whatever you do for validation

    //loop through selected courses
    foreach( $request->check as $values ) { 
        $values = explode("-", $values); //split where we added the dash
        $id = $values[0]; //the course ID
        $section = $values[1]; // the course section

        ... do what you want here with that information
    }
}

And voila!

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

7 Comments

Thanks but My job is different. Here i use one check box and through that i want to capture two variable <td > <?php echo $row_tbl['Course_ID'] ?> </td> <td> <?php echo $row_tbl['Section'] ?> </td>
You are trying to pass to values to a single input. Let me tell you already it's IMPOSSIBLE. All you do now is compact your values as a string or an array and give them to your input. You cannot pass 2 separate values through the same input.
See here how to use a delimiter to send compact your values into a single string: stackoverflow.com/questions/3407317/…
I am basically making a course registration app then . how cal i fix that
I have no idea what you are trying to accomplish. All I know is you want to pass many values in a single input. What are you trying to do? Maybe you should redesign it to use multiple checkboxes?
|

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.