Reputation: 119
I am going to try and explain this as best I can, I hope that I make sense as English is not my first language.
Lets say that I have a form on page 1 which contains these 3 inputs:
$_POST["name"];
$_POST["amount"];
$_POST["email"];
And on a second page process.php I also have another input:
$_POST["message"];
And finally on an external website there is a form that I want to POST to without having to type anything into their system:
<form action="display.php" method="post">
<input type="text" name="name"><br>
<input type="text" name="amount"><br>
<input type="text" name="email"><br>
<input type="text" name="message"><br>
<input type="submit">
</form>
I then want to be able to be automatically redirected to www.example.com/display.php from my process.php page and submit the form automatically and be able to see all of my information that I just entered
Example of what display.php looks like:
I do not have access to the code on the external website or display.php so this means that I cannot edit the code or use any sessions.
I hope that you understand me and that you could be able to help me, I would be so grateful if you could help me!
EDIT:
I tested some code that I was given in an answer and looks promising but it didn't pass the POST data to the external page (Which is not external for testing purposes)
Here is the code used to test below:
process.php
<?php
$_POST["name"] = "name";
$_POST["amount"] = "amount";
$_POST["email"] = "email";
$_POST["message"] = "message";
$post = array();
$post[] = "name=" . $_POST["name"];
$post[] = "amount=" . $_POST["amount"];
$post[] = "email=" . $_POST["email"];
$post[] = "message=" . $_POST["message"];
$ch = curl_init('http://localhost/display');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
header('Content-Type: text/html');
echo curl_exec($ch);
?>
display.php
<?php
echo $_POST["name"];
echo $_POST["amount"];
echo $_POST["email"];
echo $_POST["message"];
?>
But for some reason this did not work?
Upvotes: 4
Views: 12231
Reputation: 5665
How about this? It assumes name, amount and email will be posted to process.php
Added trouble shooting code.
process.php
<?php
//[put database queries here]
// $message = ??????????????????
$message = 'Message';
$post = array(
'name'=>$_POST['name'],
'amount'=>$_POST['amount'],
'email'=>$_POST['email'],
'message'=>$message);
$ch = curl_init('http://www.example.com/display.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING,"");
header('Content-Type: text/html');
$data = curl_exec($ch);
echo $data;
?>
does the redirect to the actual site.
<?php
$name = $_POST["name"];
$amount = $_POST["amount"];
$email = $_POST["email"];
$message = $_POST["message"];
echo <<< EOT
<html><head><style></style></head><body>
<form id="form" action="http://www.example.com/display.php" method="post">
<input type="hidden" name="name" value="$name"/>
<input type="hidden" name="amount" value="$amount"/>
<input type="hidden" name="email" value="$email"/>
<input type="hidden" name="message" value="$message"/>
</form>
<script>document.getElementById("form").submit();</script>
</body></html>
EOT;
?>
Upvotes: 2
Reputation: 3907
The script on the server cannot POST to a page the same way a browser can, so strictly speaking what you’re asking for can’t be done. You can do similar things that may still solve your problem...
1) process.php can use Curl to call display.php and display the results directly on process.php
2) the original page with the form can do some Ajax (or similar) to determine what $message should be, then add it as a Hidden input via JavaScript, and only then submit the form. No intermediate process.php form— we go straight to display.php
I think #2 will be best for the situation you describe
Upvotes: 0
Reputation: 441
You can have process.php
create and submit a form using JavaScript with something like this:
<form id="form" action="display.php" method="post">
<input type="hidden" name="name" value="<?= $_POST["name"] ?>">
<input type="hidden" name="amount" value="<?= $_POST["amount"] ?>">
<input type="hidden" name="email" value="<?= $_POST["email"] ?>">
<input type="hidden" name="message" value="<?= $_POST["message"] ?>">
</form>
<script>document.getElementById("form").submit();</script>
Hope I helped! -CM
Upvotes: 0