2015年12月19日 星期六

jQuery AJAX

jQuery AJAX Methods

AJAX is the art of exchanging data with a server, and update parts of a web page - without reloading the whole page.
The following table lists all the jQuery AJAX methods:
MethodDescription
$.ajax()Performs an async AJAX request
$.ajaxPrefilter()Handle custom Ajax options or modify existing options before each request is sent and before they are processed by $.ajax()
$.ajaxSetup()Sets the default values for future AJAX requests
$.ajaxTransport()Creates an object that handles the actual transmission of Ajax data
$.get()Loads data from a server using an AJAX HTTP GET request
$.getJSON()Loads JSON-encoded data from a server using a HTTP GET request
$.getScript()Loads (and executes) a JavaScript from a server using an AJAX HTTP GET request
$.param()Creates a serialized representation of an array or object (can be used as URL query string for AJAX requests)
$.post()Loads data from a server using an AJAX HTTP POST request
ajaxComplete()Specifies a function to run when the AJAX request completes
ajaxError()Specifies a function to run when the AJAX request completes with an error
ajaxSend()Specifies a function to run before the AJAX request is sent
ajaxStart()Specifies a function to run when the first AJAX request begins
ajaxStop()Specifies a function to run when all AJAX requests have completed
ajaxSuccess()Specifies a function to run when an AJAX request completes successfully
load()Loads data from a server and puts the returned data into the selected element
serialize()Encodes a set of form elements as a string for submission
serializeArray()Encodes a set of form elements as an array of names and values

AJAX

AJAX 概念:

AJAX




Create an XMLHttpRequest Object



var xhttp;
if (window.XMLHttpRequest) {
    xhttp = new XMLHttpRequest();
    } else {
    // code for IE6, IE5    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}


Send a Request To a Server






Server Response






The onreadystatechange Event








<!DOCTYPE html>
<html>
<body>

<h3>Start typing a name in the input field below:</h3>

<form action=""> 
First name: <input type="text" id="txt1" onkeyup="showHint(this.value)">
</form>

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

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

</body>
</html>



showCustomer

function showCustomer(str) {
  var xhttp; 
  if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
  }
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
    document.getElementById("txtHint").innerHTML = xhttp.responseText;
    }
  };
  xhttp.open("GET", "getcustomer.asp?q="+str, true);
  xhttp.send();
}




2015年12月9日 星期三

DataSource->Where

如果 在 Design Type 下,該筆  Record 的 DataSource 裡面的 WHERE 有設定值
則 Run Time 時,似乎無法改變 DataSource->Where 的值

如果 在 Design Mode 裡, Record 的 DataSource 沒有設 Where
Run time ,在 BeforeBuildSelect Event 就可以設定 Where 值!

2015年12月7日 星期一

快速訂單設計記錄

這篇是寄給我自己看的,讀者看不懂就算了!

在資料庫中讀取相同的介紹會員,且還沒有完成的快速訂單中,找出最近的一筆,亦即是 id 最大的那筆,然後,設定一個 Session 給他。

在 STEP1 Page 中的 PageBeforeInitialize 中,寫:
$db = new clsDBmysql_conn(); 
$SQL = "SELECT max(qf_id) as maxid FROM quick_form WHERE refer_mem_extid='" .$_SESSION["mem_id"]."' AND finished='0';";
$db->query($SQL);
$Result = $db->next_record(); 
if ($Result) {$_SESSION["qf_id"] = $db->f("maxid");} else {$_SESSION["qf_id"] = 0;}

然後,在 Page 的 Record 中,DataSource 中,要設定該 Table ,Where 設定為:qf_id=$_SESSION["qf_id"];

這樣就可以搞定。

STEP 2, STEP 3....之後的步驟網頁,也都是一樣的設定,這樣就可以確保處裡的都是相同的一筆資料了!

上一步、下一步,都可以指稱到相同的一筆。縱使是 user 直接在 url 輸入 step2網址,也可以指到我們希望的那一筆資料。(相同會員最後未完成的那一筆記錄)。

注意:當最後一個步驟完成時,記得必需將 Finished 欄位設為"1",且將 Session 清空!表示已經完成,不會被重複處理!


DataSource->Where

DataSource->Where Run-Time Property (PHP)

Syntax

object->DataSource->Where [=value]

This property stores all conditions that are eventually used in the WHERE clause of a query that selects, updates or deletes data from the database. Programmers can add their own conditions (preserving the SQL syntax) in one of the events triggered before the final query is built and executed (i.e. BeforeBuildSelect, BeforeBuildDelete, BeforeBuildUpdate).

在CodeCharge 的 help 裡面有這一段,他說設定了DataSource->Where 是儲存了  Select, update, delete  的 WHERE 子句。
程式設計師可以分別在 BeforeBuildSelect, BeforeBuildDelete, BeforeBuildUpdate 裡面,去設定 WHERE 子句。

結果,我一開始以為,只要在 BeforeBuildSelect 中設定了 WHERE 子句,就已經設定了所有的 WHERE 子句,結果一執行的結果,Select 出來的是我要的結果沒錯,但是,一執行 Update 就整個資料庫都被 Update ,就相當於沒有 Where 一樣。(  我在 Record 的定義中,並沒有設定 WHERE)。這並不是我要的結果。

後來經過測試的結果是:原來,我必須個別去設定 BeforeBuildUpdate  的 Where,以及 BeforeBuildDelete 的 Where 才會被正確的執行。

 CodeCharge的SELECT, UPDATE, DELETE 的 WHERE 子句是個別設定的!

2015年11月25日 星期三

SCriptcase 在 buttom 之後的 error_message

Scriptcase的error handling 提供了內定的 macro 函數:

sc_error_message();
及
sc_error_exit();

然而,此 sc_Error_message() 函數似乎僅限於在 on_xxx events 中
放在 on_validate 等都work
但是,放到 自訂的 buttom 之後,似乎不work

於是,只好改用

echo "Error: Error message!!!";
sc_error_message("Error: Error Message!!!");
sc_error_exit();

這樣可以出現 echo 視窗。

但是如果只有 
echo "Error: Error message!!!";
sc_error_exit();
 這樣,出現echo 視窗後,還是會繼續執行,不會停止程式。

必需完整的

echo "Error: Error message!!!";
sc_error_message("Error: Error Message!!!");
sc_error_exit();

三段都有才可以。

記錄之,備查!


註:使用 echo "";  可以顯示一個 Output 小視窗。




2015年11月22日 星期日

將經營規則 business rule 寫在 mysql 中

資料庫系統,一定牽涉到資料庫本身,以及應用程式所處理的功能。
這些功能的規則,稱為 Business Rule. 有人翻譯為"商業規則",我認為這個翻譯不大妥當,應該為 "經營規則"。

這些經營規則,以前通常都是在 APP 應用程式中去設計執行,但如果有不同的程式給不同的人使用,而要遵守同一個 business rule ,那就變成必需在各個 AP 中個別去撰寫程式碼,這樣就會有不一致以及浪費時間的現象。

所以,關於這些資料的經營規則 business rule 最好是直接在  mysql 中執行即可,以方便保持資料的一致性,規則的一致性。

使用 mysql 的 STORED ROUTINE(Procedure/Function)及 Trigger Routine 以及Table 間的Foreign Key 規則、Index 唯一或可重複等工具,即可以達到一定的一致性規則。

而畫面的新增/更改/刪除/查詢,則可以很簡單的運用如 Scriptcase 等畫面產生器,就可以輕鬆的完成輸出入界面。


某些技巧:

如果有些程式碼是在特定的情況下才需要被執行,可以設計一個欄位值來控制他。
例如:field: execute_it char(1), Default:"0"
在 update_trigger中的程式可以寫:

IF NEW.execute_it = "1" THEN
    -- Execute some code...
    SET NEW.execute_it = "0";
ELSE
    -- Execute the other code......
END IF;

記得最後必須將 execute_it 的值設回來"0"
    SET NEW.execute_it = "0";



如何判斷現在FORM是在 insert mode? 還是 update mode?

只要用  if (empty({primary_key})) 就可以知道是否為新增模式了。 如果 {promary_key} 是空白的,那麼就是在新增模式;反之,就是更新模式。 以上。