Passing Variables Without Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Passing Variables Without Form

Post by leewad »

Hi,

I am wanting to pass some variables to another site using post not get so the normal way like http://www.mywebsite.com/page.php?data1 ... ta2=$data2 does not work as the site has not recognised the submit.

Any ideas on how i can pass these with out using a form?

Thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Passing Variables Without Form

Post by AbraCadaver »

It will not redirect the browser but try http_post_fields() or http_post_data(). If you need a redirect with the post then you must use javascript:

Code: Select all

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;
}

http_post_redirect('test.php', array('test'=>123));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Re: Passing Variables Without Form

Post by leewad »

Thanks for your post but it does not work so will keep looking
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Passing Variables Without Form

Post by AbraCadaver »

leewad wrote:Thanks for your post but it does not work so will keep looking
Works great depending on what you want to do and if you use it properly. You probably want to set the third parameter to true.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Re: Passing Variables Without Form

Post by leewad »

This is the code which i have tried, not sure where i am going wrong?

Code: Select all

<? 

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;
}


http_post_redirect('https://forms.netsuite.com/app/site/crm/externalleadpage.nl', array( 
'compid' =>'970836',
 'formid' => '22',
 'h' => 'f43e7e0c1e0469055f30',
 'partner' => 'CURRENCYTODAY',
 'redirect_count' => '1',
 'did_javascript_redirect' => 'T',
 'firstname' => 'John',
 'lastname' => 'Bateman',
 'email' => 'johnbateman99@yahoo.co.uk',
 'phone' => '01923887565',
 'custentity_your_enquiry' => 'Rates Please',
 'submitted' => 'true')
 
 );

?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Passing Variables Without Form

Post by AbraCadaver »

AbraCadaver wrote:
leewad wrote:Thanks for your post but it does not work so will keep looking
Works great depending on what you want to do and if you use it properly. You probably want to set the third parameter to true.
third parameter true.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply