0

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>
3
  • first of all show the dump what you are getting using print_r($_post); in your php code. Secondly correct me if i am wrong, i dont see that you are posting anything on this page. As i dont see any form Commented Dec 12, 2011 at 19:08
  • you only need form elements for ajax? Commented Dec 12, 2011 at 19:12
  • is your PHP file javas.php or my_parse_file.php? Commented Dec 12, 2011 at 19:13

2 Answers 2

3

You have to send name/value pairs

hr.send("num=" + num);

Sign up to request clarification or add additional context in comments.

6 Comments

Was in the middle of posting the same thing...gotta have name/value pairs to pass.
how would i increment the value of num (first make it = 1) when the button is clicked?
Depends. If you need to store num somewhere (so the user can close his browser and come back later and the value of num is the same as it was before) then you would read the php value in the onreadystatechange func and set the javascript variable num = to that. If you just wanted num to be incremented for the duration of the session then you could change the send line to: hr.send("num=" + (++num));
this only increments once, i ould like to increment everytime the button is clicked
how do you set the value of num to the incremented value once it has sent
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.