One problem is on this line:
<option value="{{$page->id}}" @if(old('page1', $items->content_id) === $page->id) selected @endif>{{$page->title}}</option>
You’re checking the old value of page1
which will be the string entered in the text input. Change it to page
:
@if(old('page', $items->content_id) === $page->id)
To select “Add Custom URL” you need to also add the selected
attribute:
<option value='NewField' @if(old('page', $items->content_id) === 'NewField') selected @endif>Add Custom URL</option>
And finally, the JS function also needs to run on load, not just on change, so the text input gets displayed (if necessary).
This is my output https://imgur.com/TVaZkwl, I am not getting the selected value. Basically here is two fields One is drop down and another is input field how can I get this selected value if value is selectedor if value is inputed then show input field https://imgur.com/WoyIZzs
This is my edit.blade file
<div class="form-group">
<label for="page" class="labelclass">Pages</label>
<select class="form-control" name="page" id="pageone">
<option value="0">Select page</option>
@foreach($pages as $page)
<option value="{{$page->id}}" @if(old('page1', $items->content_id) === $page->id) selected @endif>{{$page->title}}</option>
@endforeach
<option disabled>---</option>
<option value='NewField'>Add Custom URL</option>
</select>
<input class="form-control" type="text" name="page1" id="page" placeholder="https://" value="{{$items->content_id}}" style="display: none; width: 450px; margin-top: 5px;" />
</div>
<!--Here is my java script code-->
<script type="text/javascript">
$(function() {
// Add lead
$("#pageone").change(function() {
var dropvalue = $(this).val();
if (dropvalue == "NewField") {
$("#page").show();
} else {
$("#page").hide();
}
});
});
</script>
And this is my controller
public function edit($id)
{
//
// $items = Menu::with('Content')->findOrFail($id);
$items = Menu::findOrFail($id);
$pages = Content::all();
// return view('menu.edit',compact('items','pages'));
// dd([$pages, $items]);
return view('menu.edit',['items'=>$items, 'pages'=>$pages]);
// return view('menu.edit',['items'=>$items]);
}
/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param int $id
* @return IlluminateHttpResponse
*/
public function update(Request $request, $id)
{
//
$this->validate($request,[
'name'=>'required',
'status'=>'required|not_in:2',
'page'=>'required|not_in:0'
]);
$items = Menu::find($id);
// return json_encode($items);
// exit();
// dd($items);
$items->name = $request->name;
$items->status = $request->status;
// $items->content_id = $request->page;
if ($request->input('page') == 'NewField') {
$items->content_id = $request->page1;
} else {
$items->content_id = $request->page;
}
$items->save();
// return Redirect::route('admin_menu')->with('message','Menu is updated successfully');
return redirect()->route('admin_menu')->with('message','Menu is updated successfully');
}
again I am not getting old selected value in dropdown. this is the output