Post and Redirect

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I remembered seeing this question on the list several times in the past,
so I thought I would post something I just hacked up for someone.

As we know, we can user header() to redirect the browser, but of course
we can't redirect the browser and have it post data to the new page.  If
you need to do this it will require javascript.  Here's a quick and
dirty function:

function http_post_redirect($url='', $data=array(), $doc=false) {

	$data = json_encode($data);

	if($doc) {
		echo "<html><head></head><body>";
	}
	echo "
	<script type='text/javascript'>
		var data = eval('(' + '$data' + ')');
		var jsForm = document.createElement('form');
		
		jsForm.method = 'post';
		jsForm.action = '$url';
		
		for (var name in data) {
			var jsInput = document.createElement('hidden');
			jsInput.setAttribute('name', name);
			jsInput.setAttribute('value', data[name]);
			jsForm.appendChild(jsInput);
		}
		document.body.appendChild(jsForm);
		jsForm.submit();
	</script>";

	if($doc) {
		echo "</body></html>";
	}
	exit;
}

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux