Home > Mobile >  How to get a php value from a select option?
How to get a php value from a select option?

Time:11-25

I want to get the value of $animal when I select an option so I can press the button and see detailed info of this option.

Code

@foreach ($animais as $animal)

  <option value="{{ $animal->id }}">{{ $animal->nome }}</option>
@endforeach
  </select>
  
  <a href="{{ route("animals.show", $animal) }}"><button type="submit">Ver</button></a>

CodePudding user response:

One the HTML page is written from the PHP, that value is a node in the DOM, and you can access it through javascript.

window.addEventListener('DOMContentLoaded', () => {
  document.querySelector('select.example').addEventListener('change', e => {
    console.log('value selected: ', e.target.value, 'from text', e.target.options[e.target.selectedIndex].text)
  })
})
<select class='example'>
  <option value='zebra'>animal</option>
  <option value='carrot'>vegetable</option>
</select>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related