php 异步提交表单

XML/HTML code
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<form name="touzi" method="post">
<table>
    <tr>
        <td>客户类型</td>
        <td><input type="radio" name="customer-type" value="个人客户" id="0"/><input type="radio" name="customer-type" value="机构客户" id="1"/></td>
    </tr>
    <tr>
        <td>联系人</td>
        <td><input type="text" id="username"/></td>
    </tr>
    <tr>
        <td>联系电话</td>
        <td><input type="text" id="mobile"/></td>
    </tr>
    <tr>
        <td>E-mail</td>
        <td><input type="text" id="email"/></td>
    </tr>
    <tr>
        <td>联系地址</td>
        <td><input type="text" id="address"/></td>
    </tr>
    <tr>
        <td>邮编</td>
        <td><input type="text" id="postcode"/></td>
    </tr>
    <tr>
        <td>内容</td>
        <td><textarea id="customer-content"></textarea></td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" value="tijiao" onclick="submitForm()"/></td>
    </tr>
</table>
</form>



JavaScript code
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function submitForm(){
    var xmlHttp = createXmlHttp();  
    if(!xmlHttp) {  
        alert("您的浏览器不支持AJAX!");  
        return 0;  
    }  
       
    var url = 'phpmail.php';  
    var postData = "";  
    postData = "username=" + document.getElementById('username').value;  
    postData += "t=" + Math.random();  
 
    xmlHttp.open("POST", url, true);  
    xmlHttp.setRequestHeader("Content-Type""application/x-www-form-urlencoded");  
    xmlHttp.onreadystatechange = function() { 
        alert(xmlHttp.status);
        if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {  
     
            if(xmlHttp.responseText == '1') {  
                alert('post successed');  
            }  
        }  
    }  
    xmlHttp.send(postData);
}
 
function createXmlHttp() {  
    var xmlHttp = null;  
        
    try {  
        //Firefox, Opera 8.0+, Safari  
        xmlHttp = new XMLHttpRequest();  
    catch (e) {  
        //IE  
        try {  
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");  
        catch (e) {  
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
        }  
    }  
        
    return xmlHttp;  
}  
    



php

PHP code
 
?
1
2
3
4
5
<?php  
    if(isset($_POST['username']) {  
        echo '1';  
    }  
?>
posted @ 2016-02-26 18:09  cymbidium920426  阅读(245)  评论(0编辑  收藏  举报