Brian W Posted July 20, 2009 Share Posted July 20, 2009 I'm finding several scripts that all send a request with a query string, but I want to send one using POST since I'm posting several textareas with line carriages and also I think their content is too long for a query string. Any suggestions, links, leads would be helpful. Thanks Link to comment https://forums.phpfreaks.com/topic/166707-solved-form-post-not-get/ Share on other sites More sharing options...
ldougherty Posted July 21, 2009 Share Posted July 21, 2009 What exactly is it you are looking for a suggestion of? You said it yourself instead of using FORM METHOD=GET use FORM METHOD=POST When you retrieve the variables use $_POST rather than $_GET Link to comment https://forums.phpfreaks.com/topic/166707-solved-form-post-not-get/#findComment-879106 Share on other sites More sharing options...
Brian W Posted July 21, 2009 Author Share Posted July 21, 2009 FORM METHOD=POST is what I want to simulate but use AJAX to post the data without reloading the entire page. Link to comment https://forums.phpfreaks.com/topic/166707-solved-form-post-not-get/#findComment-879542 Share on other sites More sharing options...
PFMaBiSmAd Posted July 21, 2009 Share Posted July 21, 2009 http://www.openjs.com/articles/ajax_xmlhttp_using_post.php Link to comment https://forums.phpfreaks.com/topic/166707-solved-form-post-not-get/#findComment-879549 Share on other sites More sharing options...
Brian W Posted July 21, 2009 Author Share Posted July 21, 2009 I'll need to build the qstring with that technique too, but the parameters are grabbed using the $_POST. I hardly see the point. Any ways, like I outlined before, I need to pass the values of some textareas in the form that likely will have line-carriages in it that aren't respected when your building a qstring. Maybe my question should be, how can I use AJAX to submit a form and respect the line-carriages in a textarea field? Link to comment https://forums.phpfreaks.com/topic/166707-solved-form-post-not-get/#findComment-879570 Share on other sites More sharing options...
xtopolis Posted July 22, 2009 Share Posted July 22, 2009 I did a little googling, it seems they just encode it properly. Afaik, the raw headers are in the same format as a GET variable would be. Saw this example: http://www.captain.at/howto-ajax-form-post-request.php (encodeURI) Then saw these key aspects: http://xkr.us/articles/javascript/encode-compare/ (this shows a raw header example for an array value being posted: http://www.phpfreaks.com/forums/index.php/topic,226617.msg1045540.html#msg1045540) Link to comment https://forums.phpfreaks.com/topic/166707-solved-form-post-not-get/#findComment-879996 Share on other sites More sharing options...
Brian W Posted July 22, 2009 Author Share Posted July 22, 2009 Thanks all for the help! I had trouble looping and getting the fields when they were in a table, so I did some research and found this page which gave me the idea to use elements rather than childNodes. Here is my final product (which also supports textarea elements). var http_request = false; function makePOSTRequest(url, parameters) { http_request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { // set type accordingly to anticipated content type //http_request.overrideMimeType('text/xml'); http_request.overrideMimeType('text/html'); } } else if (window.ActiveXObject) { // IE try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { alert('Cannot create XMLHTTP instance'); return false; } http_request.onreadystatechange = alertContents; http_request.open('POST', url, true); http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http_request.setRequestHeader("Content-length", parameters.length); http_request.setRequestHeader("Connection", "close"); http_request.send(parameters); } function alertContents() { if (http_request.readyState == 4) { if (http_request.status == 200) { //alert(http_request.responseText); result = http_request.responseText; //success code open_popup(result, true); } else { alert('There was a problem with the request.'); } } } function get(form, url) { var obj = document.getElementById(form); read_form(obj); makePOSTRequest(url, getstr); getstr = ""; } var getstr = ""; function read_form(obj){ if(obj.elements.length <= 0){ return false; } for (i=0; i<obj.elements.length; i++) { if (obj.elements[i].tagName == "INPUT") { if (obj.elements[i].type == "text") { getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&"; } if (obj.elements[i].type == "checkbox") { if (obj.elements[i].checked) { getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&"; } else { getstr += obj.elements[i].name + "=&"; } } if (obj.elements[i].type == "radio") { if (obj.elements[i].checked) { getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&"; } } } else if (obj.elements[i].tagName == "SELECT") { var sel = obj.elements[i]; getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&"; } else if (obj.elements[i].tagName == "TEXTAREA") { getstr += obj.elements[i].name + "=" + escape( encodeURI(obj.elements[i].value) ) + "&"; } else { alert(obj.elements[i].tagName); } } return getstr; } You'll need to run php urldecode() on the $_POST variables especially if they have a line-carriage in the value. Link to comment https://forums.phpfreaks.com/topic/166707-solved-form-post-not-get/#findComment-880577 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.