Hello Everybody I have ajax code send data by GET method to php page I want to change the send data method in the code from GET TO POST THE CODE function Add_Site(){ var site_name = document.getElementById('site_name').value; var site_desc = document.getElementById('site_desc').value; var site_url = document.getElementById('site_url').value; if (site_name == "" || site_desc == "" || site_url == "") { document.getElementById('show_result').innerHTML = "fields is empty"; } else { Http.onreadystatechange = function(){ if (Http.readyState == 4) { document.getElementById('show_result').innerHTML = ""; document.getElementById('show_result').innerHTML = Http.responseText; } else { document.getElementById('show_result').innerHTML = ""; document.getElementById('show_result').innerHTML = "Loading"; } } rand = Math.random() * 999999999; url = "addSite.php?site_name=" + site_name + "&site_desc=" + site_desc + "&site_url=" + site_url + "&rand=" + rand; Http.open("GET", url, true); Http.send(null); } } Code (markup):
Hi Lionking, Follow the steps for using 'post' method in AJAX. 1. change the first argument of the "open" function from GET to POST. 2. the "url" variable should only consist of the site name. 3. Set some hhtp headers required for post method 4. Define a variable say 'parameters' and assign the additional parameters in the url variable to this new variable. 5. pass this new variable in the http.send method. Here is the changed code. Hope this helps. Unni Krishnan TechnoTwins Design and Solutions
Thank you Mr Unni krishnan for Help and also mr oceaniajimmy I make it if write this the code working rand = Math.random() * 999999999; url = "addSite.php"; parameters = "site_name=" + site_name + "&site_desc=" + site_desc + "&site_url=" + site_url + "&rand=" + rand; Http.open("POST", url, true); //Send the proper header information along with the request http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); Http.send(parameters); Code (markup): but if add this The code is not working http.setRequestHeader("Content-length", params.length); http.setRequestHeader("Connection", "close"); Code (markup): I want to know what this (params.length) thank you
Lionking, I know this wasn't your question, but I still somehow feel obliged to say this ... Take an hour of your time and get familiar with jQuery, which is a superior JavaScript library. It will take you hours and hours of work on your future project, the code is much more likely to run without problems on all browsers and it allows you to do a really trendy user interface with a few lines of code. For instance, a POST request in jQuery looks like this: $.post("location-of-your-script.php", { firstParameter: "this is first parameter's value", secondOne: "yeah!" }, function(data) { // what happens when the server sends a response $("#show_result").html(data); } ); Code (markup): I really suggest you using jQuery from the bottom of my heart, haha.