Home > Software design >  How can I set a session variable conditionally in a blade template?
How can I set a session variable conditionally in a blade template?

Time:01-13

I am developing an app in Laravel 9 and am trying to add Cancel functionality to my Create template. I want the form to have two buttons: Submit and Cancel. I asked how to add Cancel functionality to my form last night in this question and got a couple of answers that work. However, it struck me that there might be another way to do it, namely having both buttons invoke the create() method of the controller and then for the create() method to determine which button was pushed, then do different logic based on which button was pressed. I'm imagining a session variable being set to "create" if the Create button is clicked and for the session variable to be set to "cancel" if the Cancel button is clicked.

If the Cancel button was pressed, the create() method would bypass validations and then redirect back to the index page with the message "Create() cancelled"; otherwise, if the Create button was pressed, the create() method would do all the validations, add the new record to the database, and then redirect to the index page with the message "Create was successful".

I have no concerns about getting the create() method to work but I'm having a challenge setting a session variable in my create template based on which button was pressed. I've tried several variations but all of them return "Cancel" REGARDLESS of which button was pressed! I don't understand this behaviour at all: clearly, Laravel doesn't work in the way that I imagine.

I researched how to set a session variable in a blade file here at StackOverflow but couldn't see any examples where the value of the variable was set conditionally so I clearly need some help with that part.

Here's my latest attempt at the template:

<x-layout>
    <x-card x>
    <h1 >Create a New Non-driving Reason</h1>
    <h1 ></h1><!-- spacer beneath title -->
    
    <form method="POST" action="/nonDrivingReasons/store" enctype="multipart/form-data">
    @csrf
        <div >
            <label for="reason_for_not_driving" >Reason for not driving</label>
            <input type="text"  name="reason_for_not_driving" value="{{old('reason_for_not_driving')}}"/>
            @error('reason_for_not_driving')
                <p >{{$message}}</p>
            @enderror
        </div>            
        <div >
            {{-- Submit the completed form --}}
            <button  onClick="@php session()->put('button', 'submit') @endphp">Submit</button>
            {{-- Cancel submission of the form --}}
            <button  onClick="@php session()->put('button', 'cancel') @endphp">Cancel</button>
                        </div>
    </form>
    </x-card>
    </x-layout> 

CodePudding user response:

You cannot set session like this onClick="@php session()->put('button', 'submit') @endphp" because, php is server side scripting language and javascript is clientside, SO I suggest you to use little bit of js code instead of setting session,

Try using button as below:

            <button  type="button">Submit</button>
        {{-- Cancel submission of the form --}}
        <button  type="button" >Cancel</button>
                    </div>

And this small piece of javascript which help you to send which button you have clicked on the request:

<script>
document.querySelector("button").addEventListener("click", (e) => {
    e.preventDefault()
    document.getElementById("button").value = e.target.innerText;
    document.querySelector('form').submit()
});

so in Controller if you do dd($request->button); you can get the value text of button that you have clicked.

  • Related