Post using XMLHTTP to add data without refreshing

Source: Internet
Author: User

Traditional data submission methods are implemented using <form>.
<Form> the method attribute in the tag determines that when the data of the form element is sent to the server,
How to package HTTP request information.
Methods available for the method attribute
Request set for reading data by using the method attribute to send form elements
Get ID at the end of the URL querystring
Post form in the body of the HTTP request (free area of the HTTP request)

This articleArticleUse XMLHTTP to implement form method = post.

Clientpost.htm

<Script language = "JavaScript">
Function adddatapost (suserid, susername)
{
/*
* ------------- Adddatapost (suserid, susername )-----------------
* Adddatapost (suserid, susername)
* Function: add data through XMLHTTP. equivalent to form method = post.
* Parameter: suserid, String, sending condition.
* Parameter: susername, String, sending condition.
* Instance: adddatapost (document. All. userid. Value, document. All. username. value );
* ------------- Adddatapost (suserid, susername )-----------------
*/
VaR obao = new activexobject ("Microsoft. XMLHTTP ");
// Special characters: +, %, &, = ,? And other transmission solutions.
// Update:
// Escape (suserid), escape (susername );
Suserid = escape (suserid );
Susername = escape (susername );
VaR userinfo = "userid =" + suserid + "& username =" + susername;
Obao. Open ("Post", "server. asp", false );
Obao. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ")
Obao. Send (userinfo );
// Clear the data in the input box.
Document. All. userid. value = "";
Document. All. username. value = "";
// The server processes the returned escape encoded string.
Alert (Unescape (obao. responsetext ))
}
</SCRIPT>
<Input type = "button" onclick = "adddatapost (document. All. userid. Value, document. All. username. Value)" value = "adddatapost"> <br>
Userid: <input type = "text" name = "userid"> <br>
Username: <input type = "text" name = "username">

Server. ASP server processing.

<% @ Language = "JavaScript" %>
<%
Function opendb (sdbname)
{
/*
* --------------- Opendb (sdbname )-----------------
* Opendb (sdbname)
* Function: Open the database sdbname and return the conn object.
* Parameter: sdbname, String, database name.
* Instance: var conn = opendb ("database. mdb ");
* --------------- Opendb (sdbname )-----------------
*/
VaR connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath (sdbname );
VaR conn = server. Createobject ("ADODB. Connection ");
Conn. Open (connstr );
Return conn;
}
VaR sresult = "";
VaR oconn = opendb ("data. mdb ");
// Equivalent to form method = post.
// Equivalent to form method = post.
// Special characters: +, %, &, = ,? And other transmission solutions. The client is transmitted through escape encoding.
// Therefore, Unescape decoding should be performed on the receiver first.
// Update:
// Unescape (request. Form ("userid ")).....
VaR userid = Unescape (request. Form ("userid "));
VaR username = Unescape (request. Form ("username "));
Sresult = "userid:" + userid + "\ nusername:" + username + "\ n added successfully ."
VaR SQL = "insert into users (userid, username) values ('" + userid + "', '" + username + "')";
Oconn. Execute (SQL );
Response. Write (escape (sresult ));
%>

Database Design
Data. MDB
Table users.
Field
Id automatic number
Userid text
Username text

Table: Users data:
Id userid Username
1 wanghr100 Administrator

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.