Solved

Posting data from a web form

Posted on 2016-11-03
10
24 Views
Last Modified: 2016-11-03
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
0
Comment
Question by:Braveheartli
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41872544
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.
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 41872548
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.
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41872558
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?
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 9

Expert Comment

by:Evan Cutler
ID: 41872566
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.
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41872574
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?
0
 
LVL 9

Accepted Solution

by:
Evan Cutler earned 500 total points
ID: 41872590
yes.  You can create a php-based API from your data system.
Some examples provided:

https://www.leaseweb.com/labs/2015/10/creating-a-simple-rest-api-in-php/
http://blog.ijasoneverett.com/2013/02/rest-api-a-simple-php-tutorial/
https://www.toptal.com/php/building-rest-api-for-legacy-php-projects

in short, you create a php document that outputs a json object of results.  You can use several techniques to attach each php page to the REST style URLs (ie. http://domain.com/queryname/parameter1/parameter2)

then add users to your php solution specific to those urls.
Grant those urls to said users, and pass that to the other web team.
0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41872591
Thank you
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41872608
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

0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41872615
Wow. Thank you Ray,
Thank you very much
1
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41872631
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
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question