siddscool19 Posted October 15, 2008 Share Posted October 15, 2008 Can we send post data to a page using XMLHTTPREQUEST like we do with curl is it possible? Like I want to send name,address,city to a php file using xmhttprequest can I do that? Can someone please tell me how to do it and also I want to send data in background Link to comment https://forums.phpfreaks.com/topic/128504-send-post-data-using-xmlhttprequest/ Share on other sites More sharing options...
alexweber15 Posted October 15, 2008 Share Posted October 15, 2008 using only js and assuming you have a properly created XHR object stored in a variable called "xmlHttp": function post(){ if(xmlHttp){ var url = 'submit.php'; // the url you want to submit to var yourdata = document.getElemenyById('yourForm').serialize(); xmlhttp.onreadystatechange = XMLHttpRequestChange; xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xmlhttp.send(yourdata); Observations: - url can be any URL that will handle the POST, no biggie here - yourdata must be a set of "GET-style" key-value pairs; either you can format them by hand ie: var yourdata = "name=myname&age=myage&foo=bar"; if you do it by hand dont forget to URLEncode the values. - the better way to do the above step is to use the serialize() function instead: it can accept either a form as a parameter (assign an ID to the form and serialize the form based on its ID as in my example or it can also accept an associative array as a parameter and it will produce "POST-SAFE" content Link to comment https://forums.phpfreaks.com/topic/128504-send-post-data-using-xmlhttprequest/#findComment-666000 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.