0

i'm having a problem with the @foreach in a blade.view

<SELECT  id= "chosen" type="text" class="form-control @error('chosen') is-invalid @enderror" name='chosen' style="width: 300px">

   @foreach($tables  as $chosen)
    <option value="{{ $chosen }}" > {{ $chosen }}</option>
   @endforeach

   </SELECT>


{{ var_dump($chosen) }}

in this dropdown menu the var_dump() does not print the value of what i choose, just the last row of the foreach, how can i print the data i choose in the menu?

  • 1
    Remember that PHP is a pre-processor and runs before the page is sent to the browser. Any changes made on the page would need to be handed by javascript – aynber Nov 12 '20 at 17:29

1 Answers1

0

PHP runs from server and sent rendered html to client, so you cant access selected value in dropdown. you can use JQuery in browser to get current selected value.

check this:

$('#chosen').on('change', function() {
  alert( this.value );
});

or

var selectedItem = $("#chosen").children("option:selected").val();
alert("You have selected the item- " + selectedItem);
Manian Rezaee
  • 1,012
  • 12
  • 26
  • how should i put this inside my blade? –  Nov 12 '20 at 17:44
  • its not depends to blade. Do the same with blade as you do with html. just put this codes inside a – Manian Rezaee Nov 12 '20 at 17:51
  • ok now it's more clear, but the alert appear just when i go to the page for the first time, what if i want to print dinamically the selected row (i need that for a later storing) –  Nov 12 '20 at 18:07