PHP Form Passthrough

Howdy! Sorry if this is simple for you PHP experts out there, but as I rarely am in that environment my searching has yet to provide a simple solution that hopefully someone else out there can.

A friend has a web page that submits a form to another site. He would like to capture this data locally before passing it off. Obviously the solution is to change the form submit to a local PHP page that saves the form values into a MySQL database and then pass the form variables to the original destination. Can anyone point me to some code examples or provide some to put me in the ballpark?
clubsideAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
On another question, we figured out that that probably couldn't be made to work.  But, if you can edit the form tag and add a 'onsubmit' attribute that calls a javascript routine, you can have that routine send all of the form data to a local PHP page.  When it's done, it returns to the form and the form submits as before.
0
osiris00Commented:
I hope I understood what you mean when you say" submit form to another site "..I presume you email the data captured?

To do what you want should be straightforward..
You process the form via php and hence you can do what you like with the form variables before submitting it.
So in the form tag you would have something that looks like this:
<form action="contact.php" method="post">

where contact.php  is the file that processes the form  and hence accesses the variables

You can find an example of this here:
http://myphpform.com/final-form.php

The example shows html file together with php file.
0
clubsideAuthor Commented:
I can make whatever changes to the local file are needed, my friend just wants something that works. No, it doesn't email the form, the action page is on a different site so my plan had been to change the submit to a local page and then forward the form on to its original destination for whatever those guys do with the data (I'm just trying to help out a friend, so I only know about the pages I have in front of me and that his host supports PHP and MySQL where my only experience has been in writing WordPress plug-ins).
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Dave BaldwinFixer of ProblemsCommented:
On a form submit, not just the data but the browser goes to the 'action' page.  That why a 'side trip' thru javascript and an AJAX post to a PHP page will work because it returns and let's the form submit as normal.  On another question we couldn't figure out a way to 're-post' form data so intercepting the form didn't work.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
osiris00Commented:
You could intercept and the repost...method depends on what is presented to user after submitting:
here is an example of local php file which then post to remote
<?php
      
      //create array of data to be posted which are obtained from form eg.
      $post_data['firstName'] = $_POST['yourname'];
      $post_data['action'] = $_POST['Register'];
      
      //traverse array and prepare data for posting (key1=value1)
      foreach ( $post_data as $key => $value) {
            $post_items[] = $key . '=' . $value;
      }
      
      //create the final string to be posted using implode()
      $post_string = implode ('&', $post_items);
      
      //we also need to add a question mark at the beginning of the string
      $post_string = '?' . $post_string;
      
      //we are going to need the length of the data string
      $data_length = strlen($post_string);
      
      //let's open the connection
      $connection = fsockopen('www.domainname.com', 80);
      
      //sending the data
      fputs($connection, "POST  /target_url.php  HTTP/1.1\r\n");
      fputs($connection, "Host:  www.domainname.com \r\n");
      fputs($connection,
          "Content-Type: application/x-www-form-urlencoded\r\n");
      fputs($connection, "Content-Length: $data_length\r\n");
      fputs($connection, "Connection: close\r\n\r\n");
      fputs($connection, $post_string);
      
      //closing the connection
      fclose($fp);
      
      ?>

alternatively cURL can be used within PHP if installed
0
rajkumar_pbCommented:
The modified version of the above code by osiris00.

Hope that help you
<?php
      
      //create array of data to be posted which are obtained from form eg.
      $post_data['firstName'] = $_POST['yourname'];
      $post_data['action'] = $_POST['Register'];
      
      //traverse array and prepare data for posting (key1=value1)
      foreach ( $post_data as $key => $value) {
            $post_items[] = $key . '=' . $value;
      }
      
      //create the final string to be posted using implode()
      $post_string = implode ('&', $post_items);
      
      //we also need to add a question mark at the beginning of the string
      $post_string = '?' . $post_string;
      
      //we are going to need the length of the data string
      $data_length = strlen($post_string);
      // Inserting DATA to local DB before post it to another domain
      $connection = mysql_connect($host,$un,$pwd);
      $query = "INSERT INTO local_db(column1,column2,..,$domain) VALUES($value1,$value2,'domainname.com')";
      $result = mysql_query($query,$connection);
      
      
      //let's open the connection
      $connection = fsockopen('www.domainname.com', 80);
      
      //sending the data
      fputs($connection, "POST  /target_url.php  HTTP/1.1\r\n");
      fputs($connection, "Host:  www.domainname.com \r\n");
      fputs($connection,
          "Content-Type: application/x-www-form-urlencoded\r\n");
      fputs($connection, "Content-Length: $data_length\r\n");
      fputs($connection, "Connection: close\r\n\r\n");
      fputs($connection, $post_string);
      
      //closing the connection
      fclose($fp);
      
      ?>

Open in new window

0
clubsideAuthor Commented:
I decided to go the route suggested by this solution but as it didn't include details I had to track down an easy way to go through jQuery to implement it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.