Jump to content

Recommended Posts

Hi, i am new to this, trying to test it out, but not sure why it doesnt work:

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!
ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

ajaxRequest.open("POST", "serverTime.php", true);
if(ajaxRequest.readyState == 4){
	ajaxRequest.send("test=hi");
}

}
//-->
</script>



<form name='myForm' method='POST'>
Name: <input type='text' onChange="ajaxFunction();" name='username' /> <br />
Time: <input type='text' name='time' />
</form>

and the serverTime.php is

session_start();
if (isset($_POST['test'])) {$_SESSION['test'] = $_POST['test'];}
echo $_SESSION['test'];
echo "<br />";

Thank you very much :P

Link to comment
https://forums.phpfreaks.com/topic/161973-sending-variables-by-post/
Share on other sites

Try this instead, remember to set your form id

 

   var object_busy= false;
   ShowLoading = 1 ;
   var MyTimer = null;
   
   Myvar = "<table  align='center'><tr><td><img src='/templates/images/loading.gif' ></td></tr></table>";
   
   function RequireAjaxData_post($Request, $Control) {
      if ($Control == "" || $Control == null)   {alert ("No output specified !"); return;}
      var ai = new AJAXInteraction($Request, GetServerData,  $Control );
     ai.doPost("sendForm");
   }
   
   function GetServerData  ($TheData, $Control){
      document.getElementById($Control).innerHTML = $TheData;
   }

   function AJAXInteraction(url, callback, $Control) {
         var req = init();
       req.onreadystatechange = processRequest;
       
       function init() {
         if   (window.XMLHttpRequest)      {   return new XMLHttpRequest();               }
         else if (window.ActiveXObject)   {   return new ActiveXObject("Microsoft.XMLHTTP");  }
         else {alert ("Your browser seems to be out of date, Please update!"); return;      }
       }
   
       function processRequest () {
         if (req.readyState == 4) {
              if (req.status == 200) callback(req.responseText, $Control);
         }
         else   callback(Myvar , $Control);
       }
       
   this.processForm = function (formID) {
     var tmp = new Array();
	 var n, form;

	 form = document.getElementById(formID);

	 for (n=0;n<form.length;n++)
	      tmp.push(encodeURI(form.elements[n].name) + "=" + encodeURI(form.elements[n].value));
         
	 return tmp.join("&");
   }
   
       this.doGet = function(formID) {
         var vars = this.processForm(formID);
         req.open("GET", url+"?"+vars, true);
         req.send(null);
       }
   
       this.doPost = function(formID) {
         var body = this.processForm(formID);
         req.open("POST", url, true);
         req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         req.send(body);
	 }
   }

Your main problem is that you have not created the XMLHttpRequest object. Add something like this to your code:

 

if (window.XMLHttpRequest) { var ajaxRequest = new XMLHttpRequest(); }
else if (window.ActiveXObject){ var ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); }

 

 

Also, both open() and send() must be called before the request is sent to the server. readyState will only be equal to 4 after the request has been successfully sent to the server (i.e. after calling both open() and send()) and a response retrieved.

 

Replace:

ajaxRequest.open("POST", "serverTime.php", true);
   if(ajaxRequest.readyState == 4){
      ajaxRequest.send("test=hi");
   }

 

with:

ajaxRequest.open("POST", "serverTime.php", true); 
ajaxRequest.send("test=hi");
   if(ajaxRequest.readyState == 4){
      alert(ajaxRequest.responseText); //Do something with the response
   }

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.