2016年2月12日 星期五

ajax 的基本例子

index.php  內容

大概流程就是   

First name: <input type="text" onkeyup="showHint(this.value)">
                           |
                           v
              function showHint(str) 
                           |
                           v
        xmlhttp.open("GET", "gethint.php?q=" + str, true);
                           |
                           v
   document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                           |
                           v
   <p>Suggestions: <span id="txtHint"></span></p>     


文字筆記如下:

<p>Suggestions: <span id="txtHint"></span></p>  -> 


他內嵌了一段 javascript....   以 <script> 開頭...
                                </script> 結尾....
裡面的實作是一個 function  showHint(str)

先判斷  str.length 的數值...如果為零...就直接返回空字串....

如果不為零.... 首先取得  xmlhttp = XMLHttpRequest()...

然後check xmlhttp.onreaystatechange ....

如果 xmlhttp.readyState == 4 而且  xmlhttp.status == 200
     
則 document.getElementById("txtHint").innerHTML = xmlhttp.responseText

"txtHint" 為 html 主體裡面的   <p>Suggestions: <span id="txtHint"></span></p>

接下來執行

xmlhttp.open("GET", "gethint.php?q=" + str, true);
xmlhttp.send();

他會去執行  gethint.php 裡面的程式碼...

$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
........
// get the q parameter from URL
$q = $_REQUEST["q"];

// lookup all hints from array if $q is different from "" 
if ($q !== "") {
    $q = strtolower($q);
    $len=strlen($q);
    foreach($a as $name) {
        if (stristr($q, substr($name, 0, $len))) {
            if ($hint === "") {
                $hint = $name;
            } else {
                $hint .= ", $name";
            }
        }
    }
}

=======================================================
<html>
<head>
<script>
function showHint(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET", "gethint.php?q=" + str, true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<p><b>Start typing a name in the input field below:</b></p>
<form> 
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
=================================================================

gethink,php 的內容

=================================================================
<?php
// Array with names
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$a[] = "Diana";
$a[] = "Eva";
$a[] = "Fiona";
$a[] = "Gunda";
$a[] = "Hege";
$a[] = "Inga";
$a[] = "Johanna";
$a[] = "Kitty";
$a[] = "Linda";
$a[] = "Nina";
$a[] = "Ophelia";
$a[] = "Petunia";
$a[] = "Amanda";
$a[] = "Raquel";
$a[] = "Cindy";
$a[] = "Doris";
$a[] = "Eve";
$a[] = "Evita";
$a[] = "Sunniva";
$a[] = "Tove";
$a[] = "Unni";
$a[] = "Violet";
$a[] = "Liza";
$a[] = "Elizabeth";
$a[] = "Ellen";
$a[] = "Wenche";
$a[] = "Vicky";

// get the q parameter from URL
$q = $_REQUEST["q"];

$hint = "";

// lookup all hints from array if $q is different from "" 
if ($q !== "") {
    $q = strtolower($q);
    $len=strlen($q);
    foreach($a as $name) {
        if (stristr($q, substr($name, 0, $len))) {
            if ($hint === "") {
                $hint = $name;
            } else {
                $hint .= ", $name";
            }
        }
    }
}

// Output "no suggestion" if no hint was found or output correct values 
echo $hint === "" ? "no suggestion" : $hint;
?>

=====================================================================


Reference : http://www.w3schools.com/php/php_ajax_php.asp


2016年2月11日 星期四

2016年2月9日 星期二

wordpress 的一些文章




wordpress 的布景主題

web building tutorial

What We Will Do

We will build a web site
  • We will create web pages using HTML
  • We will add a consistent style using CSS
  • We will add computer code using JavaScript
  • We will add static data pages
  • We will fetch data using Http
  • We will fetch data using AppML
  • We will fetch data using SQL
  網站內容大概長得像這樣



Create a CSS Style Sheet

body {
    font-family: "Trebuchet MS", Verdana, sans-serif;
    font-size: 16px;
    background-color: dimgrey;
    color: #696969;
    padding: 3px;
}

#main {
    padding: 5px;
    padding-left:  15px;
    padding-right: 15px;
    background-color: #ffffff;
    border-radius: 0 0 5px 5px;
}

h1 {
    font-family: Georgia, serif;
    border-bottom: 3px solid #cc9900;
    color: #996600;
    font-size: 30px;
}
The CSS file above defines the styles to be used for:
  • The HTML body element <body>
  • The HTML element with id="main"
  • The HTML heading element <h1>
edit the file index.html

<head>
  <title>Our Company</title>
  <link href="site.css" rel="stylesheet">
</head>

<body>

  <div id="main">
  <h1>Welcome to Our Company</h1> 
   .....
  <p>Live Data (Files and Databases)</p>
  </div>

</body>
</html>

Edit the About Page

To complete your work, do the same changes in about.html.
1. Add a link to the style sheet.
2. Add a <div id="main"> element.

about about.html

<!DOCTYPE html>
<html>
<head>
  <title>About</title>
  <link href="site.css" rel="stylesheet">
</head>

<body>

  <div id="main">
  <h1>About Us</h1>
  <p>Lorem Ipsum Porem Lorem Ipsum Porem</p>
  </div>

</body>
</html>

Adding Code (JavaScript)


What We Will Do

In this chapter we will:
  • Create some computer code (JavaScript)
  • Add a link to the script in your pages


Create JavaScript

create a new file named script.js

edit script.js
==============================================================
document.getElementById("foot01").innerHTML =
"<p>&copy;  " + new Date().getFullYear() + " W3Schools. All rights reserved.</p>";
==========================================

Edit the Home Page


edit the file index.html

<div id="main">
 ........
  <p>Live Data (Files and Databases)</p>
  <footer id="foot01"></footer>
</div>

<script src="script.js"></script>

</body>

Web Building - Adding a Data Page


What We Will Do

In this chapter we will:
  • Add a data page to the web site
edit site.css

Create a Data Page

create a new file named customers.html

add the following content to customers.html

customers.html

<!DOCTYPE html>
<html>

<head>
  <title>Customers</title>
  <meta charset="UTF-8">
  <link href="site.css" rel="stylesheet">
</head>

<body>

<div id="main">
<h1>Customers</h1>

<table>
<tr>
  <th>Name</th>
  <th>City</th>
  <th>Country</th>
</tr>
<tr>
  <td>Alfreds Futterkiste</td>
  <td>Berlin</td>
  <td>Germany</td>
</tr>
<tr>
  <td>Berglunds snabbköp</td>
  <td>Luleå</td>
  <td>Sweden</td>
</tr>
<tr>
  <td>Centro comercial Moctezuma</td>
  <td>México D.F.</td>
  <td>Mexico</td>
</tr>
<tr>
  <td>Ernst Handel</td>
  <td>Graz</td>
  <td>Austria</td>
</tr>
<tr>
  <td>FISSA Fabrica Inter. Salchichas S.A.</td>
  <td>Madrid</td>
  <td>Spain</td>
</tr>
<tr>
  <td>Galería del gastrónomo</td>
  <td>Barcelona</td>
  <td>Spain</td>
</tr>
<tr>
  <td>Island Trading</td>
  <td>Cowes</td>
  <td>UK</td>
</tr>
<tr>
  <td>Königlich Essen</td>
  <td>Brandenburg</td>
  <td>Germany</td>
</tr>
<tr>
  <td>Laughing Bacchus Wine Cellars</td>
  <td>Vancouver</td>
  <td>Canada</td>
</tr>
<tr>
  <td>Magazzini Alimentari Riuniti</td>
  <td>Bergamo</td>
  <td>Italy</td>
</tr>
<tr>
  <td>North/South</td>
  <td>London</td>
  <td>UK</td>
</tr>
<tr>
  <td>Paris spécialités</td>
  <td>Paris</td>
  <td>France</td>
</tr>
<tr>
  <td>Rattlesnake Canyon Grocery</td>
  <td>Albuquerque</td>
  <td>USA</td>
</tr>
<tr>
  <td>Simons bistro</td>
  <td>København</td>
  <td>Denmark</td>
</tr>
<tr>
  <td>The Big Cheese</td>
  <td>Portland</td>
  <td>USA</td>
  </tr>
<tr>
  <td>Vaffeljernet</td>
  <td>Århus</td>
  <td>Denmark</td>
</tr>
<tr>
  <td>Wolski Zajazd</td>
  <td>Warszawa</td>
  <td>Poland</td>
</tr>
</table>

<footer id="foot01"></footer>
</div>

<script src="script.js"></script>

</body>
</html>

Web Building - Adding Navigation


What We Will Do

In this chapter we will:
  • Add a navigation menu
edit  script.js
   add the following content into script.js
===================================
   document.getElementById("nav01").innerHTML =
"<ul id='menu'>" +
"<li><a href='index.html'>Home</a></li>" +
"<li><a href='customers.html'>Data</a></li>" +
"<li><a href='about.html'>About</a></li>" +
"</ul>";
===============================================

edit your style sheet site.css
Add the following to the file content:

==================================
ul#menu {
    padding: 0;
    margin-bottom: 11px;
}

ul#menu li {
    display: inline;
    margin-right: 3px;
}

ul#menu li a {
    background-color: #ffffff;
    padding: 10px 20px;
    text-decoration: none;
    color: #696969;
    border-radius: 4px 4px 0 0;
}

ul#menu li a:hover {
    color: white;
    background-color: black;
}
=================================

edit your index.html

add <nav id="nav01"></nav> into index.html
================================
<body>

<nav id="nav01"></nav>

<div id="main">
=================================


Edit All Pages

  edit customers.html and about.html

  add  <nav id="nav01"></nav>

Reference : http://www.w3schools.com/website/