PHP with Post Method-1
Send request to the server by post method and reterive response

In this section we are sending request to the server by post method then getting data. in this section we create three page

1. HTML
2. Javascript
3. PHP

About Set Request Method

Set Request method is use to send value to the server by POST method of HTML form tag.

Code

XMLHttpRequestObject.open('POST', 'value.php', true);
XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
XMLHttpRequestObject.send("mytext=" + document.getElementById("mytext").value);

when send value to the server by html post method then this is necessary to use
Content-type- application/x-www-form-urlencoded


HTML page

<html>
<head>
<title>How to Submit Value through Ajax</title>
<script language="javascript" src="ajax.js"></script>
</head>
<body>
<form action="javascript:sendvalue(document.getElementById('frm'));" name="frm" id="frm">
Enter Text<input id="fname" type="text"/><br>
<br>
<input type="submit" name="button" value="Show Text">
</form>
<div name="txt" id="txt"></div>
</body>
</html>

Javascript page save as (ajax.js)

function sendvalue(obj)
{

var XMLHttpRequestObject=false;
if(window.XMLHttpRequest)
{
XMLHttpRequestObject=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
XMLHttpRequestObject=new ActiveXObject("Microsoft.XMLHTTP");
}

XMLHttpRequestObject.onreadystatechange=function()
{
if (XMLHttpRequestObject.readyState==4 && XMLHttpRequestObject.status==200)
{
result = XMLHttpRequestObject.responseText;
document.getElementById('txt').innerHTML = result;
}
}
XMLHttpRequestObject.open('POST', 'value.php', true);
XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
XMLHttpRequestObject.send("fname=" + document.getElementById("fname").value);

}

PHP page save as (value.php)

<?php
echo $_POST['fname'];
?>

Note: This page work behind the scenes.


Previous
Next