Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

icelander's avatar

error in getting the session value

this is my select option displaying the list of nearby pharmacies

 <form action="{{ route('pay') }}" method="post" class="creditly-card-form agileinfo_form">
                              <section class="creditly-wrapper wrapper">
                                 <div class="information-wrapper">
                                    <div class="first-row form-group">
                                        <div class="input-group mb-3">
                                             <div class="input-group-prepend">
                                                <label class="input-group-text" for="inputGroupSelect01">Pharmacies</label>
                                             </div>
                                            
                                             <select class="custom-select" id="inputGroupSelect01" name="pharmacy">
                                             
                                                
            </select>

this is my controller

public function redirectToGateway(Request $request)
    {
        $pharm= $request->get('pharmacy');
     

        dd($pharm);
        return Paystack::getAuthorizationUrl()->redirectNow();
    }

this is what it looks like when i click inspect elements on the browser

<select class="custom-select" id="inputGroupSelect01" name="pharmacy">
                                             
                                                
                                                
     <option value="Magduke Pharmaceutical Ltd., Ziks Ave, Awka, Nigeria">Magduke Pharmaceutical Ltd., Ziks Ave, Awka, Nigeria</option>
<option value="Radopin Supermarket Awka, Nnamdi Azikiwe Avenue, Awka, Nigeria">Radopin Supermarket Awka, Nnamdi Azikiwe Avenue, Awka, Nigeria</option>
<option value="Joez Pharmacy, Zik Avenue, Awka, Nigeria">Joez Pharmacy, Zik Avenue, Awka, Nigeria</option></select>

where am i getting it wrong please.

0 likes
8 replies
Snapey's avatar

No idea. What should it look like?

What error are you getting?

How do you submit the form?

Where is the csrf token included in the form?

icelander's avatar

this is the form with the csrf token. it submits to the above route, pay

  <form action="{{ route('pay') }}" method="post" class="creditly-card-form agileinfo_form">
                                 @csrf
                              <section class="creditly-wrapper wrapper">
                                 <div class="information-wrapper">
                                    <div class="first-row form-group">
                                        <div class="input-group mb-3">
                                             <div class="input-group-prepend">
                                                <label class="input-group-text" for="inputGroupSelect01">Pharmacies</label>
                                             </div>
                                            
                                             <select class="custom-select" id="inputGroupSelect01" name="pharmacy">
                                             
                                                
                                             </select>
                                             <input type="hidden" name="email" value="{{Auth::user()->email}}"> {{-- required --}}
                              <input type="hidden" name="pharmacy" value=" {{session('pharmacy')}} ">
                              <input type="hidden" name="amount" value="{{$totalPrice*100}}"> {{-- required in kobo --}}
                  
                              <input type="hidden" name="metadata" value="{{ json_encode(['pharmacy' => session('pharmacy')]) }}" > {{-- For other necessary things you want to add to your payload. it is optional though --}}
                              <input type="hidden" name="reference" value="{{ Paystack::genTranxRef() }}"> {{-- required --}}
                              <input type="hidden" name="key" value="{{ config('paystack.secretKey') }}"> {{-- required --}}
                              {{ csrf_field() }} {{-- works only when using laravel 5.1, 5.2 --}}

                               <input type="hidden" name="_token" value="{{ csrf_token() }}"> {{-- employ this in place of csrf_field only in laravel 5.0 --}}

                                          </div>
                                              <div class="card_number_grids">
                                              <div class="card_number_grid_left">
                                             
                                          </div>
                                          
                                          <div class="clear"> </div>
                                       </div>
                                       
                                       
                                    </div>
                                    
                                 </div>
                              </section>
                               <div class="checkout-right-basket">
                              <button class="submit check_out" type="submit" value="Make Payment">Make a Payment </button> 
                           </div>
                           </form>

when i click on the make payment button, i want to get the value i selected from the select drop down list, but this is the error i get

null

obviously, it is not getting any value from the select option. however,what i want it to do is to grab the value i selected from the drop down so i can see it displayed on the page when i hit the make payment button.

Snapey's avatar

Why are the <option> fields not shown in the form?

Why do you have csrf field three times in the form?

Snapey's avatar

but anyway, change this

$pharm= $request->get('pharmacy');

to

$pharm= $request->pharmacy;

icelander's avatar

the option field is created and appended to the select option with the help of javascript

 let option = document.createElement("option");
              option.text=pharmname;
              option.value=pharmname;
              document.getElementById('inputGroupSelect01').appendChild(option);

i have removed the multiple sightings of the csrf token.

Snapey's avatar

open the browser network tools.

post the form

Click on the row for the post request to the server and check what form values were sent.

icelander's avatar

these are the form values sent

_token: QO9O0B9jvYB3h1dCMAMXaH5jLBRfDTl2RDyuuTLZ
pharmacy: Joez Pharmacy, Zik Avenue, Awka, Nigeria
email: [email protected]
pharmacy:   
amount: 60000
metadata: {"pharmacy":null}
reference: brRQNgBs4DkrwPN7Nrn0361Ca
key: sk_test_b0ebcd79a3df3c4bffc741c237c6e478c22ea8b9

meaning that the appropraite selection of pharmacy in the select drop down was captured, as seen in the first pharmacy value: joez pharmacy, but the other empty pharmacy variable means that the session was not captured.

Snapey's avatar

You have

<input type="hidden" name="pharmacy" value=" {{session('pharmacy')}} ">

Please or to participate in this conversation.