-2

I am trying to build a simple application in which the client writes few JAVA script variables to a file in the server, over the course of interaction with the user. I have the following method to send a variable to PHP. I am not able to know: (a) how to read this variable in examp.php? (b) how to pass multiple variables? I am a beginner in programming with these languages.

<script type="text/javascript">
if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
}

else{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

var PageToSendTo = "examp.php?";
var MyVariable = "Hello, how are you?";
var VariablePlaceholder = "data=";
var UrlToSend = PageToSendTo + VariablePlaceholder + MyVariable;

//XMLHttpRequestObject.send("data=" + sTime);
xmlhttp.open("POST", UrlToSend, false);
xmlhttp.send();

</script>
Neeks
  • 101
  • 1
  • 6
    taken any ajax/PHP tutorials? Any of the available ones should give nice clear examples of this sort of thing. – ADyson Sep 27 '17 at 14:58

1 Answers1

0

Here is an example:

function ajaxReq(php_file, data) {
  var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); // XMLHttpRequest object

  request.open("POST", php_file, true); // set the request

  // adds  a header to tell the PHP script to recognize the data as is sent via POST
  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  request.send(data); // calls the send() method with data as parameter

  // Check request status
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      alert(request.responseText);
    }
  }
}

var data_send ='n1=v1&n2=v2&n3=v3';
ajaxReq('file.php', data_send);
CoursesWeb
  • 4,179
  • 3
  • 21
  • 27