4

I have a form that submits via POST and I capture the variables once the form is submitted.

How can I concatenate the form data and then POST it to the url then re-direct to the thank you page?

This is not the exact code, I just can't find any normal answers, and I'm sure there is more than one way to do this. Just trying to figure out the simplest way possible.

if(isset($_POST['submit'])){
    $var1 = $_POST['var1'];
    $var2 = $_POST['var2'];

$url = 'https://api.this.com/foo/bar?token=IHAVETOKEN&foo=$Var1&bar=$var2'

post_request_to($url);

header("Location: thankyou.php");
}

EDIT:

HERE IS THE ACTUAL ANSWER/WORKING CODE:

if(isset($_GET['submit'])){
  $firstName = $_GET['firstname'];
  $lastName = $_GET['lastname'];
  $email = $_GET['email'];
  $password = $_GET['password'];
  $phone = $_GET['phone'];
}

  $data = array(
        'token' => 'sadfhjka;sdfhj;asdfjh;hadfshj',
        'firstName' => $firstName,
        'lastName' => $lastName,
        'email' => $email,
        'password' => $password,
        'phone' => $phone

    );


  $postvars = http_build_query($data) . "\n";

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://api.com/foo/bar?');
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  $server_output = curl_exec ($ch);

  curl_close ($ch);
8
  • you realize you have a parse error for what you posted. Plus, variables won't parse in single quotes. Commented Jul 26, 2017 at 23:31
  • it's not the exact code. I can't find just a general way to do this. Commented Jul 26, 2017 at 23:32
  • Plus, variables are case-sensitive. Your code failed on quite a few levels. Commented Jul 26, 2017 at 23:32
  • 1
    Welcome to StackOverflow. Please read How to Ask and minimal reproducible example then edit your question. Commented Jul 26, 2017 at 23:36
  • 1
    This is a wee-bit too broad. Why not turn your psuedo-code into real code and if it doesn't work or something maybe come back. I think the SO community is being way to generous here, this really is kind of a broad question for so many answers. Commented Jul 26, 2017 at 23:38

2 Answers 2

8

http_build_query

(PHP 5, PHP 7) http_build_query — Generate URL-encoded query string

Example:

<?php
$data = array(
    'foo' => 'bar',
    'baz' => 'boom',
    'cow' => 'milk',
    'php' => 'hypertext processor'
);

echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&amp;');

?>

The above example will output:

foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor

The rest depends on your flow logic. To post to another script:

From this answer:

Possibly the easiest way to make PHP perform a POST request is to use cURL, either as an extension or simply shelling out to another process. Here's a post sample:

// where are we posting to?
$url = 'http://example.com/script.php';

    // what post fields?
    $fields = array(
       'field1' => $field1,
       'field2' => $field2,
    );
    
    // build the urlencoded data
    $postvars = http_build_query($fields);
    
    // open connection
    $ch = curl_init();
    
    // set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
    
    // execute post
    $result = curl_exec($ch);
    
    // close connection
    curl_close($ch)
Sign up to request clarification or add additional context in comments.

3 Comments

I think this is what I'm looking for
@Matthew bet you are. You need to glue them together to work within your code. The examples in my answer should give you a good headstart. If you face an issue, just shoot me a comment and I will try to help. If my answer is actually what you are looking for, marking it as the answer to your question will be the next thing to do.
Thank you so much! This gave me exactly what I needed to get where I needed to go.
0

to concatenate url and do post method with file_get_contents

if(isset($_POST['submit'])) {
  $var1 = $_POST['var1'];
  $var2 = $_POST['var2'];

  $url = 'https://api.this.com/foo/bar?token=IHAVETOKEN&foo='.$Var1.
  '&bar='.$var2;


  $opts = array('http' =>
    array(
      'method' => 'POST',
      'header' => 'Content-type: application/x-www-form-urlencoded'
    )
  );

  $context = stream_context_create($opts);

  $result = file_get_contents($url, false, $context);

  header("Location: thankyou.php");
}

you can also using curl

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.