本实例有两个文件,一个php处理文件,一个html请求文件。 代码如下:
my_parse_file.php
<?php
echo \'Thank you \'. $_POST[\'firstname\'] . \' \' . $_POST[\'lastname\'] . \', says the PHP file\';
?>
example.html
<html>
<head>
< >
function ajax_post(){
// Create our HttpRequest
var hr = new HttpRequest();
// Create some variables we need to send to our PHP file
var url = \"my_parse_file.php\";
var fn = document.getElementById(\"first_name\").value;
var ln = document.getElementById(\"last_name\").value;
var vars = \"firstname=\"+fn+\"&lastname=\"+ln;
hr.open(\"POST\", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader(\"Content-type\", \"application/x-www-form-urlencoded\");
// Access the event for the HttpRequest
hr. = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById(\"status\").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
document.getElementById(\"status\").innerHTML = \"processing...\";
}
</ >
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
First Name: <input id=\"first_name\" name=\"first_name\" type=\"text\"> <br><br>
Last Name: <input id=\"last_name\" name=\"last_name\" type=\"text\"> <br><br>
<input name=\"myBtn\" type=\"submit\" value=\"Submit Data\" =\"ajax_post();\"> <br><br>
<div id=\"status\"></div>
</body>
</html> 继续阅读与本文标签相同的文章
上一篇 :
php ajax实现的文件上传进度条
下一篇 :
php preg_match正则表达式函数实例
-
带你解读美国2019《国家人工智能战略》
2026-05-15栏目: 教程
-
职场必备的五大Excel打印技巧,这样打印省纸又省时
2026-05-14栏目: 教程
-
初学者必须知道的Linux的15个基础总结!
2026-05-14栏目: 教程
-
公益大数据开发和利用:我们走到哪里了?
2026-05-14栏目: 教程
-
就差一张脸了!虚拟机器人向全世界征集肖像,一经征用直接获得近百万报酬
2026-05-14栏目: 教程
