Selection box option issue in laravel - php

I had this problem while submitting form
blade
{{ Form::label('qualification', '') }}</th><td>{{ Form::select('qualification', $qualification, $jobs_list->qualification,array('class' => 'form-control')) }}
In this $qualification my full database list is there and $jobs_list->qualification is my selected value.but again am changing value and submitting form it stored as number because ex : value ="numbers"
controller:
$field = ToQualification::select('value')->get();
$qualification = array();
foreach($field as $user_qualification){
$qualification[] = $user_qualification->value;
}
generated html by blade code:
<select class="form-control" id="qualification" name="qualification">
<option value="0">Associate's</option>
<option value="1">Bachelor's</option>
<option value="2">Diploma</option>
<option value="3" selected="selected">Doctoral</option>
<option value="4">High Schools</option>
<option value="5">Master's</option>
</select>
anyone tell how to display name and id in table same as value in selection box?

Related

Issue With option Tag In HTML While Storing in Database By PHP

I want to save value as Hsn/product code and option text as product.
i defined value for hsn because i want to show it on my active webpage as Selected option Hsn code.
but problem is that when i submit form, then i recieve same value in productName & ProductHSN in our Database
And i want to store productName as saree, lehga, etc
And ProductHSN as W1, M1,etc .
plase help
<select class="form-control" id="colTwo2" name="colTwo[]" data-
type="productName" required="">
<option value=""> --SELECT-- </option>
<option value="W1">Saree</option>
<option value="W2">Lehga</option>
<option value="G1">Kurti</option>
<option value="G2">Salwar Suit</option>
<option value="G3">Lagi</option>
<option value="G4">Hairam</option>
<option value="G5">Long Suit</option>
<option value="W3">Goun</option>
<option value="W10">Chunri</option>
<option value="M1">Suiting</option>
<option value="M2">Shirting</option>
<option value="G6">Jeans</option>
<option value="M4">T-Shirt</option>
</select>
You are getting same value because the value in option value="" is only sent. Not the option text.
You could use PHP explode function.
HTML
<select class="form-control" id="product" name="product" required>
<option value=""> --SELECT-- </option>
<option value="W1|Saree">Saree</option>
<option value="W2|Lehga">Lehga</option>
<option value="G1|Kurti">Kurti</option>
<option value="G2|Salwar Suit">Salwar Suit</option>
</select>
PHP
<?php
$result = $_POST['product'];
$result_explode = explode('|', $result);
$producthsn = $result_explode[0];
$productname = $result_explode[1];
?>
In the select box we are giving 2 options with a '|' separator to split the values. In action page result is exploded into an array. You can then insert each one separately in the database.

How to get id of selected value from dropdown and use it in other dropdown in laravel?

I'm newbie at Laravel and on stackoverflow. I'm making an app for registration. I want to show all those ids of section where class_id is selected in upper dropdown.
$cid=DB::table('classses')->get();
$counterr=0;
$sec=DB::table('sections')->where('class_id',$cid)->get();
$counterrr=0;
Dropdown menu for Class
<div class="col-md-6">
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $cc)
#if($counterr==0)
<option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
{$counterr++}}
#else
<option value="{{$cc->id}}">{{$cc->title}}</option>
#endif
#endforeach
</select>
Dropdown menu for Section where I want to get all the values of section where class_id is upper selected
<select section="" name="section_id">
<option value="null">Section</option>
#foreach($sec as $sc)
#if($counterrr==0)
<option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
{{$counterrr++}}
#else
<option value="{{$sc->id}}">{{$sc->title}}</option>
#endif
#endforeach
</select>
Please help me in these case. You can freely ask anything if you want to.
You do not need to set a $counter in order to set the first select option to selected. Instead, you can check if the $key == 0:
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $key => $cc)
<option {{ $key == 0 ? 'selected="selected"' : '' }} value="{{$cc->id}}">{{$cc->title}}</option>
#endforeach
</select>
Here we use a ternary expression to echo out the selected option based on if $key equals 0. If not, it will not echo out anything.

Laravel: Use same column as option name and its value in select box/drop-down?

I'm trying to create a form with a select box in Laravel.
View
{{ Form::select('cmp_type', $cmp_types, null, $attributes = ['class' => 'form-control']) }}
Controller
public function showAddCompany()
{
$cmp_types = cmpTypes::where('status', true)->pluck('type');
return view('addCompany', compact('cmp_types'));
}
This is generating a select box.
<select class="form-control" id="cmp_type" name="cmp_type">
<option value="0">public</option>
<option value="1">pvt Ltd</option>
<option value="2">LLP</option>
<option value="3">NPO</option>
<option value="4">partnership</option>
<option value="5">proprietorship</option>
<option value="6">one person</option>
</select>
What can I do to get the select box as?:
<select class="form-control" id="cmp_type" name="cmp_type">
<option value="public">public</option>
<option value="pvt Ltd">pvt Ltd</option>
<option value="LLP">LLP</option>
<option value="NPO">NPO</option>
<option value="partnership">partnership</option>
<option value="proprietorship">proprietorship</option>
<option value="one person">one person</option>
</select>
I believe what is happening is that when you call pluck, you are being returned a numeric array (Collection?) of the values and those numeric keys are being used for the value property and the actual value of said key is being used as the display, what if you did:
$cmp_types = cmpTypes::where('status' , true )->pluck('type');
$cmp_types = array_combine($cpm_types, $cmp_types);
Reading Material
array_combine
array_combine() helped me.
the solution is:
public function showAddCompany()
{
$cmp_types_obj = cmpTypes::where('status' , true )->pluck('type');
$cmp_types = json_decode($cmp_types_obj);
$cmp_types = array_combine($cmp_types, $cmp_types);
return view('addCompany', compact('cmp_types'));
}
my query is returning an object, so i converted it into an array first and then i combined it with itself to get an associate array as
['public']=>'public,['private']=>'private',...

Getting 'old' value of a select input laravel

I'm working on validating my registration form.
If the input is a regular input, i can populate the previous value if it fails like so
<input type="tel" name="phone" value="{{old('phone')}}">
How do I populate the value on a select option?
<div class="input--select">
<label>Age Range <span>*</span></label>
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
<option selected disabled>Select your age range</option>
<option value="18-25">18-25</option>
<option value="26-35">26-35</option>
<option value="36-45">36-45</option>
<option value="46-55">46-55</option>
<option value="56-65">56-65</option>
<option value="66-75">66-75</option>
<option value="75+">75+</option>
</select>
#if ($errors->has('age_range')) <span class="error-message">Age range option is required.</span> #endif
</div>
I suggest putting all you ages in an array and pass that array to view:
$ages = ['18-25','25-50','50-75','75+']; // More dynamic and you can extend in anytime
Now changes in html :
<div class="input--select">
<label>Age Range <span>*</span></label>
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
// 1. first change which is creating your problem
<option {{ old('age_range') ? "" : "selected" }} disabled>Select your age range</option>
// 2. This is just optimization for short code
#foreach($ages as $age)
<option {{old('age_range') ==$age" ? $selected : ""}} value="{{$age}}">{{$age}}</option>
#endforeach
</select>
#if ($errors->has('age_range')) <span class="error-message">Age range option is required.</span> #endif
</div>
So in point 1 you are always applying the selected with first option.
Suppose your old value was 46-55
your html looks like :
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
<option selected disabled>Select your age range</option>
<option value="18-25">18-25</option>
<option value="26-35">26-35</option>
<option value="36-45">36-45</option>
<option selected value="46-55">46-55</option>
<option value="56-65">56-65</option>
<option value="66-75">66-75</option>
<option value="75+">75+</option>
</select>
if you take a look at above html there are 2 selected options. Html always picks the first one this is what creating the problem.
Point 1. will check if there is a old value available it will not apply the selected to the first placeholder option.
{!! Form::select('name', $varableyouwanttodisplay, old('name'),
['id'=>'id', 'class' => 'form-control select2', 'data-
placeholder' => 'select','required', 'style' => 'width:100%']) !!}
try like above code.
#php $selected = old('experience') ?: 66; #endphp
and than
{{ Form::select('experience', $experience, $selected, ['class' => 'form-control']) }}
This works in a case when a blade template loads for the first time and doesn't have 'old' value and you need predefined selected option(I.e. 66) for that case.

How do I Pass Values from One Form to Another?

I have this form:
<form name="summaryform">
<select name="category" id="category" style="width:500px;">
<option value="">Choose category...</option>
<option value="ActionScript">ActionScript</option>
<option value="AppleScript">AppleScript</option>
<option value="Asp">Asp</option>
<option value="BASIC">BASIC</option>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Clojure">Clojure</option>
<option value="COBOL">COBOL</option>
<option value="ColdFusion">ColdFusion</option>
<option value="Erlang">Erlang</option>
<option value="Fortran">Fortran</option>
<option value="Groovy">Groovy</option>
</select><br>
<select name="subcategory" id="subcategory" style="width:500px;">
<option value="">Choose sub category...</option>
<option value="Haskell">Haskell</option>
<option value="Java">Java</option>
<option value="JavaScript">JavaScript</option>
<option value="Lisp">Lisp</option>
<option value="Perl">Perl</option>
<option value="PHP">PHP</option>
<option value="Python">Python</option>
<option value="Ruby">Ruby</option>
<option value="Scala">Scala</option>
<option value="Scheme">Scheme</option>
</select><br>
<input type="text" name="comments" id="comments" value=" Enter request comments..." onfocus="clearText(this)" onblur="restoreText(this)" style="width:493px;color:#999;font-size:9pt;height:20px;">
On the form above, I have two dropdowns (name="categoryId" and "subcategoryid") and one textboxt (name="comments")
How do I pass the values from this form to another form?
Let's say below is the form I want to pass the values to:
<div>
<p>
<form name="summaryform" method='POST' action='process.php'>
<div style="font-size:12pt;">
value from one dropdown
value from the other dropdown
value from textbox
</form>
</p>
</div>
jQuery Method
If the forms are on the same page, you can use jQuery to get the values and write to the div:
var content = $('#category').val()+'<br>'+$('#subcategory').val()+'<br>'+$('#comments').val();
$('#div-to-write-to').html(content);
PHP Method
If the forms are on different pages, you will need to use PHP and the $_POST[] variable to pass the values you are looking for.
NOTE: for this method to work the page being POSTed to MUST be PHP. You will also need to add an action and method in the first form to POST to the second form.
This would be the code in the form on the page being POSTed to:
<?php echo $_POST['category'].'<br>'.$_POST['subcategory'].'<br>'.$_POST['comments']; ?>
A value can be passed between forms using the POST and GET methods. The first form (Which is sending the values) does not have this defined, so modify the first form to this -
<form name="summaryform" method='POST' action='process.php'>
Then, in process.php, put the following php code -
$dropdown_first = $_POST['category'];
$dropdown_second= $_POST['subcategory'];
$comment = $_POST['comments'];
After that, you can just use the variables as you see fit!
If you want just print the selected values from first form #zsaa14's
answer is good
If you want to print whole select list, autoselected your value, heres a code.
foreach($categories as $category) {
$selected = $category['name'] === $_POST['category'] ? 'selected="selected"' : '';
echo " <option value="{$category['name']}" {$selected}>{$category['name']}</option>" ;
}
NOTE: the variable names and keys are for example, use Yours. This is only fragment of code.
You can use post data fields to send the values to the next form

Categories