js ajax提交留言評論工作筆記

來源:互聯網
上載者:User

直接上例子吧,費話不說多


html

 代碼如下 複製代碼

  <form id="form1" name="form1" method="post" action="">
                        <h3>MESSAGE 留言板</h3>
                        <dl>
                            <dt>Your name 您的姓名</dt>
                            <dd><input type="text" name="name" id="name" class="s"></dd>
                            <dt>Email 電子郵件</dt>
                            <dd><input type="text" name="email" id="email" class="s"></dd>
                            <dt>Message 填寫您的留言</dt>
                            <dd><textarea class="textbox" name="content" id="content"></textarea></dd>
                        </dl>
                        <div class="button"><a href="javascript:;" onClick="checkgoobkform();">Send 送出</a></div>
                     </form>

javascript ajax部份

 代碼如下 複製代碼


var xmlHttp

function checkgoobkform()
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
  alert ("Browser does not support HTTP Request")
  return false;
} else{
 var name = document.getElementById("name").value;
 if(name=='') {
    alert("請輸入您的姓名! ");
    document.getElementById("name").focus();
    return false;
  }
 
 var email = document.getElementById("email").value;
 if(email=='')
 {
  alert("請輸入電子郵件!");
  document.getElementById("email").focus();
  return false; 
 }else if((email.indexOf("@")==-1)||(email.indexOf(".")==-1))
 {
  alert("電子郵件格式不正確!");
  document.getElementById("email").focus();
  return false; 
 }
 
 var content = document.getElementById("content").value;
 if(content=='') {
   document.getElementById("content").focus();
    alert("請填寫您的留言! ");
    return false;
  }
}

var url="/functionsubmit.php"
url=url+"?name="+name
url=url+"&email="+email
url=url+"&content="+content
url=url+"&action=addgbook"
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
  var restext=xmlHttp.responseText;
  if(restext=="ok"){
   alert("提交成功,感謝您的留言!");
   document.getElementById("name").value="";
   document.getElementById("email").value="";
   document.getElementById("content").value="";
  }
  else
  {
   alert('提交失敗!'+restext);
  }
 }
}

php儲存資料部份

 代碼如下 複製代碼

<?php
$sname =isset($_GET['name'])?addslashes(iconv('gbk','utf-8',$_GET['name'])):0;
$email =isset($_GET['email'])?addslashes($_GET['email']):0;
$content =isset($_GET['content'])?addslashes(iconv('gbk','utf-8',$_GET['content'])):0;

include("data/common.inc.php");
mysql_connect('localhost',$cfg_dbuser,$cfg_dbpwd) or die('database connect failer');
mysql_select_db($cfg_dbname);
mysql_query("set Names '$cfg_db_language'");
$sql ="insert into ".$cfg_dbprefix.'guestbook'."(title,email,msg)values('$sname','$email','$content')";
$query = mysql_query( $sql ) or die($sql);
if( $query )
{
 echo 'ok';
 
}
else
{
 echo '參數錯誤或記錄己被刪除!';
}

?>

sql是dedecms的一個留言板外掛程式

 代碼如下 複製代碼


--
-- 表的結構 `xjz_guestbook`
--

CREATE TABLE IF NOT EXISTS `xjz_guestbook` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(60) NOT NULL DEFAULT '',
  `tid` mediumint(8) NOT NULL DEFAULT '0',
  `mid` mediumint(8) unsigned DEFAULT '0',
  `posttime` int(10) unsigned NOT NULL DEFAULT '0',
  `uname` varchar(30) NOT NULL DEFAULT '',
  `email` varchar(50) NOT NULL DEFAULT '',
  `homepage` varchar(50) NOT NULL DEFAULT '',
  `qq` varchar(15) NOT NULL DEFAULT '',
  `face` varchar(10) NOT NULL DEFAULT '',
  `ip` varchar(20) NOT NULL DEFAULT '',
  `dtime` int(10) unsigned NOT NULL DEFAULT '0',
  `ischeck` smallint(6) NOT NULL DEFAULT '1',
  `msg` text,
  PRIMARY KEY (`id`),
  KEY `ischeck` (`ischeck`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

相關文章

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。