Php form set selected

Setting selected option in laravel form

Everybody talking about you go using but, if all you need is to use plain simple HTML.. here is another way to do it.

  

the old() function is useful when you submit the form and the validation fails. So that, old() returns the previously selected value.

  

You have to set the default option by passing a third argument.

You can read the documentation here.

@JuniorMinKhant Glad to know its helped.Please mark this as an answer so that other can find it helpful.

that’s for Laravel 4.2 .. If you want to use Form::select on Laravel 5.1+ you need to pull in laravelcollective/html package

what if it was a multi select where multiple items are checked? i have a multi-select with checkboxes, see multiselect-ui

use this package and check the docs:

form you html , you need use this mark

Another ordinary simple way this is good if there are few options in select box

  

Setting selected option is very simple in laravel form :

you answered same answer as @chonchol about 2 hrs later? i would poin tit, but i pointed first to answer. or did somebody edit theirs?

 @foreach ($categories as $category)  @endforeach 
 get()->pluck('name','id'); $selectID = 3; ?> 
> 'form-control']) !!>

This show similar types of following options :

  
 

To echo some other answers here, the code I just used with 5.6 is this

 'Draft', 'Sent' => 'Sent', 'Paid' => 'Paid'], $model->status, ['id' => 'status']) >> 

In order to be able to use the Form Helper from LaravelCollective I took a look at https://laravelcollective.com/docs/master/html#drop-down-lists

I also had to composer require the dependency also so that I could use it in my projects

composer require "laravelcollective/html":"^5" 

Lastly I altered my config/app.php and added the following in the $aliases array

 'Form' => Collective\Html\FormFacade::class, 

Источник

How do I set the selected item in a drop down box

The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?

13 Answers 13

You need to set the selected attribute of the correct option tag:

Your PHP would look something like this:

I usually find it neater to create an array of values and loop through that to create a dropdown.

Erk sorry, didn’t see your mostly identical comment before posting mine. I’ll upvote yours but leave it to the questioner to pick a winner. Btw, does your opening PHP tag have a typo? or is

In MS Expression Web I get a warning that this (php script) is not permitted inside the HTML5

You mark the selected item on the tag, not the tag.

So your code should read something like this:

  

You can make this less repetitive by putting all the month names in an array and using a basic foreach over them.

Pro Tip: there is absolutely no advantage/benefit in repeating the option’s text as the value declaration.

You can use this method if you use a MySQL database:

include('sql_connect.php'); $result = mysql_query("SELECT * FROM users WHERE `id`!='".$user_id."'"); while ($row = mysql_fetch_array($result)) < if ($_GET['to'] == $row['id']) < $selected = 'selected="selected"'; >else < $selected = ''; >echo(''); > mysql_close($con); 

It will compare if the user in $_GET[‘to’] is the same as $row[‘id’] in table, if yes, the $selected will be created. This was for a private messaging system.

Simple and easy to understand example by using ternary operators to set selected value in php

Источник

Specify a default selected item for HTML form drop-downs

The syntax for boolean attributes such as selected is either just selected or selected=»selected» . selected=»true» is wrong.

@RoToRa according to the spec, the value can be anything, so selected=»true» isn’t «wrong» as such, but it does go against convention.

@roryf The HTML 4.01 spec refers to w3.org/TR/html401/intro/sgmltut.html#h-3.3.4.2 which doesn’t say that.

7 Answers 7

There is no attribute like that on the element. Assuming your output is in a loop, I don’t see how it makes a huge difference:

$selected = "2"; foreach($values as $key => $val) < echo ""; > 

(my PHP is a little rusty, that may not be 100% correct)

No, but you can add your default value to your id like

then in your options, you have

or something to that effect(forgive me i forgot my php syntax, but i hope you get the point).

Anyway, that is a dirty fix also, so i suggest just adding a method to check for the default selected tag and printing it selected=»selected» when it is the default. You can just call it once if you loop through your select options

That won’t work since it’s the presence of the selected attribute that infers ‘selectedness’, irrespective of it’s value.

Put JavaScript after the declaration of the listbox and set the selected index there:

  

Could someone seriously explain to me why using JavaScript would be a good (up-voteable) solution in this case? (The explanation «I don’t care for users without JS» is not valid)

I’m not saying that this is an ideal solution, but it could be a good workaround. The best of course would be generating html on the server side as explained in the most popular answer. Sometimes web applications are to be used in intranets and it can be assumed that JS is on by policy. Anyway thank you for pointing out that JavaScript works only if it’s enabled.

Its the best workaround, agreed. The other solutions simply reiterate the cause of the problem as an answer, and expect me to applaud them. Absolute rubbish.

@Jenko I did answer your question, the answer was no. The point I was trying to make is that if your output is in a loop, it is only defined in one place as it is, so where is the problem?

I can’t believe someone would consider this a good, or even the best workaround, especially since a simple function such as roryf’s answer would solve the problem perfectly.

Источник

Defining selected option of html form in PHP

$value is a variable which comes from PHP codes. This methods seems to be very simple and naive. Is it the best way to do so?

You’d better loop through all options, and create the

6 Answers 6

$options = array(10,20,50); foreach($options as $option) < $selected = ($value == $option) ? ' selected="selected"' : ''; echo ''; > 

Looks like somebody has been systematically downvoting every answer to this question without comment

Not a bad way to do it. But why not create the entire thing using programmatic approach (i.e., generate the code using a PHP loop through an array):

$items = array(10, 20, 50); for ($i = 0; $i < count($items); $i ++) < echo(""); > 

Something along the lines of the below should work:

Keep your values in an array and loop over it to generate the HTML. Then you don’t need to repeat yourself.

Would be cleaner if you separated the presentation from the logic. It’s been years since PHP was introduced as a template engine.

While I agree that a templating engine would be the best approach, I think this solution is simpler, clearer and more maintainable than having a function that vomits html and much more straightforward than having to learn a template engine

Источник

Читайте также:  List set list питон
Оцените статью