Jump to content

[SOLVED] Cant retrive $_POST vars after AJAX POST


nadeemshafi9

Recommended Posts

Hi guys im trying to retrive my poisted vars from my form, im trying to read them in the page i post them to via AJAX but they are empty, the system works it goes throgh to the page and echos things but teh POST vars are empty.

 

here is the JS

 

function post_admin_form(id, adminpage) {

params = "id=" + id + "&action=update";

if(adminpage == "indexpages") http.open("post", "common/AJAX/indexpages.php?id=" + id + "&action=update", true); 

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function () { 
	if(http.readyState == 4 && http.status == 200){ 
		var response = http.responseText;
		if(response) { 
			if(adminpage == "indexpages") document.getElementById("maincontentarea").innerHTML = response;
		}
	}
}
http.send(params);
}

Link to comment
Share on other sites

you haven't initilized the ajax variable:

 

var http;  // The variable that makes Ajax possible!

try{
	// Opera 8.0+, Firefox, Safari
	http = new XMLHttpRequest();
} catch (e){
	// Internet Explorer Browsers
	try{
		http = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			http = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			// Something went wrong
			alert("Your browser broke!");
			return false;
		}
	}

Link to comment
Share on other sites

i have initialised it but thanks m8 i have solved this problem now,

 

in order for you to recive them as your $_post vars you need the following MIME

 

http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

 

you have to make all your form data into a string and send it in the http.send(params);

 

function getFormValues(fobj,valFunc)
{
var str = "";
var valueArr = null;
var val = "";
var cmd = "";

for(var i = 0;i < fobj.elements.length;i++)
{
	//alert(fobj.elements[i].type);

	switch(fobj.elements[i].type)
	{
		case "text":
			if(valFunc)
			{
				//use single quotes for argument so that the value of
				//fobj.elements[i].value is treated as a string not a literal
				cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
				val = eval(cmd)
			}
			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
		break;
		case "textarea":
			if(valFunc)
			{
				cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
				val = eval(cmd)
			}
			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
		break;
		case "hidden":
			if(valFunc)
			{
				cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
				val = eval(cmd)
			}
			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
		break;
		case "button":
			if(valFunc)
			{
				cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
				val = eval(cmd)
			}
			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
		break;
		case "file":
			if(valFunc)
			{
				cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
				val = eval(cmd)
			}
			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
			str += fobj.elements[i].name + "_size=" + escape(fobj.elements[i].size) + "&";
		break;
		case "checkbox":
			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].checked) + "&";
		break;
		case "select-one":
			str += fobj.elements[i].name + "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
		break;
	}
}
str = str.substr(0,(str.length - 1));
return str;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.