Jump to content

Passing on a variable


IndianaGuy

Recommended Posts

I swear I have been trying to figure this out for two hours. Please help. The value of the input text is not getting passed to the process.php

 

 

 

 

<body>
<script>
function RecordTime(y) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
xmlhttp.open("GET", "process.php?p2=" + y, true);
xmlhttp.send();
    };
};
</script>
 
<form>
Play Start <input type="text" name="PosA" id="PosA" onchange="RecordTime(this.value)"><br>
</form>
Link to comment
Share on other sites

open() and send() have to be called outside the readystatechange event handler.

function RecordTime(y) {
  var xmlhttp = new XMLHttpRequest();

  xmlhttp.onreadystatechange = function() {
    // This event fires five times during a successful request
  }

  xmlhttp.open("GET", "process.php?p2=" + y, true);
  xmlhttp.send();
};

Keep in mind that the onchange event only fires when the text field loses focus.

Link to comment
Share on other sites

interesting. Excuse me for being so new. I dont really have any need for this code. I just need "y" to be sent to process.php. How can I change it to do that?

  xmlhttp.onreadystatechange = function() {
    // This event fires five times during a successful request
  }
Edited by IndianaGuy
Link to comment
Share on other sites

It's already doing that. You don't need the onreadystatechange event handler if you don't need information from the server. This will work:

function RecordTime(y) {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET", "process.php?p2=" + y, true);
  xmlhttp.send();
}

Normally, though, you will want to make sure that the server got the information, so you would have your PHP end print out a message to indicate that it got the value you're sending.

<?php
if(!empty($_GET['p2'])) {
  echo 1;
} else {
  echo 0;
}
?>

Then in your Javascript end you would check to see if the server returned "1"

function RecordTime(y) {
  var xmlhttp = new XMLHttpRequest();

  xmlhttp.onreadystatechange = function() {
    // Request is complete when readyState is 4
    if(xmlhttp.readyState == 4) {
      // Make sure no server errors occurred
      if(xmlhttp.status != 200) {
        alert("Request failed");
      }

      // Response is inside the responseText property
      if(xmlhttp.responseText != "1") {
        alert("Processing failed");
      }
    }
  };

  xmlhttp.open("GET", "process.php?p2=" + y, true);
  xmlhttp.send();
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...