ecshop中的ajax非常好用,當自己第一次使用的時候,也非常吃力。當自己用多了的時候,卻感覺十分簡單,也特別方便。
首先,建立dwt.裡面寫個form進去,加個form提交時間的按扭<img src="images/tijiaoanniu_07.gif" width="69" height="20" border="0" style="cursor:pointer" onclick="return sub_zixun();" />
其次,在js中增加函數
function sub_zixun(){ var frm = document.forms['zixun_form']; var msg_title = frm.elements['msg_title'].value; var msg_phone = frm.elements['msg_phone'].value; var user_email = frm.elements['user_email'].value; var msg_content = frm.elements['msg_content'].value; var msg = ''; if (msg_title.length == 0) {msg += '標題不可為空' + '\n'; } if (msg_phone.length == 0) {msg += '電話號碼不可為空' + '\n'; } if (user_email.length == 0) {msg += 'Email不可為空' + '\n'; } if (msg_content.length == 0) {msg += '評論不可為空' + '\n'; } if (msg.length > 0) {alert(msg);return false; } else {Ajax.call( 'zixun.php?act=act_sub', 'msg_phone=' + msg_phone+'&user_email='+user_email+'&msg_content='+msg_content+'&msg_title='+msg_title, act_callback , 'POST', 'TEXT', true, true ); }}
3:增加回呼函數act_callback ()
function act_callback(result){if(result == 'true'){alert("提交評論成功");}else{alert("提交評論失敗");}}
處理ajax回調值的結果
elseif ($act == 'act_sub'){include_once(ROOT_PATH . 'includes/lib_clips.php');$message = array( 'user_id' => $_SESSION['user_id'], 'user_name' => $_SESSION['user_name'], 'user_email' => isset($_POST['user_email']) ? htmlspecialchars(trim($_POST['user_email'])) : '', 'msg_type' => isset($_POST['msg_type']) ? intval($_POST['msg_type']) : 0, 'msg_title' => isset($_POST['msg_title']) ? trim($_POST['msg_title']) : '', 'msg_content' => isset($_POST['msg_content']) ? trim($_POST['msg_content']) : '', 'order_id' => 0, 'msg_area' => 1, 'msg_phone' => trim($_POST['msg_phone']), 'upload' => array() );//留言板提交的資料 if (add_message($message)){ echo 'true'; }else{ echo 'false'; }}
這樣就完成了一次ecshop中ajax的所有調用步驟。