Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP open another webpage with POST data

I'm new to PHP and I'm trying to do something that may be bad practise and may well be impossible. I'm basically just hacking something together to test my knowledge and see what PHP can do.

I have one webpage with a form that collects data. That is submited to a PHP script that does a bunch of processing - but doesn't actually display anything important. What I want is that once the processing is done, the script then tells the browser to open another page, where the results are displayed.

I know I can use header('Location: page.php'); but I can't work out how to provide POST data with this. How can I do that? Alternatively, is there another way to tell the browser to open another page?

EDIT: What I'm taking from the responses is that it's possible to do this using various hacks but I'd be better off to just have the processing and the display code in one file. I'm happy with that; this was an experiment more than anything.

like image 419
thornate Avatar asked Nov 22 '08 11:11

thornate


People also ask

How can we load another page using PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

How redirect data from one page to another in PHP?

Redirection from one page to another in PHP is commonly achieved using the following two ways: Using Header Function in PHP: The header() function is an inbuilt function in PHP which is used to send the raw HTTP (Hyper Text Transfer Protocol) header to the client.

How can I connect two PHP pages?

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

How post URL in PHP?

If you're looking to post data to a URL from PHP code itself (without using an html form) it can be done with curl. It will look like this: $url = 'http://www.someurl.com'; $myvars = 'myvar1=' . $myvar1 .


2 Answers

You could use JavaScript as a dirty work-around:

<form id="redirect_form" method="post" action="http://someserver.com/somepage.php">
    <input type="hidden" name="field_1" value="<?php echo htmlentities($value_1); ?>">
    <input type="hidden" name="field_2" value="<?php echo htmlentities($value_2); ?>">
    <input type="hidden" name="field_3" value="<?php echo htmlentities($value_3); ?>">
</form>
<script type="text/javascript">
    document.getElementById('redirect_form').submit();
</script>

(the script should be below the form)

like image 197
Tom Avatar answered Nov 15 '22 19:11

Tom


Is it really necessary to call another page after the processing is done? I'd probably do the following:

<form method="post" action="display.php">
...
</form>

display.php:

if ($_POST) {
    require_once(process.php);
    process($_POST);
    display_results;
}

with process.php containing the code necessary for processing the post request.

Alternatively, you could use something like the cURL library to pass the results of the processing to a page specified by yourself. Don't know if that's really what you're after though.

like image 25
Josh Smeaton Avatar answered Nov 15 '22 21:11

Josh Smeaton