Petran76 Posted December 20, 2007 Share Posted December 20, 2007 Hello everyone and good u.s. east cost morning. I am starting with ajax and I wrote the following javascript code. It seems to work but in my php script the $_POST array it is empty.Any idea. Appreciate. function ajaxRegister() { var http = false; if(navigator.appName == "Microsoft Internet Explorer") { http = new ActiveXObject("Microsoft.XMLHTTP"); } else { http = new XMLHttpRequest(); } http.open("POST", "ajax.php", true); http.onreadystatechange=function() { if(http.readyState == 4) { document.getElementById("email").value = http.responseText; } } var data ="petros=1"; http.send(data); } <?php foreach ($_POST as $key=>$value) {echo $key ." ".value;} echo $_POST['petros']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82555-ajax-post-method/ Share on other sites More sharing options...
The Little Guy Posted December 20, 2007 Share Posted December 20, 2007 You need to create post variables, like so (they look the same as get variables): va = 'value1=cat&value2=dog&value3=bird'; The whole thing: function updateTeams(id, i){ var va = 'value1=cat&value2=dog&value3=bird'; var contentType = "application/x-www-form-urlencoded; charset=UTF-8"; var ajaxRequest; try{ ajaxRequest = new XMLHttpRequest(); } catch (e){ try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ alert("Your Browser Doesn't support AJAX."); return false; } } } ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ document.getElementById('email').innerHTML = ajaxRequest.responseText; } } ajaxRequest.open("POST", 'process/event/addTeam', true); ajaxRequest.setRequestHeader("Content-Type", contentType); ajaxRequest.send(va); } Quote Link to comment https://forums.phpfreaks.com/topic/82555-ajax-post-method/#findComment-419692 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.