PHP Some of the default selection methods for radio, check boxes (example)

Source: Internet
Author: User

I. Radio and checkboxes and PHP Select Implementation code selected by default

1. Radio the default selection of the single box

code example:

<input type= "Radio" name= "Dishu" value= "3" <?php if ($myrow [dishu]==3) echo ("checked");? >> blank <input type= "Radio" name= "Dishu" value= "1" <?php if ($myrow [Dishu]==1) echo ("checked");? >> sell <input type= "Radio" name= "Dishu" value= "2" <?php if ($myrow [dishu]==2) echo ("checked");? >> transfer <input type= "Radio" name= "Dishu" value= "4" <?php if ($myrow [dishu]==4) echo ("checked");? >> Collective

2. The default selection of the CheckBox check box is implemented

code example:

<input name= "Fujia_jiaju" type= "checkbox" value= "Jiaju" <?php if ($myrow [Fujia_jiaju]) echo ("checked");? >></font></strong> Furniture </td><td width= "108" height= "1" ><strong><font color= "# 008080 "face=" Italic _gb2312 "><inputname=" Fujia_chuju "type=" checkbox "value=" Chuju "<?php if ($myrow [Fujia_ Chuju]) echo ("checked");? >></font></strong> kitchenware </td><td width= "height=" 1 "><strong><font color=" # 008080 "face=" Italic _gb2312 "><inputname=" Fujia_woju "type=" checkbox "value=" Woju "<?php if ($myrow [Fujia_woju]) Echo ("checked");? >></font></strong> bedding </td><td width= "height=" 1 "><strong><font color=" # 008080 "face=" Italic _gb2312 "><inputname=" Fujia_tv "type=" checkbox "value=" TV "<?php if ($myrow [FUJIA_TV]) echo (" Checked ");? >></font></strong> TV

These form fields work together to modify some database records and keep the original records as they were added!

3. Select Default Selection

code example:

<form name= "form" method= "POST" action= "enctype=" Multipart/form-data "><select name= ' aaa ' id= ' BBB ' >< Option value= ' > Provinces category---</option><option value= ' Inner Mongolia ' > Inner Mongolia </option><option value= ' Xinjiang ' > Xinjiang </option><option value= ' Yunnan ' > Yunnan </option><option value= ' Tibet ' > Tibet </option><option Value= ' Gansu ' > Gansu </option><option value= ' Ningxia ' > Ningxia </option><option value= ' Qinghai ' > Qinghai </option ><option value= ' Sichuan ' > Sichuan </option><option value= ' Guangxi ' > Guangxi </option><option value= ' Guizhou ' > Guizhou </option><option value= ' Hainan ' > Hainan </option><option value= ' Guangdong ' > Guangdong </option><option Value= ' Hunan ' > Hunan </option><option value= ' Hubei ' > Hubei </option><option value= ' Fujian ' > Fujian </option ><option value= ' Jiangxi ' > Jiangxi </option><option value= ' zhejiang ' > Zhejiang </option><option value= ' Anhui ' > Anhui </option><option value= ' Jiangsu ' > Jiangsu </option><option value= ' Henan ' > Henan </option>&Lt;option value= ' Shaanxi ' > Shaanxi </option><option value= ' Shandong ' > Shandong </option><option value= ' Hebei ' > Hebei </option><option value= ' Shanxi ' > Shanxi </option><option value= ' Heilongjiang ' > Heilongjiang </option><option Value= ' Jilin ' > Jilin </option><option value= ' Liaoning ' > Liaoning </option></select></form><script Type= "Text/java script" ><!--document. FORM.bbb.value = "Shandong";//form is the table sole name, atc_province is the select tag ID, Shandong is the select default selection//--></script>

4, the default choice of PHP Select implementation method

code example:

<select name= "wuyeleixing" size= "1" ><option <?php if ($myrow [Wuyeleixing]==1) Echo ("selected");? > value= "1" > Residential </option><option <?php if ($myrow [wuyeleixing]==2) Echo ("selected"); > value= "2" > Office </option><option <?php if ($myrow [wuyeleixing]==3) Echo ("selected"); > value= "3" > Business room </option><option <?php if ($myrow [wuyeleixing]==4) Echo ("selected"); > value= "4" > Workshop </option><option <?php if ($myrow [wuyeleixing]==5) Echo ("selected"); > value= "5" > Warehouse </option><option <?php if ($myrow [wuyeleixing]==6) Echo ("selected"); > value= "6" > Plots </option><option <?php if ($myrow [wuyeleixing]==7) Echo ("selected"); > value= "7" > Complex building </option><option <?php if ($myrow [wuyeleixing]==8) Echo ("selected");? > value= "8" > Farm Ranch </option><option <?php if ($myrow [wuyeleixing]==9) Echo ("selected");? > value= "9" > Business house </option><option <?php if ($myrow [wuyeleixing]==10) Echo ("Selected");? > value= "Ten" > hotel restaurant </option><option <?php if ($myrow [wuyeleixing]==11) Echo ("selected"); > value= "One" > School buildings </option></select>


Two. Select Label, Radio box, check box to set the default selection method

In the background information changes, you need to display the original information, but if in <select >,<input type= "Radio" > and <input type= "checkbox" > Display, To show which one is selected, there is some trouble!

If, according to the following:

<input type= "Radio" name= "status" value= "0" checked= "${advert.status==0?" Checked ': '} '/>/>

Because, every radio will have checked, so the display will be a problem!
At this point, you can use the <c:choose><c:when/><c:otherwise/><c:choose/> in Jstl to judge, and then the line display. The disadvantage is that the amount of code is more, if there are multiple such tags, it will be very cumbersome.

So, choose a different approach:

<input type= "Radio" name= "status" value= "0" <c:out value= "${advert.status==0?" Checked ': '} '/>/>

In this way, it is convenient to have a tedious judgment.

Summarize:
1. In <select >,<input type= "Radio" > and <input type= "checkbox" > As long as the checked or select will be selected, whether it is checked = "Checked" in the form.
2. El expression ${} and jstl<c:out> two can be displayed directly on the Web page, but some cases ${} can not be recognized, so if the use of <c:out> to display, the interaction between the two, the combination is used, the effect is better.


Three. JS implements the default selection for radio and select

1, radio default selected JS code, where <%=order.gettype ()%> is the background data.

code example:

<script>//radio Default Check function init () {  var v = ' <%=order.gettype ()%> ';  var type = Document.getelementsbyname ("OrderType");  for (var i = 0; i < type.length; i = i + 1) {  if (Type[i].value = = v) {  type[i].checked = "checked";  return;  }  } }</script>

2, select the default selected JS code, where <%=order.gettype ()%> is the background data.

code example:

<script>//select Default Check//by www.function init () {  var v = ' <%=c.getlevel ()%> ';  var type = document.getElementById ("level");  for (i = 0;i<=type.options.length;i++) {if (Type.options[i].value = = v) {type.options[i].selected = ' selected ';}}} </script>

3. Invoking the example

<body onload= "Init ();" >
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.