LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-03-2011, 04:45 AM   #1
dstu
Member
 
Registered: Dec 2006
Posts: 35

Rep: Reputation: 0
Unhappy PHP Automatic Form Action


Hello,

I'm trying to prepare a php page that gets data by post, processes it and should redirect the visitor to another page, to which we also send other data by post.

As I can't use a simple HTML Form, I tried the fsockopen, but it only gives me back the result and doesn't GO to that page.

How should I do this?

Thanks a lot.

David
 
Old 08-04-2011, 03:31 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
You have basically two options.

Your PHP script can act as a reverse proxy, and do the POST request on behalf of the client, echoing the response to the client. You could do this using fopen() alone, if you construct the necessary HTTP request yourself, and your PHP configuration has allow_url_fopen enabled. In any case, note that you need to output the headers (everything before first empty line) using header(). You could also use the PECL extension pecl_http to do the reverse proxy query.

The common solution is to generate an intermediate page, which will be shown to the user. This page will contain the a hidden form, with values set in your PHP script. The form will be automatically submitted by a simple Javascript onload handler in the body element; it only needs to call the submit() method on the hidden form.

There are major differences between the limitations of the two methods. The former method can forward files in the POST request, while it is extremely difficult in the latter; you may have to modify the second POST handler to get file upload to work at all using the latter method. The latter method will also show the user an intermediate page, unless you use a hidden frame to target the initial POST request at. There may be other considerations too; for example, if there is a lot of data, the latter method will effectively transfer it three times during the process between the client and the servers, while the former method will transfer it only once between the client and the first server, and once between the two servers.

In general, if your POST data may contain file upload fields, go with the first method. If you know your POST data will not contain file upload fields, and relying on Javascript at the client end is not a problem, go with the second method.

Hope this helps.
 
1 members found this post helpful.
Old 08-08-2011, 05:38 AM   #3
dstu
Member
 
Registered: Dec 2006
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Nominal Animal View Post
Your PHP script can act as a reverse proxy, and do the POST request on behalf of the client, echoing the response to the client. You could do this using fopen() alone, if you construct the necessary HTTP request yourself, and your PHP configuration has allow_url_fopen enabled. In any case, note that you need to output the headers (everything before first empty line) using header(). You could also use the PECL extension pecl_http to do the reverse proxy query.
I don't need to "echo" a server's response to the client. I need a client to POST data to my page, get the data, manipulate it locally and then POST the new data I build to a 3rd party server, but in addition, I need the visitor to be routed to that page, just as if he pressed a SUBMIT button on my page, but it should be done automatically. I tried several PHP functions, such as socket_connect, fsockopen and curl_exec, but they all echoed the data, but didn't redirect me to the final page.


Quote:
Originally Posted by Nominal Animal View Post
The common solution is to generate an intermediate page, which will be shown to the user. This page will contain the a hidden form, with values set in your PHP script. The form will be automatically submitted by a simple Javascript onload handler in the body element; it only needs to call the submit() method on the hidden form.
Looks more like what we need. Is there an example to how to get this done? Does this mean we would need to integrate PHP with JavaScript to achieve this "automatic POST and redirect" feature we need?

Thanks.
 
Old 08-08-2011, 07:19 AM   #4
dstu
Member
 
Registered: Dec 2006
Posts: 35

Original Poster
Rep: Reputation: 0
Smile php + JavaScript

I finally found the way to do it, using JavaScript:

Code:
<?php

function prepare_post($param1, $param2, $param3, $param4) {

...	
        	return $post_string;
		
      }


// This line should be changed to get the incoming $_POST values
 $form_elements = prepare_post('1', '2', '3', '4');

 $inputs = '';

 foreach ($form_elements as $key => $value)
	{
		$inputs .= '<input type=\'hidden\' name=\'' . $key . '\' value=\'' . $value . '\'>';
	}
/**/

 
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <title>Checkout </title>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 <script type="text/javascript" language="javascript">
 function submitform(){
 document.getElementById('myForm').submit();
 }
 
 </script>
 
 </head>
 <body onload="submitform()">
 
	<form name="myForm" action="http://www.domain.com/page.html" method="post">
		<?php echo $inputs ?>
	</form>
           
 <script type="text/javascript" language="javascript">
	document.myForm.submit();
 </script>
 
  


Reply

Tags
form, php, post


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to show results of a php query from a form on the main form texmansru47 Programming 2 06-27-2008 01:26 PM
PHP: build query from form entry, then display results in the same form tonedeaf1969 Programming 4 06-22-2007 07:55 AM
form action ="?" ... where is my form posting to? verbatim Programming 2 05-23-2005 06:55 PM
is this php automatic form submit post wriiten correctly? fsock error.. verbatim Programming 1 05-11-2005 10:24 AM


All times are GMT -5. The time now is 03:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration