Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

Posting data from a web form

Can i post my html form data to two different pages at the same time?

I'd like to send my data to another web site of mine when i receive an order.

Is it possible?

What do you suggest I should do?

I'd like to send data to two different domain.
I use php and mysql
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

No,  Form POST can only go to one page at a time.  Note that the browser goes to the action page when you submit a form.

There are a number of things you can do to make a second POST.  You can use AJAX before the regular form POST or you can use PHP curl on the action page to send the data to another site.
can you please be more specific on what you are trying to accomplish?
It sounds like you just need to post the data to the database via the webserver, then let the other webpage poll the data when it loads.

Thanks.
Avatar of BR

ASKER

Dear Evan, are you suggesting that I should give them to access to mysql table, right?

Can they understand instantly when i insert a data to the table?
ok.  when you say other website, you mean outside yours?

If so, you need to do one of these three things:

1. Create an API for the other website to poll
2. Grant access to table directly <-- Most dangerous if you can't control the outcome, but you can mitigate it by locking down usernames and accesses.
3. use a message handler like socket.io or elephant.io (similar to option 1)

1 and 2 gives you "near real time" access and it's up to the other website to determine how and when it gets pulled.
3 is realtime, but takes some expertise to get it right.

Your call.
Avatar of BR

ASKER

Good answer, they asked api first?
I have a very big hosting package but the server is not mine. Can i still create an api for my web site?
ASKER CERTIFIED SOLUTION
Avatar of Evan Cutler
Evan Cutler
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

Thank you
There are a lot of moving parts to this question, so please read all of this carefully.

First part: A "reflector" script that will receive an HTTP request and reflect back the contents of the request variables.  We need this for testing.
<?php // demo/request_reflector.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// START A BUFFER TO CAPTURE THE BROWSER OUTPUT
ob_start();

// USE PREFORMATTING TO MAKE THE OUTPUT EASY TO READ
echo '<pre>';

// SHOW THE COOKIE(S)
echo '$_COOKIE: ';
var_dump($_COOKIE);
echo PHP_EOL;

// SHOW THE GET REQUEST
echo '$_GET: ';
var_dump($_GET);
echo PHP_EOL;

// SHOW WHAT WAS RECEIVED IN POST
echo '$_POST: ';
var_dump($_POST);
echo PHP_EOL;

// SHOW WHAT WAS RECEIVED IN FILES
echo '$_FILES: ';
var_dump($_FILES);
echo PHP_EOL;

// CAPTURE THE BUFFER
$posted_data = ob_get_clean();

// SAY THANK YOU
echo 'REQUEST REFLECTED FROM ' . $_SERVER['HTTP_REFERER'] . ' AT ' . date('c');
echo PHP_EOL;
echo $posted_data;

Open in new window


Next, a script that uses cURL to send the POST-request variables to the reflector.  This is what you would want to use to send a post request from one URL to another.  In theory, you could send the request to many URLs - just make additional cURL calls with new URL addresses!
<?php // demo/post_repost_with_curl.php
/**
 * https://www.experts-exchange.com/questions/28980840/Posting-data-from-a-web-form.html
 *
 * Demonstrate how to use cURL to make a POST request
 * This may be used to start an asynchronous process
 * Property 'title' is a comment field to identify the object
 *
 * http://php.net/manual/en/book.curl.php
 * http://curl.haxx.se/libcurl/c/libcurl-errors.html
 */
error_reporting(E_ALL);


Class POST_Response_Object
{
    public $href, $title, $http_code, $errno, $info, $document;

    public function __construct($href, $post_array=[], $title=NULL)
    {
        // ACTIVATE THIS TO AVOID TIMEOUT FOR LONG RUNNING SCRIPT
        // set_time_limit(10);

        // STORE THE CALL INFORMATION
        $this->href  = $href;
        $this->title = $title;

        // PREPARE THE POST STRING
        $post_string = http_build_query($post_array);

        // MAKE THE REQUEST
        if (!$this->my_curl($href, $post_string))
        {
            // ACTIVATE THIS TO SEE THE ERRORS AS THEY OCCUR
            // trigger_error("Errno: $this->errno; HTTP: $this->http_code; URL: $this->href", E_USER_WARNING);
        }
    }

    protected function my_curl($url, $post_string, $timeout=3)
    {
        // PREPARE THE CURL CALL
        $curl = curl_init();
        curl_setopt( $curl, CURLOPT_URL,            $url           );
        curl_setopt( $curl, CURLOPT_REFERER,        $_SERVER['HTTP_HOST'] );
        curl_setopt( $curl, CURLOPT_HEADER,         FALSE          );
        curl_setopt( $curl, CURLOPT_POST,           TRUE           );
        curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string   );
        curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
        curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout       );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );
        curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE           );

        // IF USING SSL, THIS INFORMATION MAY BE IMPORTANT
        // http://php.net/manual/en/function.curl-setopt.php#110457
        // http://php.net/manual/en/function.curl-setopt.php#115993
        // http://php.net/manual/en/function.curl-setopt.php#113754
        // REDACTED IN 2015 curl_setopt( $curl, CURLOPT_SSLVERSION, 3 );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE  );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE  );

        // RUN THE CURL REQUEST AND GET THE RESULTS
        $this->document  = curl_exec($curl);
        $this->errno     = curl_errno($curl);
        $this->info      = curl_getinfo($curl);
        $this->http_code = $this->info['http_code'];
        curl_close($curl);

        // RETURN DOCUMENT SUCCESS SIGNAL
        return $this->document;
    }
}


// IF NOTHING IS POSTED YET, PUT UP THE HTML FORM TO RECEIVE THE DATA
if (empty($_POST))
{
    $form = <<<ENDFORM
    <form method="post">
    ENTER TWO THINGS:
    <input name="foo" />
    <input name="bar" />
    <br/><input type="submit" />
    </form>
ENDFORM;
    die($form);
}


// THE URL WE WANT TO TALK TO
$url  = 'https://iconoun.com/demo/request_reflector.php';
$pro = new Post_Response_object($url, $_POST, 'Test');
echo '<pre>';
var_dump($pro);

Open in new window


When the reflector shows us the response we will be able to find it in the display of $_POST.  The entire output of this script, including the reflector output looks something like this.
object(POST_Response_Object)#1 (6) {
  ["href"]=>
  string(46) "https://iconoun.com/demo/request_reflector.php"
  ["title"]=>
  string(4) "Test"
  ["http_code"]=>
  int(200)
  ["errno"]=>
  int(0)
  ["info"]=>
  array(26) {
    ["url"]=>
    string(46) "https://iconoun.com/demo/request_reflector.php"
    ["content_type"]=>
    string(9) "text/html"
    ["http_code"]=>
    int(200)
    ["header_size"]=>
    int(177)
    ["request_size"]=>
    int(214)
    ["filetime"]=>
    int(-1)
    ["ssl_verify_result"]=>
    int(0)
    ["redirect_count"]=>
    int(0)
    ["total_time"]=>
    float(0.086539)
    ["namelookup_time"]=>
    float(0.012907)
    ["connect_time"]=>
    float(0.013005)
    ["pretransfer_time"]=>
    float(0.037733)
    ["size_upload"]=>
    float(15)
    ["size_download"]=>
    float(219)
    ["speed_download"]=>
    float(2530)
    ["speed_upload"]=>
    float(173)
    ["download_content_length"]=>
    float(-1)
    ["upload_content_length"]=>
    float(15)
    ["starttransfer_time"]=>
    float(0.084622)
    ["redirect_time"]=>
    float(0)
    ["redirect_url"]=>
    string(0) ""
    ["primary_ip"]=>
    string(12) "69.65.21.212"
    ["certinfo"]=>
    array(0) {
    }
    ["primary_port"]=>
    int(443)
    ["local_ip"]=>
    string(12) "69.65.21.212"
    ["local_port"]=>
    int(59846)
  }
  ["document"]=>
  string(219) "REQUEST REFLECTED FROM iconoun.com AT 2016-11-03T12:29:36-05:00
$_COOKIE: array(0) {
}

$_GET: array(0) {
}

$_POST: array(2) {
  ["foo"]=>
  string(3) "one"
  ["bar"]=>
  string(3) "two"
}

$_FILES: array(0) {
}

"
}

Open in new window

Avatar of BR

ASKER

Wow. Thank you Ray,
Thank you very much
It's sometimes worth it to leave your questions open a little longer.  E-E members are distributed around the globe, and we don't always see questions in "real time."  Just a thought...

If you're interested in seeing the design of an API, there is a drop-dead simple example here.
https://www.experts-exchange.com/articles/12239/Introduction-to-Application-Programming-Interfaces.html