i want send a javascript variable to the php page and i write below code but there is a problem.
i want send x variable in below code with send function.please help me.
- Code: Select all
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","ajax-post-form.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var x=25;
xmlhttp.send(x);
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
</body>
</html>
and php code:
- Code: Select all
<?php
$site = $_POST['x'];
echo "value: $site <br />";
?>

