0

I am currently facing 2 problems right now:

  1. I want to make my checkbox appear if my driver license is yes, so I decided to try using if else in this scenario but I seem to keep getting this error:

Parse error: syntax error, unexpected '<'

Can someone tell me the proper way to do it?

  1. I want to check my checkbox based on my database value, so for example if class A and Class B is already selected and saved in the database last time, I want them to be checked when I try to update their data so that I know what the user has entered

I have tried following other people example to do but nothing is happening.

I tried following here, but still not checked:

https://laracasts.com/discuss/channels/general-discussion/getting-a-checkbox-checked

Here is the code:

<div class="editfield">
              <div class="radio">
                <span><b>Do you have a Driver`s license?</b></span>
                <div id="Driver_licenseID">
                   <label><input type="radio" name="Driver_license" id="yesid" value="Yes" onclick="document.getElementById('Driver_license').style.display='block'" {{ $object->Driver_license == 'Yes' ? 'checked' : '' }} >Yes</label>
                  <label><input type="radio" name="Driver_license" id="noid" value="No" onclick="document.getElementById('Driver_license').style.display='none'" {{ $object->Driver_license == 'No' ? 'checked' : '' }}>No</label></div>
                </div>
            </div>
            @if ({{ $object->Driver_license == 'Yes' ? 'checked' : '' }})
            <div class="editfield" id="Driver_license" required>
              <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class1" value="Class 1">Class 1 
              <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class2" value="Class 2">Class 2 <br>
              <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class3" value="Class 3">Class 3 
            </div>
            @endif
            @if({{ $object->Driver_license == 'No' ? 'checked' : '' }})
            <div class="editfield" id="Driver_license" style="display:none">
              <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class1" value="Class 1">Class 1 
              <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class2B" 
              <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class2" value="Class 2">Class 2 <br>
              <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class3" value="Class 3">Class 3 
            </div>
            @endif
Dkna
  • 409
  • 3
  • 15
  • 37

1 Answers1

1

First:

The parse error is due to invalid syntax for blade.

https://laravel.com/docs/5.5/blade#if-statements

@if ($object->Driver_license == 'Yes')

instead of:

@if({{ $object->Driver_license == 'Yes'}})

Second:

To make a checkbox checked in HTML, you just need to add the checked attribute. To make it checked conditionally, use a conditional statement to provide the checked attribute.

Try the following:

<div class="editfield">
  <div class="radio">
    <span><b>Do you have a Driver`s license?</b></span>
    <div id="Driver_licenseID">
        <label><input type="radio" name="Driver_license" id="yesid" value="Yes" onclick="document.getElementById('Driver_license').style.display='block'" {{ $object->Driver_license == 'Yes' ? 'checked' : '' }} >Yes</label>
      <label><input type="radio" name="Driver_license" id="noid" value="No" onclick="document.getElementById('Driver_license').style.display='none'" {{ $object->Driver_license == 'No' ? 'checked' : '' }}>No</label></div>
    </div>
</div>
<div class="editfield" id="Driver_license" required>
  <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class1" value="Class 1">Class 1
  @if ($object->Driver_license == 'Yes')
    <input
      type="checkbox"
      name="Driver_license_class[]"
      id="Driver_license_class2B"
      @if($object->Driver_license_class)
        checked
      @endif> 
  @endif
  <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class2" value="Class 2">Class 2 <br>
  <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class3" value="Class 3">Class 3 
</div>
Community
  • 1
  • 1
Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158
  • Hi thanks for replying, I tried closing the tag already but the same error still occur where it points to here, Driver_license == 'Yes' ? 'checked' : ''); ?>): ?> – Dkna Dec 21 '17 at 01:43
  • And for the Second part I want it to be checked based on my database, so if my database contain value of Class 2 and Class 3, Class 2 and Class 3 need to be checked, I don't want it to manually add in the checked function – Dkna Dec 21 '17 at 01:44
  • @Dkna, I've updated. Looks like the syntax error is due to invalid blade syntax. – Raphael Rafatpanah Dec 21 '17 at 01:53
  • The first works, thanks a lot but the second still not being checked, I put mine as like this, @if($object->Driver_license_class ? 'checked' : '') @endif. Is this right? – Dkna Dec 21 '17 at 02:02
  • I think I got it right this time. – Raphael Rafatpanah Dec 21 '17 at 02:06
  • Erm isn't that just forcing it to checked if the Driver_license_class has value? – Dkna Dec 21 '17 at 02:11
  • If I want it to be checked based on database shouldn't I put something to all the input fields like the select input, , something like this? How do I do that? – Dkna Dec 21 '17 at 02:13
  • @Dkna That looks good to me. Regarding `isn't that just forcing it to checked if the Driver_license_class has value`, no, PHP is converting that value to a boolean (if it isn't already). If that value isn't a boolean, then you can do something like `@if($object->Driver_license_class) == "value"` – Raphael Rafatpanah Dec 21 '17 at 02:31
  • @Dkna If the `{{ $object->Country == 'Others' ? 'selected' : ''}}` syntax doesn't work, then you can use the `@if($object->Driver_license_class) checked @endif> ` syntax instead. If you find a better syntax, please update this answer with the better syntax. – Raphael Rafatpanah Dec 21 '17 at 02:32
  • The @if($object->Driver_license_class) checked @endif> will only return all my checkbox as checked while the @if($object->Driver_license_class) == "value" nothing happen... still unchecked. – Dkna Dec 21 '17 at 02:36
  • Just checking is there a need to use id? Because from what I saw some people uses the id to do it, https://laravel.io/forum/09-15-2014-how-to-show-checkboxes-as-checked-when-values-are-set-in-the-database – Dkna Dec 21 '17 at 02:37
  • @Dkna, are you talking about an HTML id, or an id from the database? – Raphael Rafatpanah Dec 21 '17 at 02:41
  • Just trying my luck hahahah – Dkna Dec 21 '17 at 02:42
  • Hi just wondering is there a proper method to do this? @if($object->Driver_license_class ->contains('Class 3')) checked @endif – Dkna Dec 21 '17 at 03:11
  • @Dkna, try it, and see what happens. Search Laravel's excellent docs if you get stuck. – Raphael Rafatpanah Dec 21 '17 at 03:22
  • Erm no, just want to use a function like contain instead of using ==, the Class 3 is just the name of the value used for the driver license – Dkna Dec 21 '17 at 03:23
  • I found another way, @if(in_array(Class 1, old('Driver_license_class'))) checked @endif, but it keep giving me undefined Class 1. So just wondering among all the suggestion that i found which do you think it the right one? So that I can focus more on it – Dkna Dec 21 '17 at 03:25
  • @Dkna Looks like you forgot the quotes. `@if(in_array('Class 1', old('Driver_license_class'))) checked @endif` – Raphael Rafatpanah Dec 21 '17 at 03:38
  • now this is return, use of undefined constant any idea? – Dkna Dec 21 '17 at 03:46
  • @Dkna Sounds like it's time for a new question – Raphael Rafatpanah Dec 21 '17 at 03:55