0

I have this method where I pass some posts and also this "isAnyPostButtonChecked" to the view:

public function edit($id)
    {
        ...
        $isAnyPostButtonChecked = false;
        $isAnyPostButtonChecked = $isAnyPostButtonChecked && (old('radiobutton') && old('radiobutton') == $event->id);
        return view('posts.edit')
            ->with('posts', $post)
            ->with('anyPost', $isAnyPostButtonChecked);
    }

In the view I have a form and above the form I have "{{$anyPost}}" to verify if any radio button is checked when the the form is submited. But it dont shows nothing with "{{$anyPost}}" when a radio button is selected and the form is submited. Do you know why?

   {{$anyPost}}
    <form id="editposts" method="post" 
      action="{{route('posts.update', ['post_id' => $post->id])}}" enctype="multipart/form-data">
  {{csrf_field()}}
  <div>
    @foreach($posts as $post)
    <div class="form-check">
      <input {{ (old('radiobutton') && old('radiobutton') == $post->id) ? 'checked' : '' }} class="form-check-input radio" type="radio" name="radiobutton" value="{{ $post->id }}" id="{{$post->id}}">
      <label class="form-check-label">
        {{$post->title}}
      </label>
    </div>
    @endforeach
  </div>
  <div class="form-check">
    <input checked checked {{ (old('radiobutton') && old('radiobutton') == 'create_post') ? 'checked' : '' }} class="form-check-input" type="radio" name="radiobutton" id="create_post"
           value="create_post">
    <label class="form-check-label">
      Create post
    </label>
  </div>

  <!-- form fields, here is the name but are more like description,... -->
  <div class="form-group">
    <label>Post title</label>
    <input  type="text" required class="form-control" value="{{ old('title') }}" name="title" id="tile">
  </div>
  <input type="submit" id="postupdate" value="Update"/>
</form>
johnW
  • 309
  • 1
  • 8
  • 26

2 Answers2

1

If It is not Printing in case of false you can check out this answer

{{ $anyPost ? 'true' : 'false' }}

Before doing this, Please check by assigning other variable to $anyPost .

I could not post it as comment, due to my low reputation :D

Please let me know if not working.

  • Thanks, but when a edit post radio button is checked and the form is submited it also appears false. Do you know why? – johnW Apr 05 '18 at 13:12
  • To my understanding , you will get the value of checkbox in controller whatever the value of the checkbox is. As per my knowledge, If we check a checkbox and submit form it will be appeared in dd($request->all()) . and if we dont check the checkbox, It will not be present in controller formvalues. So, you can check if checkbox is checked by isset($request['radiobutton']) . One thing more , I dont think it is good practice to generate multiple inputs with same name. you can check this answer https://stackoverflow.com/questions/2203430/posting-form-fields-with-same-name-attribute] – Maninder Singh Apr 05 '18 at 17:20
1

Because $anyPost variable contains the false every time, so on the false value view show noting and if there would be true value then it will show 1 in view.

There is the issue in your condition please check it for true value.

abrar
  • 480
  • 7
  • 10