Question

Posted

Hi all,

I want to PASS post data from a form to a php page and then to another php page.

At the moment it does this.

1. POST to External PHP page.
2. External PHP page displays SUCCESS or ERROR.

I don't want the user to see this.

Is there a way to implement it like so:

1. POST to local PHP page
2. POST from local PHP to external PHP.
3. Load PHP response into string.
4. If string contains word ERROR then display a message on local php page.
5. Otherwise Display "submission successful"

Share this post


Link to post
Share on other sites

7 answers to this question

  • 0

Posted

Hi Axel, it sounds to me like you want an Ajax call; have you considered this? It would at least cover the requirement of letting you post data to the external PHP page and retrieving back a result without moving your user off the page.

Share this post


Link to post
Share on other sites
  • 0

Posted

Yo again!

I did this by doing the following. Please note I don't know how security-risky is this. But here it goes.


[CODE]
<?php
session_start();

$_SESSION['post_data'] = $_POST;

?>
[/CODE]

Now, on the other page

[CODE]
<?php
session_start();
$_SESSION['post_data']/// Use this as the POST variable.
?>
[/CODE]

If you want to be sure that there data is cleared then do it this way:
[CODE]
<?php
session_start();
$_SESSION['post_data']/// Use this as the POST variable.

//After finishing the code:
unset($_SESSION['post_data']);
?>
[/CODE]

Hope this helps :)

Anybody who thinks this presents a security risk please let me know, because I'm using this technique right now. Although I'm not using a sensitive information.

Share this post


Link to post
Share on other sites
  • 0

Posted

I'll clarify a little bit. I'm sending the form data to [url="https://www.formstack.com/forms/index.php"]https://www.formstac...forms/index.php[/url]

I don't think the Formstack API supports JSON.

I can quite easily get a webpage into a variable using php:

[code]
$url = "https://www.formstack.com/forms/index.php"
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}[/code]

Now is it possible to somehow forward the $_POST data in the curl request so that I get the relevant data?

I hope that makes sense.

Edit: Hi Jose_49! I don't thing that technique will work on the basis that I don't have any access to the code on the https://www.formstack.com/forms/index.php page so unfortunately I can't alter it. What do you make of my suggestion above?

Share this post


Link to post
Share on other sites
  • 0

Posted

This is interesting:

http://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request

[code]
curl --data "param1=value1&param2=value2" http://example.com/resource.cgi
[/code]

No idea how to use this though.

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Jose_49' timestamp='1359826970' post='595497084']
I did this by doing the following.

<snip>
[/quote]
You've misunderstood, one of the two pages the OP wants to send the data to is on another web service!

[quote name='Axel' timestamp='1359827010' post='595497088']
I'll clarify a little bit. I'm sending the form data to [url="https://www.formstack.com/forms/index.php"]https://www.formstac...forms/index.php[/url]

I don't think the Formstack API supports JSON.
[/quote]
One way you could perhaps find out: grab the POSTman addon for Google Chrome and use it to send a request. Set the method to POST, place your json encoded string in the body (RAW mode), and set the 'Content-Type' header to 'application/json', then see what you get back.

[quote name='Axel' timestamp='1359827010' post='595497088']
I can quite easily get a webpage into a variable using php:

[code]
$url = "https://www.formstack.com/forms/index.php"
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}[/code]

Now is it possible to somehow forward the $_POST data in the curl request so that I get the relevant data?
[/quote]
This is the method I would suggest you use to do it, submitting to your own PHP script, then with that submitting to the other web service.

Assuming the web service accepts JSON encoded data, you could use the following code:

[code]
$data = json_encode($_POST);

$ch = curl_init('https://www.formstack.com/forms/index.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
);

$result = curl_exec($ch);
[/code]

If the web service does not accept JSON:

The default encoding for form data in a POST request is application/x-www-form-urlencoded. I just made a little test script, and it does not seem that PHP keeps a copy of it in this format in a server variable. The application/x-www-form-urlencoded format is described here: [url="http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1"]http://www.w3.org/TR...tml#h-17.13.4.1[/url]. It's essentially the same as query string format. e.g. forename=foo&surname=bar, and certain characters being encoded.

Thankfully though, there is no need to translate the $_POST array into an application/x-www-form-urlencoded string, you can simply give CURLOPT_POSTFIELDS an array, and it'll sort it out for you!

[code]
$ch = curl_init('https://www.formstack.com/forms/index.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
[/code]

[quote name='Axel' timestamp='1359829256' post='595497158']
This is interesting:

[url="http://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request"]http://superuser.com...-a-post-request[/url]

[code]
curl --data "param1=value1&param2=value2" http://example.com/resource.cgi
[/code]

No idea how to use this though.
[/quote]

Obviously this is command line usage (I'm sure you knew that, just making sure). Note that the data is in application/x-www-form-urlencoded form, as mentioned above! You could probably pass this as a string to php's exec() function, but that [b]could very likely be extremely insecure, so I would strongly discourage it[/b]!
2 people like this

Share this post


Link to post
Share on other sites
  • 0

Posted

Thanks so much for the reply! I'm not going to get the chance to fully assimilate and deploy this information 'til Monday. Will definitively get back to you/this thread then. Many thanks. :-)

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='theblazingangel' timestamp='1359833018' post='595497250']
Thankfully though, there is no need to translate the $_POST array into an application/x-www-form-urlencoded string, you can simply give CURLOPT_POSTFIELDS an array, and it'll sort it out for you!

[code]
$ch = curl_init('https://www.formstack.com/forms/index.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
[/code]

[/quote]

YOU SIR ARE AN ACTUAL GOD - THANK YOU SO MUCH!!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.