2016年2月11日 星期四

HTML escape characters


有時候在輸入字串時
字串裡的字會跟HTML TAG
相同而造成把字串當成HTML TAG
結果網頁輸出錯誤的內容
這時候就要用escape characters

較常使用的escape characters

  • Space    
  • "           "
  • &         &
  • <          &lt;
  • =          &#613
  • >          &gt;


Symbol Entity Name
&euro;
Space &nbsp;
" &quot;
& &amp;
< &lt;
= &#61;
> &gt;
Non-breaking space &nbsp;
¡ &iexcl;
¢ &cent;
£ &pound;
¤ &curren;
¥ &yen;
¦ &brvbar;
§ &sect;
¨ &uml;
© &copy;
ª &ordf;
¬ &not;
­ &shy;
® &reg;
¯ &macr;
° &deg;
± &plusmn;
² &sup2;
³ &sup3;
´ &acute;
µ &micro;
&para;
· &middot;
¸ &cedil;
¹ &sup1;
º &ordm;
» &raquo;
¼ &frac14;
½ &frac12;
¾ &frac34;
¿ &iquest;
À &Agrave;
Á &Aacute;
 &Acirc;
à &Atilde;
Ä &Auml;
Å &Aring;
Æ &AElig;
Ç &Ccedil;
È &Egrave;
É &Eacute;
Ê &Ecirc;
Ë &Euml;
Ì &Igrave;
Í &Iacute;
Î &Icirc;
Ï &Iuml;
Ð &ETH;
Ñ &Ntilde;
Ò &Ograve;
Ó &Oacute;
Ô &Ocirc;
Õ &Otilde;
Ö &Ouml;
× &times;
Ø &Oslash;
Ù &Ugrave;
Ú &Uacute;
Û &Ucirc;
Ü &Uuml;
Ý &Yacute;
Þ &THORN;
ß &szlig;
à &agrave;
á &aacute;
â &acirc;
ã &atilde;
ä &auml;
å &aring;
æ &aelig;
ç &ccedil;
è &egrave;
é &eacute;
ê &ecirc;
ë &euml;
ì &igrave;
í &iacute;
î &icirc;
ï &iuml;
ð &eth;
ñ &ntilde;
ò &ograve;
ó &oacute;
ô &ocirc;
õ &otilde;
ö &ouml;
÷ &divide;
ø &oslash;
ù &ugrave;
ú &uacute;
û &ucirc;
ü &uuml;
ý &yacute;
þ &thorn;

Python string formatting

Python string formatting有二種
% 和 format method
%是比較舊的方式
建議使用format,比較恰當
在讀程式時較易理解


s1 = "John!"
s2 = "love"

print "i am  %s" % s1     #i am John
print "i am  {0}".format(s1) #i am John

print "with %(kwarg)s!" % {'kwarg':sub2} #with love
print "with {kwarg}!".format(kwarg=sub2) #with love

2016年2月10日 星期三

HTML select

<select>可以用做下拉是選單
搭配<option>做為選擇的值
而query parameter就是option的值
跟checkbox和radio不同
query parameter一定有值
代表至少會是其中一個option裡的值


<form action="/testform">
   <select name="q">
    <option>one</option>
    <option>two</option>
    <option>three</option>
   </select>
   <input type="submit">
</form>


而option也可以給value attribute
有給則query parameter就會是value attribute
沒有給則預設是option裡的值

    <option value="1">one</option>
    <option>two</option>
    <option>three</option>

選one時query parameter會給1
其他會是two和three

HTML lable

要想在checkbox或在radio的選項
顯示選項的提示
可以使用<lable>
把<input>放在lable
在input前面顯示提示及可

<form>
   <lable>
      one
     <input type="checkbox" name="q">
   </lable>
   <lable>
      two
     <input type="checkbox" name="s">
   </lable>
   <lable>
      three
     <input type="checkbox" name="r">
   </lable>  
     <input type="submit">
</form>

one two three
<form>
   <lable>
      one
     <input type="radio" name="q" value="one">
   </lable>
   <lable>
      two
     <input type="radio" name="q" value="two">
   </lable>
   <lable>
      three
     <input type="radio" name="q" value="three">
   </lable>  
     <input type="submit">
</form>

one two three



HTML input radio

<form>
     <input type="radio" name="q" value="one">
     <input type="radio" name="q" value="two">
     <input type="radio" name="q" value="three">
      <input type="submit">
</form>


radio和checkbox一樣
沒有選取的話,query parameter就不會有
而想要三個checkbox只能選其中一個
選另一個,目前選就會取消
只要把name的名字取一樣就行了
而query parameter的值
如果後面沒有給value attribute,則值就是ON
有給就是value attribute的值

HTML input checkbox

<form >
     <input type="checkbox" name="q">    
     <input type="submit">
</form>


如果將q,勾選submit
那query parameter的q值會是ON
那沒有勾選話呢?
答案不是OFF,而是連q都不會在query parameter
所以在判斷是否checkbox有被選取
比較好的做法是判斷ON
不要用OFF來做
有選就是有ON,沒有選就是沒有ON
這樣比較不會出錯

HTML HTTP GET V.S POST

HTTP requset 有二種送參數的method
GET 和 POST

下表為二者的比較



GET POST
  • parameters放在url裡
  • 用來取得資料
  • 最大長度為URL的長度限度
  • 可以當做cache
  • 不可在server端改變
  • parameters放在request header之後
  • 用來更新資料
  • 無最大長度
  • 不可以當做cache
  • 可在server端改變


由表可知二者的不同
在應用上也就不同
必需依據其特性來使用
否則在應用上會造成錯誤或不便