user12156754
user12156754

Reputation:

Conditional statement based on value of attributes in laravel blade

i want to make if else statement based on the input id of select given

here is my code

my attribute id

<select name="cu" id="cu" class="input-sm form-control" onchange="">

My if else condition

@if(??)
    <a class="btn btn-sm btn-default pull-right" type="button"
     title="Search Category"   onclick="openNewWindow('{{ route('reporting.') }}')" >
    <i class="fa fa-hand-o-up"></i>&nbsp;&nbsp;Choose Category</a>

@else
    <a class="btn btn-sm btn-default pull-right" type="button" title="Search Category"   
    onclick="openNewWindow('{{ route('reporting.categoryCheckBoxList', ['cuid' => '_cuid_']) }}'.replace('_cuid_', document.getElementById('cu').value))" >
    <i class="fa fa-hand-o-up"></i>&nbsp;&nbsp;Choose Category</a>

@endif

what source code i need to put inside if statement

Upvotes: 0

Views: 4412

Answers (2)

Sehdev
Sehdev

Reputation: 5682

You cannot pass js value inside php. You can achieve the same result using Jquery show and hide element:

<select name="cu" id="cu" class="input-sm form-control" onchange="showLink(this.value)">

<a style="display:none" id="first" class="btn btn-sm btn-default pull-right" type="button"
   title="Search Category" onclick="openNewWindow('{{ route('reporting.') }}')">
    <i class="fa fa-hand-o-up"></i>&nbsp;&nbsp;Choose Category</a>

<a style="display:none" id="second" class="btn btn-sm btn-default pull-right" type="button" title="Search Category"
   onclick="openNewWindow('{{ route('reporting.categoryCheckBoxList', ['cuid' => '_cuid_']) }}'.replace('_cuid_', document.getElementById('cu').value))">
    <i class="fa fa-hand-o-up"></i>&nbsp;&nbsp;Choose Category</a>

<script>
    function showLink(value) {
        if (value == < your value >)
        {
            $("#first").show();
            $("#second").hide();
        }
        else
        {
            $("#second").show();
            $("#first").hide();
        }
    }
</script>

Upvotes: 0

Foued MOUSSI
Foued MOUSSI

Reputation: 4813

The short answer you can't pass selected value (JS variable) to @if() statement (PHP code).

Here is why

On page request: The process flow looks like this:

  1. Server Side PHP code rendering
  2. Response sent to the Browser
  3. Browser begins rendering and executing JS

You would want to hide (using bootstrap d-none class utility) both a tags on page load then on user select "change" event you show/hide your correspondant a tag.

Blade Code

<select name="cu" id="cu" class="input-sm form-control">
     <option value="option-value1"> Opt 1</option>
     <option value="option-value2"> Opt 2</option>
</select>

//...

<a id="option-value1" class="btn btn-sm btn-default pull-right d-none" type="button"
     title="Search Category"   onclick="openNewWindow('{{ route('reporting.') }}')" >
    <i class="fa fa-hand-o-up"></i>&nbsp;&nbsp;Choose Category</a>


<a id="option-value2" class="btn btn-sm btn-default pull-right d-none" type="button" title="Search Category"   
    onclick="openNewWindow('{{ route('reporting.categoryCheckBoxList', ['cuid' => '_cuid_']) }}'.replace('_cuid_', document.getElementById('cu').value))" >
    <i class="fa fa-hand-o-up"></i>&nbsp;&nbsp;Choose Category</a>

//...

<script>
//...
//make sure you already loaded Jquery
$('select#cu').on('change', function() {
    value = this.value;
    $("a#" + value).show();
    $("a id[value!='" + value + "']").hide();   
});
</script>

Upvotes: 1

Related Questions