This is my simple form which contains employe name, empno, empsal and doj(date of joining). Here i'm posting the values of this form through XMLHttpRequest object through send asynchronously and updating the requestTxt in the DOM in myDiv.
Here when i pass the parameters some thing like this, i can able to get the values in php echo $empname. "is my name";
var params = "empname="+myname+"&empno="+myno+"&empsal="+mysal+"&empdoj="+mydoj;
But at the same time, if i pass it as a object, stringified i don't know why it is not printing
var params = {
"empname":myname,
"empno":myno,
"empsal":mysal,
"empdoj":mydoj
};
var jsonText = JSON.stringify(params);
alert(jsonText);
Here is my code index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1252">
<title>Sample form</title>
<style>
table td{
border-collapse:collapse;
border:1ps solid #000;
}
.hide{
display:none;
}
</style>
<script type="text/javascript" src="util.js"></script>
</head>
<body>
<form name="sampleForm" id="sampleForm" action="javascript:return false;">
<table width="40%">
<tr>
<td>Employee Name:</td>
<td><input type="text" name="empname" id="empname"/></td>
</tr>
<tr>
<td>Employee No:</td>
<td><input type="text" name="empno" id="empno"/></td>
</tr>
<tr>
<td>Employee salary:</td>
<td><input type="text" name="empsal" id="empsal"/></td>
</tr>
<tr>
<td>Employee DOJ:</td>
<td><input type="text" name="empdoj" id="empdoj"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submitBtn" id="submitBtn1" /></td>
</tr>
</table>
<div class="hide" id="myDiv"></div>
</form>
<script type="text/javascript">
var subform = document.getElementById("sampleForm");
subform.onsubmit = function(){
var myReq;
var uRLTxt = "loginCheck.php";
var myname = document.getElementById("empname").value;
var myno = document.getElementById("empno").value;
var mysal = document.getElementById("empsal").value;
var mydoj = document.getElementById("empdoj").value;
/*var params = "empname="+myname+"&empno="+myno+"&empsal="+mysal+"&empdoj="+mydoj; */
var params = {
"empname":myname,
"empno":myno,
"empsal":mysal,
"empdoj":mydoj
};
var jsonText = JSON.stringify(params);
alert(jsonText);
myReq = new XMLHttpRequest();
myReq.open("POST",uRLTxt,true);
myReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//myReq.setRequestHeader("Content-type", "application/JSON; charset=utf-8");
myReq.setRequestHeader("Content-length", jsonText.length);
//alert(jsonText.length);
myReq.setRequestHeader("Connection", "close");
myReq.onreadystatechange = function() {
if(myReq.readyState == 4 && myReq.status == 200){
var return_data = myReq.responseText;
console.log(return_data);
document.getElementById("myDiv").style.display = "block";
document.getElementById("myDiv").innerHTML = return_data;
}
}
myReq.send(jsonText);
return false;
};
</script>
</body>
</html>
Php Code - loginCheck.php
<?php
$empname = isset($_REQUEST['empname']);
$empno = isset($_REQUEST['empno']);
$empsal = isset($_REQUEST['empsal']);
$empdoj = isset($_REQUEST['empdoj']);
echo $empname."is my name";
?>