Repopulate values in select field - php

I want to repopulate the old value after validation.I have tried many options and I am not getting.
Here is my code
select name="Skills[]" class="form-control select2" multiple="multiple" id ="Skills"
#foreach($skills as $skill)
option value="{{$skill->skill_id}}" >{{$skill->skill_name}}</option>
#endforeach
/select

correct me if I'm wrong but what you mean by repopulate the old value is to show as selected the option chosen by the user after validation right?
if so then you can try after redirecting to a View the following:
<?php $i=1; ?>
<select name="Skills[]" class="form-control select2" multiple="multiple" id ="Skills">
#foreach($skills as $skill)
<option value="{{$skill->skill_id}}" #if($skill->skill_id==$i++){'selected="selected"'}#endif>{{$skill->skill_name}}</option>
#endforeach
</select>

Related

Select field Old value cannot be displayed in laravel

I'm having a user registration form with a select field, When ever I get an error on the form user redirected to the form and display the previously entered values
I able to display old values for the text fields but I am finding it difficult to display old values for the select field,
My select field as follows,
<select id="app-subdomainsuffix" class="form-control #error('subDomainSuffix') is-invalid #enderror" name="subDomainSuffix" aria-required="true">
<option value="">- {{ __('sentence.Select domains') }} -</option>
<option value="test.site" >TEST.SITE</option>
</select>
I'm using laravel 7
First of all you need value for each option and i don't see any value for your first option.
Then you need a logic to check if the option was selected and assign "selected Attribute" to HTML :
<select id="app-subdomainsuffix" class="form-control #error('subDomainSuffix') is-invalid #enderror" name="subDomainSuffix" aria-required="true">
<option value="value1" #if(old('subDomainSuffix') == "value1") selected #endif >- {{ __('sentence.Select domains') }} -</option>
<option value="test.site" #if(old('subDomainSuffix') == "test.site") selected #endif >TEST.SITE</option>
</select>
Above example can just demonstrate how to select the option, but for cleaner code you better come with array of option as mentioned in this topic .
<option value="test.site" {{ (old("subDomainSuffix") == 'test.site' ? "selected":"") }}>TEST.SITE</option>

How to fix php getting select option value instead of text?

I have issues posting my select option values from html form to php back-end
Here are my codes:
<form action="submit.php" method="POST">
<select name="approver" class="form-control" autofocus>
<option selected disabled value="0">— Select Approver —</option>
<option selected disabled value="1">User A</option>
<option selected disabled value="2">User B</option>
</select>
</form>
submit.php:
$approver = mysqli_real_escape_string($conn, $_POST['approver']);
Is there a way for php to receive the value of the option, eg. 1, instead of the text inside the option tag?
Thanks in advance
"selected disabled" attributes should be used for the first option(generally which is not really an option for the user to select). It is used so that the user cannot choose the first option again after changing the option. Remove the "selected disabled" attributes from other options.
<select name="approver" class="form-control" autofocus>
<option selected disabled value="0">— Select Approver —</option>
<option value="1">User A</option>
<option value="2">User B</option>
</select>

sending value inside <options> tag through url

Hey guys I'm using the Laravel framework. I fetched data from my database and populated my drop down list like this.
<select class="form-control" id="username" name="username">
<option value="" selected>Select User</option>
#foreach($getUsers as $list)
<option value="{{$list->id}}" >{{$list->name}}</option>
#endforeach
</select>
I want to be able to click one of the options in the drop down list and send the value through a url (preferably using href) and reload the page.
You could use a simple javascript function bound to the onchange event of the select menu like this.
<script>
function offyoupop(e){
location.search='username='+e.target.value;
}
</script>
<select name='username' id='username' class='form-control' onchange='offyoupop(event)'>
<option>Please select
<option value='1'>One
<option value='2'>Two
<option value='3'>Three
<option value='4'>Four
<option value='5'>Five
</select>

Whats the possibility of one updating a database field with a form having a drop down without changing the value of the drop down field

Lets say i input data into a mysql database using a drop down like:
<div>
<input name="name" type="text" id="name" placeholder="Enter Name"></div>
<div>
<select name="position" id="position" type="text">
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
With a normal text field, i can simply include the value field to show on the form the initially entered data in the table. And change that so when i submit form, it updates the related field.
<div>
<select name="position" id="position" type="text" value="<?php echo $row_position['Position']; ?>>
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
So assuming i do not want to update the field relating to the drop down, how do i it? Because what happens is, once i submit the form without selecting any option from the drop down, it picks the first option from the list which is the "Select" option and uses that to update the related field in the database.
I'd suggest at least two steps to make it user-friendly and achieve your goals:
First, make the initially entered selection selected if available:
<?php
$positions = Array("A", "B", "C");
?>
<div>
<select name="position" id="position">
<option value="-1">Select</option>
<?php
foreach($positions as $v) {
$selected = ($row_position['Position']===$v) ? "selected" : "";
echo "<option value='$v' $selected>$v</option>";
}
?>
</select>
</div>
And on the receiving php-script check if you've received a valid option, otherwise send error-message:
<?php
//other stuff...
if($_POST['position']>0) {
// save that value
} else {
// send error to user
}
// even more stuff..
?>
Notes: in select tag there is no type=text nor a value=anything available.
Assuming you are fetching dropdown options from database.
Note: Array keys, variables are only for demonstration purposes. This may differ from your actual records.
<div>
<select name="position" id="position">
<?php foreach($options as $option): ?>
<option value="<?= $option['position'] ?>" <?php if($option['position'] == $current_record['position']) { echo "selected"; } ?>> <!-- e.g. $option['id'] -->
<?= $option['name'] ?>
</option>
<? endforeach; ?>
</select>
</div>
What i have done that has solved the issue was to simply add the value from the database to the selectedoption or the first option field. So by default without changing the drop down, the first option with the value from the database is selected. And so there would be no change.
<div>
<div><?php echo $row_position['Position']; ?></div>
<select name="position" id="position">
<option value="<?php echo $row_position['Position']; ?>Select to Change</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
This gives a simple or straight forward solution to the problem.
Will still try the other suggestions to see how it all plays out. Thanks guys.

what is the function of attribute selected in combo box while fetching data from MYSQL in php

Hello i am beginner in PHP. I am trying to fetch data from MYSQL and want to show in combo box but if user does not re selects the item from item list it highlights error that nothing is selected. Is there any php function to do this particular task ? I want that when the data of particular id is loaded in form and if without making any change in combo boxes data user cliks on submit then it must submit but in my case it shows that the fields are empty while data is here. Here is my Code
<li id="foli3" class="notranslate ">
<label class="desc" id="title3" for="Field3">
what is the current occupation of your guardian?
<span id="req_3" class="req">*</span>
</label>
<span>
<select id="Field3" name="occupation" class="field select addr" tabindex="8" required >
<option value="" selected="selected"><?php echo $rows['occupation']; ?></option>
<option>Private Sector Employee</option>
<option>Social Sector Employee</option>
<option>Agriculturalist</option>
<option>Businessman</option>
<option>Government Employee</option>
<option>Other</option>
</select>
</span>
</li>
use jQuery to select default value for combo box.
<select id="mySelect">
<option value="foo">Foo</option>
<option value="bar">Bar</option>
<option value="quux">Quux</option>
</select>
$("#mySelect").val("quux");
Try to use option with value attribute:
e.g.
<option value="Private Sector Employee">Private Sector Employee</option>
<option value="Social Sector Employee">Social Sector Employee</option

Categories