Reputation: 1676
I would like to display the variable num
in the php page.
The error i am getting is as follows:
Notice: Undefined index: num in C:\xampp\htdocs\javas.php on line 4
PHP:
<?php
$lol = $_POST['num'];
echo "$lol " ;
?>
HTML/Javascript:
<html>
<head>
<script language="JavaScript" type="text/javascript">
function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "javas.php";
var num = "lol";
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(num); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
<input name="myBtn" type="submit" value="increment" onClick="javascript:ajax_post();">
<br /><br />
<div id="status"></div>
</body>
</html>
Upvotes: 0
Views: 2170
Reputation: 19635
One part of the issue here is that the parameter that you're posting is not properly serialized. The parameter string for a POST is similar to the query string for a GET request:
var1=foo&var2=bar
As it stands right now you're just sending a raw piece of data over (the value assigned to the variable num
.
Upvotes: 0