Home > Enterprise >  Laravel - get value from "select" in post form
Laravel - get value from "select" in post form

Time:11-24

so I have a form with a select option inside it, and I'm trying to get the choice of the user and then insert it into the table and then database, but it doesn't seem to be working. Here is the code I thought was necessary to understand the problem, if there is anything else you need please let me know!

HTML

<form method="POST" action="{{ route('createEvent') }}">
    <label for="is_private">Privacy</label>
    <select id="is_private" name="is_private" required autofocus>
        <option value="" disabled selected>-- select one --</option>
        <option value="is_public">Public</option>
        <option value="is_private">Private</option>
    </select>
</form>

EventController.php

if (null !== $request->input('is_private')) {
  $event->is_private = 'true';
}
  else {
    $event->is_private = 'false';
}

The problem is that it's always returning with $event->is_private = 'true'; and I don't know why...

Thanks alot for any help you may provide!

CodePudding user response:

You need to use following code. I think you want that if user select is_private option then $event->is_private is true other wise false.

if ($request->input('is_private') == "is_private") {
  $event->is_private = 'true';
}
else {
    $event->is_private = 'false';
}

CodePudding user response:

Hmm.. I would try something like:

switch( $request->input( 'is_private' ) )
{
   case 'is_private':
     $event->is_private = 'true';
     break;

   default:
     $event->is_private = 'false';
     break;
}
  • Related