16693

Selecting dynamic default value in

Question:

I have a php script which updates user info. I have created an html form, which allows user to edit the values and enter new ones. Now when the form is loaded for the first time, it displays some default values taken from php variables. The works fine but the problem is with tag.

<input type = "text" name = "name" class = "text" value = "<?php echo $user->name; ?>" />

this works fine..

<select name = "department" value = "<?php echo $user->department; ?>"> <option>Information Technology</option> <option>Computer Science</option> <option>Electronics & Communication</option> <option>Mechanical Engineering</option> <option>Civil Engineering</option> <option>Electrical & Electronics Engineering</option> <option>M.Tech</option> <option>MBA</option> <option>MCA</option> </select>

how can I solve this issue?

Answer1:

The Select tag doesn't have a value option. Use this instead:

For example:

<select name = "department"> <option value="IT" <?php if ($user->department == "IT") echo "selected='selected'";?> >Information Technology</option> <option value="CS" <?php if ($user->department == "CS") echo "selected='selected'";?> >Computer Science</option> </select>

Answer2:

Something like this would work

<select name = "department" value = "<?php echo $user->department; ?>"> <option value="1" <?= ( $user->department == 1 ? 'selected="selected"' : '' ) ?>> Information Technology </option> ... </select>

Answer3:

This can be solved with one single if statement.

For example:

<select name = "department"> <option value="IT">Information Technology</option> <option value="CS">Computer Science</option> <?php if (strlen($user->department)>1 ) echo "<option value="."$user->department"."selected='selected >"."$user->department"."</option>";?> </select>

Recommend

  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Meteor helpers not available in Angular template
  • Get one-time binding to work for ng-if
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Volley JsonObjectRequest send headers in GET Request
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Modifying destination and filename of gulp-svg-sprite
  • How to extract text from Word files using C#?
  • ILMerge & Keep Assembly Name
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • Importing jscolor library in angular 2
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Large data - storage and query
  • ORA-29908: missing primary invocation for ancillary operator
  • How can I estimate amount of memory left with calling System.gc()?
  • WOWZA + RTMP + HTML5 Playback?
  • GridView Sorting works once only
  • Cannot Parse HTML Data Using Android / JSOUP
  • Hits per day in Google Big Query
  • Java static initializers and reflection
  • Bitwise OR returns boolean when one of operands is nil
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Does armcc optimizes non-volatile variables with -O0?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal