Jump to content

[SOLVED] form "POST", not get


Brian W

Recommended Posts

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

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?

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)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.