php ajax XMLHttpRequest实例
本实例有两个文件,一个php处理文件,一个html请求文件。 代码如下: my_parse_file.php echo 'Thank you '. $_POST['firstname'] . ' ' . $_POST['lastname'] . ',says the PHP file'; ?> example.html function ajax_post(){ ? ? // Create our XMLHttpRequest object ? ? var hr = new XMLHttpRequest(); ? ? // 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 onreadystatechange event for the XMLHttpRequest object ? ? hr.onreadystatechange = 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..."; } Ajax Post to PHP and Get Return DataFirst Name: Last Name: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |