JS Ajax Submit Message Comment work notes

Source: Internet
Author: User

Let's go straight to the example, crap don't say much.


Html

The code is as follows Copy Code

<form id= "Form1" Name= "Form1" method= "Post" action= "" >
<dl>
<dt>your Name Your name </dt>
<dd><input type= "text" name= "name" id= "name" class= "s" ></dd>
<dt>email e-Mail </dt>
<dd><input type= "text" name= "email" id= "email" class= "s" ></dd>
<dt>message fill in your message </dt>
<dd><textarea class= "textbox" name= "Content" id= "content" ></textarea></dd>
</dl>
<div class= "button" ><a href= "javascript:;" onclick= "checkgoobkform ();" >send send out </a></div>
</form>

JavaScript Ajax part

The code is as follows Copy Code


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 ("Please enter your name!");
document.getElementById ("name"). focus ();
return false;
}

var email = document.getElementById ("email"). Value;
if (email== ')
{
Alert ("Please enter email!");
document.getElementById ("email"). focus ();
return false;
}else if ((Email.indexof ("@") ==-1) | | (Email.indexof (".") ==-1))
{
Alert ("Incorrect email format!");
document.getElementById ("email"). focus ();
return false;
}

var content = document.getElementById ("content"). Value;
if (content== ') {
document.getElementById ("Content"). focus ();
Alert ("Please fill in your message!");
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 ("submitted successfully, thank you for your message!") ");
document.getElementById ("name"). value= "";
document.getElementById ("email"). value= "";
document.getElementById ("Content"). value= "";
}
Else
{
Alert (' Commit failed! ') +restext);
}
}
}

PHP Save Data part

The code is as follows Copy Code

<?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 ' parameter error or record has been deleted! ';
}

?>

SQL is a message board plug-in for DEDECMS

The code is as follows Copy Code


--
--The structure of the table ' Xjz_guestbook '
--

CREATE TABLE IF not EXISTS ' Xjz_guestbook ' (
  ' ID ' mediumint (8) unsigned not NULL auto_increment,
  ' title ' varchar NOT NULL default ',
  ' tid ' mediumint (8) NOT null default ' 0 ',
  ' mid ' mediumint (8) Unsigned default ' 0 ',
  ' posttime ' int (a) unsigned not NULL DEFAULT ' 0 ',
  ' uname ' varchar (a) Not NUL L default ',
  ' email ' varchar NOT null default ',
  ' homepage ' varchar ' (+) NOT null default ',   ' QQ ' varchar () NOT NULL default ',
  ' face ' varchar (%) NOT null default ',
  ' IP ' varchar Not null default ',
  ' dtime ' int (a) unsigned NOT null default ' 0 ',
  ' Ischeck ' smallint (6) Not N ull DEFAULT ' 1 ',
  ' msg ' text,
  PRIMARY key (' id '),
  key ' Ischeck ' (' Ischeck ')
) engine=m yisam  DEFAULT Charset=utf8 auto_increment=5;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.