Andre J
Andre J

Reputation: 227

How can I submit an HTML form to Google docs as well as to a PHP file

I have the following form :

<div class="ss-form">
<form onsubmit = "return validateForm();" action="https://spreadsheets.google.com/formResponse?hl=en&amp;formkey=dHXVVeEUybk9UNEVYdEdrNlVOSTZMbFE6MA&amp;
theme=0AX42CRXMsmRFxMDNhN2Y&amp;ifq'" method="POST" id="ss-form" name="data"\>

This works well, and submits the data to Google Docs.

My question is, how can I submit this form to google docs AND submit it to a PHP file at the same time?

I have tried using php include, but it does not work as the google docs link is not a php file.

Also tries using cURL but not sure what Im doing wrong :

    //set options
    $curl_connection = curl_init('https://spreadsheets.google.com/');

    curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl_connection, CURLOPT_USERAGENT, 
      "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

    //set data to be posted
        $post_string = "formResponse?hl=en&amp;formkey=dHXVVeEUybk9UNEVYdEdrNlVOSTZMbFE6MA&amp;
    theme=0AX42CRXMsmRFxMDNhN2Y&amp;ifq";

    curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
    $result = curl_exec($curl_connection);

    //show information regarding the request
    print_r(curl_getinfo($curl_connection));
    echo curl_errno($curl_connection) . '-' . 
    curl_error($curl_connection);

    //close the connection
    curl_close($curl_connection);

Upvotes: 0

Views: 5622

Answers (6)

aalaap
aalaap

Reputation: 4411

Follow the instructions on this page and set up a custom handler for the Google Docs form submit:

In step 9, where you replace the form action with the JavaScript code, insert a function that dynamically creates a new form with the same fields and point it to the local PHP script. The function should also copy the data into the new form from the old form and then fire the submit event.

This way, the form data gets posted to Google Docs in the IFRAME and the second "copy" form posts the data to your PHP script. You can even have that second form post into a second, dynamically created IFRAME if you want to handle the 'thank you' in the same page.

Upvotes: 1

seriousdev
seriousdev

Reputation: 7656

First, set the form's action attribute to yourscript.php.
In yourscript.php write this:

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => "Content-Type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen(http_build_query($_POST)) . "\r\n", // updated line
        'content' => $_POST
    )
);

$context  = stream_context_create($opts);

$send_data = file_get_contents('<google-docs-form-url>', false, $context);

// then process data

Upvotes: 0

KJYe.Name
KJYe.Name

Reputation: 17169

Let's say you have a php file name submitForm.php, and you basically point the action of your form to submit to this php script. With in this script you handle your php side business logic, and after you are done with that you can use cURL to post a form to google doc. This is one way to do it.

Here is cURL tutorial on how to submit form: PHP cURL Submit Form

Try modify your code to this:

$curl_connection = curl_init('https://spreadsheets.google.com/formResponse');
//setup options for client url connection
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0;      Windows NT 5.1)');
//set data to be posted
    $post_string = "hl=en&amp;formkey=dHXVVeEUybk9UNEVYdEdrNlVOSTZMbFE6MA&amp;
theme=0AX42CRXMsmRFxMDNhN2Y&amp;ifq";

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//execute the post and then close the connection
curl_exec($curl_connection);

Better yet Format your POST data with this traverse:

//traverse array and prepare data for posting (key1=value1)
    foreach ( $_POST as $key => $value) {
        $post_items[] = $key . '=' . $value;
    }

    //create the final string to be posted using implode()
    $post_string = implode ('&', $post_items);

Upvotes: 0

Mikhail
Mikhail

Reputation: 9007

Use javascript to submit two ajax forms.

Upvotes: 0

konsolenfreddy
konsolenfreddy

Reputation: 9671

You can send it to your PHP file and send it to google Docs afterwards.

You'll have to add the $_POST parameters. See here for more info how to do that: Possible to post data in a PHP redirect?

Upvotes: 0

Tobias
Tobias

Reputation: 7380

First idea: submit it to your php script and then redirect from the php script to google as a second call.

Upvotes: 0

Related Questions