displaying live data what user insert into form - php

I am trying to make display value from live filling form. For example, selecting value from dropdown select box, then its showing me ID, but I wanted to display value which is DB table.
Suppose, I select field:NSEwhile filling form, its should be live diaply "NSE" but its showed me ID as '1'
exchange table:
below is my code:
JS:
function all_function()
{
value = $("#exchange").val();
if (value != "")
{
$("#exchange_value").html("Selected Exchanges :"+value);
}
}
PHP:
{!! Form::open(['method'=>'POST', 'action'=> 'trades\AddSingleTradeController#store', 'id'=> 'form', 'files'=>true]) !!}
<div class="row">
<div class="form-group col-sm-4">
{!! Form::label('exchange_id', 'Exchanges:') !!}
{!! Form::select('exchange_id', [''=>'Choose Options'] + $exchanges , null, ['class'=>'form-control', 'id'=>'exchange', 'name'=>'exchange_id', 'onchange' => 'all_function()'])!!}
</div>
<p id="exchange_value"></p>
{!! Form::close() !!}

Try this:
function all_function()
{
value = $("#exchange option:selected").text();
if (value != "")
{
$("#exchange_value").html("Selected Exchanges :"+value);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="exchange" id="exchange" onchange="all_function()">
<option value="1">abc</option>
<option value="2">def</option>
<option value="3">ghi</option>
<option value="4">jkl</option>
<option value="5">mno</option>
</select>
<div id="exchange_value"></div>

Related

Fom::select in laravel empty for default

I´m trying to create a form select in laravel.
I was created perfectly, but in my view first option is 0 and one first option empty
I need to remove this 0 and option empty, but i don't know how i can do it, this is my actual code:
<div class="form-group row ">
{!! Form::label('restaurant_id', trans("lang.blog_restaurant_id"),['class' => 'col-3 control-label text-right']) !!}
<div class="col-9">
{!! Form::select('restaurant_id', [null => null, $restaurant], null, ['class' => 'select2 form-control']) !!}
<div class="form-text text-muted">{{ trans("lang.blog_restaurant_id_help") }}</div>
</div>
</div>
$restaurant it's one data array for to fill my select. I read that with first option null put selected option empty, but generate a empty option and 0.
Thanks for help me.
UPDATE
Laravel include optgroup into Form::select that it´s triggering my 0 this it´s the problem. i need delete optgroup
<select class="select2 form-control select2-hidden-accessible" id="restaurant_id" name="restaurant_id" tabindex="-1" aria-hidden="true">
<option value="" selected="selected"></option>
<optgroup label="0">
<option value="17">Foody Lindgren, Cremin and Erdman gdfgfg fgfg fg fgfg g fggf</option>
<option value="28">restaurante de pruebas</option>
<option value="29">probando</option>
<option value="30">pincho de castilla2</option>
</optgroup>
update 2
getting list of restaurant:
$restaurant = $this->restaurantRepository->pluck('name', 'id');
this is result in blade:
{"17":"Foody Lindgren, Cremin and Erdman gdfgfg fgfg fg fgfg g fggf","28":"restaurante de pruebas","29":"probando","30":"pincho de castilla2"}
Add toArray():
$restaurant = $this->restaurantRepository->pluck('name', 'id');
$restaurant->toArray();
same result
Please try this. Add blank key and blank value like below.
{!! Form::select('restaurant_id', ["" => "", $restaurant], null, ['class' => 'select2 form-control']) !!}
It seems that the $restaurant is an array.
So, you'd better do something like this:
Form::select('restaurant_id', $restaurant, null, ['class' => 'select2 form-control'])
you may need a proper mapping for your $restaurant in your controller, depending on what you need, like adding an empty option in the beginning.
// in Controller
$options = ["" => "select"] + $restaurant;
// In view
Form::select('restaurant_id', $options, null, ['class' => 'select2 form-control'])
update 2
You should make sure to send the $restaurant as an array:
$restaurant = $this->restaurantRepository->pluck('name', 'id')->toArray();
For adding an empty option:
$restaurant = ["" => "select"] + $restaurant;
There is also another option:
You can even add the select box manually instead of using Form::select.
<select class="select2 form-control" id="restaurant_id" name="restaurant_id">
<option value="" selected="selected"></option>
#foreach($restaurant as $value => $option)
<option value="{{ $value }}">{{ $option }}</option>
#endforeach
</select>

Separating a sort by filter/select

I have a form where i search for products on a few subjects, i also have a html select working with a php switch to enable the ability to sort the order of the results. this currently works in the search form and i want to make the sort by html select to work without clicking the the search button and to run the correct select option every time its changed and to instantly change the order of the products to match the select option.
Here is the HTML:
{!! Form::open(['route' => 's.index', 'method' => 'GET']) !!}
<div class="form-group">
{!! Form::label('productsname', 'Search By Name:') !!}
{!! Form::text('productsname', null, array('class' => 'form-control')) !!}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{!! Form::label('categories_id', 'Search By Category:') !!}
<select class="form-control" name="categories_id" >
<option value=""></option>
#foreach($types as $type)
<option value="{{$type->id}}">{{$type->name}}</option>
#endforeach
</select>
</div>
</div>
<div class = col-md-6>
<div class="form-group">
</div>
</div>
<div class="form-group">
<select class="form-control" name="SortbyList" >
<option value="1">Highest Avg</option>
<option value="2">Lowest Avg</option>
<option value="3">Another Sort option</option>
<option value="2">another sort option</option>
</select>
{!! Form::submit('Find Products', array('class' => ' btn-lg btn-block')) !!}
{!! Form::close() !!}
</div>
Here is the PHP:
public function index(Request $request)
{
//
$productsQuery = Product::where('approved', '=', 1)->leftJoin('reviews', 'reviews.products_id', '=', 'products.id')->select('products.*', DB::raw('AVG(ratings) as ratings_average' ))->groupBy('products.id');
switch ($request->SortbyList) {
case 1:
$productsQuery = $productsQuery->orderBy('ratings_average', 'DESC');
break;
case 2:
$productsQuery = $productsQuery->orderBy('ratings_average', 'ASC');
break;
case 3:
$productsQuery = $productsQuery->orderBy('ratings_average', 'ASC');
break;
case 4:
$productsQuery = $productsQuery->orderBy('ratings_average', 'ASC');
break;
default:
$productsQuery = $productsQuery->orderBy('ratings_average', 'DESC');
}
$name=$request->input('productname');
$location=$request->input('categories_id');
if(!empty($name)){
$sightsQuery->where('productname', 'LIKE', '%'.$name.'%')->get();
}
if(!empty($location)){
$sightsQuery->where('categories_id', $request->input('categories_id') )->get();
}
$products= $productsQuery->paginate(8);
return view('p.search')->withproducts($products);
}
If I understood correctly, you want to change the order without clicking on the submit button (and so, without changing/reloading the page)?
PHP can not dynamically update your page, but Javascript can.

Laravel AJAX form (receiving data concern)

I have an issue recieving the data from a form which was modified with AJAX.
I have 2 fields: Cinema and Session.
The Session field updates automatically based on which cinema you have chosen. However, when I try to get the session field's value in my controller, it returns as "" (blank) where-as I want it to return what is actually displayed in the drop down box in the form.
Code:
Script:
<script>
$('#cinema').on('change', function(e){
console.log(e)
var cinema_id = e.target.value;
//ajax
$.get('{{ url('/ajax-subcat') }}?cinema_id=' + cinema_id, function(data){
//success data
console.log(data)
$('#sesh').empty();
$.each(data, function(index, subcatObj){
$('#sesh').append('<option value="'+subcatObj.id+'">'+subcatObj.session_time+'</option>');;
});
});
});
</script>
Controller:
public function cart()
{
$formData = array(
'cinema' => Input::get('cinema'),
'sesh' => Input::get('sesh'),
'ticketType' => Input::get('ticketType'),
'count' => Input::get('count')
);
$cinemaDetails = Cinema::find($formData['cinema']);
$session = Session::find($formData['sesh']);
return view('movies/ticketpage/cart')
->with('formData', $formData)
->with('cinemaDetails', $cinemaDetails)
->with('session', $session);
}
View/Blade file:
<div class="container">
<div class="row">
<div class="col-sm-4">
<h2>{{$movie->title}}</h2>
<img src="/WDAAssign2/Assign2-A/{{ $movie->image }}" height="200" width="150">
<p>{{ $movie->description }}</p>
</div>
<div class="col-sm-4">
<h2>Purchase tickets!</h2>
{!! Form::open(array('action'=>'MoviePageController#cart', 'files'=>true)) !!}
<label>Select a Cinema:</label><br>
<select id = "cinema" name = "cinema">
#foreach ($cinemas as $cinema)
<option value="{{$cinema->id}}">{{$cinema->name}}</option>
#endforeach
</select>
<br>
<label>Select a session:</label>
<br>
<select id = "sesh" name = "sesh">
<option value=""></option>
</select>
<br>
<label>Type of ticket:</label>
<br>
<select id= ="ticketType">
<option value="adult">Adult</option>
<option value="concession">Concession</option>
<option value="children">Children</option>
</select>
<br>
<label>Number of tickets:</label><br>
<select id = "count" name ="count">
#for ($i = 1; $i < 10; $i++)
<option value="{{$i}}">{{$i}}</option>
#endfor
</select>
<br><br>
<input type="submit" value="Submit">
{!!Form::close()!!}
</div>
</div>
</div>
Routes:
Route::post('/movie/ticketpage/cart', 'MoviePageController#cart');

dropdown show right value

ve two values in my combo box. Car and bicycle when the user selects one of them it will be stored in the database with 1 or 2. But how do I make sure that when the user edits it the chosen values shows up.
For example I've saved bicycle in the database than when the user wants to edit it it must show bicycle and not car!
I'm working with laravel.
EDIT
<div class="form-group">
<label for="content" class="col-lg-2 control-label">Standaard route</label>
<div class="col-lg-10">
#if($standaardroute->isEmpty())
<label>U heeft nog geen standaardroutes. Maak deze hier aan.</label>
#else
<select class="form-control input-sm" name="standaardroute" id="standaardroute">
#foreach($standaardroute as $route)
<option value="{!! $route->id !!}">{!! $route->van !!} - {!! $route->naar !!}</option>
#endforeach
</select>
</div>
</div>
#endif
EDIT
I've 2 dropdown menus. When one dropdown is selected the other one should receive a number from the database and show the right value for example bicycle. When the first dropdown menu is selected it post a request and receives the values back. When I receive information back for a text box I say:
$("#van").val(result.van);
But how do I have to do this with a dropdown menu?
<script>
$(document).ready(function () {
var xhr;
});
$("#standaardroute").change(function(e) {
csrf = $("#token").attr('content')
option = $(this).val();
$.ajax({
url: '/public/receiveuserinformation',
type: 'POST',
data: { option_id: option },
beforeSend: function(xhr){xhr.setRequestHeader('X-CSRF-TOKEN', csrf);},
success: function(result) {
$("#van").val(result.van);
//here should be something for the dropdownmenu
}
});
});
</script>
Suppose you've two options in an array: $types = array('1' => 'car', '2' => 'bicycle'); and you're saving it in products table and you've current product data in $product variable
<select>
<?php foreach($types as $type_id => type_name): ?>
<option value="<?php echo $type_id; ?>" <?php if($type_id == $product->type) echo "selected"; ?>>
<?php echo $type_name; ?>
</option>
<?php endforeach; ?>
</select>
EDIT: if you're using blade in laravel then you could simply achieve it by doing this:
{!! Form::label('type', 'Type: ') !!}
{!! Form::select('type', $types !!}
EDIT2: Try this:
<option value="{!! $route->id !!}" {{ ($standaardroute->route == $route->id) ? 'selected':'' }}>{!! $route->van !!} - {!! $route->naar !!}</option>
Note: replace $standaardroute->route your actual variable
EDIT3: Put this inside your success function:
$('#van option').each(function(){
$(this).removeAttr('selected');
if($(this).val() == result.van){
$(this).attr('selected', 'selected');
}
});
Note: I've not tested this

Laravel : Form select option value is always NULL when submitting

I have a form where I put de id's of my bands table inside a select option. When I select an id in the dropdownlist and try to submit the form, the value always remains NULL. How can I retrieve the value I selected?
create.blade.php file :
<form>
<select name="band_id">
#foreach ($bands as $band)
<option value="{{ $band->band_id }}">{{ $band->band_id }}</option>
#endforeach
</select>
<input type="submit" value="Submit">
</form>
My Bandcontroller inside my store action :
$bands->band_id = $input['band_id'];
First of all you are using #foreach ($band as $band), make sure it's not a typo and if not then fix it (Could be $bands as $band and assumed you know what I mean). Anyways, you may also try this:
{{ Form::select('band_id', $bands, Input::old('band_id')) }}
Just pass the $bands variable to the View and Laravel will create the SELECT for you, you don't need to loop manually. To retrieve the value, you may try this:
$band_id = Input::get('band_id');
Laravel 6 or above
//blade
{!!Form::open(['action'=>'CategoryController#storecat', 'method'=>'POST']) !!}
<div class="form-group">
<select name="cat" id="cat" class="form-control input-lg">
<option value="">Select App</option>
#foreach ($cats as $cat)
<option value={{$cat->id}}>{{ $cat->title }}</option>
#endforeach
</select>
</div>
<div class="from-group">
{{Form::label('name','Category name:')}}
{{Form::text('name','',['class'=>'form-control', 'placeholder'=>'Category name'])}}
</div>
<br>
{!!Form::submit('Submit', ['class'=>'btn btn-primary'])!!}
{!!Form::close()!!}
// controller
public function storecat(Request $request){
Log::channel('stack')->info('name'.$request->app);
if($request->input('cat')==null){ // retriving option value by the name attribute of select tag
return redirect(route('cats.cat'))->with('error', 'Select an app');
}
$this->validate($request,[
'name'=>'required',
]);
}

Categories