ted_chou12 Posted June 12, 2009 Share Posted June 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/161973-sending-variables-by-post/ Share on other sites More sharing options...
dreamwest Posted June 13, 2009 Share Posted June 13, 2009 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/161973-sending-variables-by-post/#findComment-854901 Share on other sites More sharing options...
jacksonmj Posted June 13, 2009 Share Posted June 13, 2009 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 } Quote Link to comment https://forums.phpfreaks.com/topic/161973-sending-variables-by-post/#findComment-855219 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.