0

What I'm trying to do is Schedule form represent in table with input field like day, start time, and end time.

In blade file, I have an array that I assigned values which is represent days (7 in total). The reason I put in array cause I don't want to write html code 7 times. I do FOR loop to display input field with assigned value.

Then, I want to input another value which is 'start time' and 'end time' according to each day.

Below is my code

In blade


    <?php
          $days = array("Monday","Tuesday","Wednesday","Thursday","Friday","Satuday");
          $daylength=count($days);

          for($i=0; $i<$daylength; $i++)
          {
     ?>
            <tr>
            <td><input type="text" name="day[]" class="form-control" value="{{ $days[$i] }}" disabled/></td>
            <td><input type="time" name="start_time[]" class="form-control"/></td>
            <td><input type="time" name="end_time[]" class="form-control"/></td>
            <td></td>
            </tr>

    <?php } ?>

In controller

$days = $request->input('day');
$start_time = $request->input('start_time');
$end_time = $request->input('end_time');
$zone_id = $zone->id;

    for($count = 0; $count < count($days); $count++)
    {
        $data = array(
                'day' => $days[$count],
                'start_time'  => $start_time[$count],
                'end_time'  => $end_time[$count],
                'zone_id'  => $zone_id,
              );

        $insert_schedule[] = $data; 
     }
     Schedule::insert($insert_schedule);

What I expected, it should store input values into database table. The problem is after submitting the form, controller request empty value.

The error shown :

count(): Parameter must be an array or an object that implements Countable

Hope anyone can help. Thank you in advanced.

2
  • maybe it's not submitting day because the input is disabled? Commented Jul 18, 2019 at 4:40
  • Yes you're right ! I found the solution from one of the answers below. I should replace "disabled" to "readonly". Commented Jul 18, 2019 at 6:30

4 Answers 4

2

disabled input won't submit. You need to use readonly if you want to make it not editable by user.

<input type="text" name="day[]" class="form-control" value="{{ $days[$i] }}" readonly/>
Sign up to request clarification or add additional context in comments.

Comments

2

Change

<input type="text" name="day[]" class="form-control" value="{{ $days[$i] }}" disabled/>

To

<input type="text" name="day[]" class="form-control" value="{{ $days[$i] }}" />

Disabled inputs will not submit with the form request. You'll have to use a different technique if you don't want a user to be able to change the value. Can it be hidden?

1 Comment

Thanks for the solution. It can't be hidden cause I need to display the days, but I don't want the user make any change on that. Finally found the solution, I must replace "disabled" with "readonly".
0

try this:

 for ($i = 0; $i < $start_time; $i++) {
    foreach ($days as $day) {
      $insert_array[] = array(
      'day' => $day[$i ],
        'start_time'  => $start_time[$i ],
        'end_time'  => $end_time[$i ],
        'zone_id'  => $zone_id,
          );
         }
       }

1 Comment

Thanks for the answer. I replace "disabled" with "readonly" at input (html). It's worked.
0

There is a problem in count() function try :

for($count = 0; $count < collect($days)->count(); $count++)
{
    $data = array(
            'day' => $days[$count],
            'start_time'  => $start_time[$count],
            'end_time'  => $end_time[$count],
            'zone_id'  => $zone_id,
          );

    $insert_schedule[] = $data; 
 }

1 Comment

Thanks for the answer. I replace "disabled" with "readonly" at input (html). It's worked.

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.