Skip to content

Instantly share code, notes, and snippets.

@iddar
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save iddar/86792f2f600d0b87737d to your computer and use it in GitHub Desktop.

Select an option

Save iddar/86792f2f600d0b87737d to your computer and use it in GitHub Desktop.
Send data to Google Form
{
"name": "sendform",
"version": "0.0.0",
"authors": [
"Iddar Olivares <[email protected]>"
],
"main": "index.html",
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"jquery": "~2.1.4"
}
}
(function ($) {
// Name: sendGoogleForm
// params:
// url: url to GoogleForm.php file
// ej: 'path/otherFolder/GoogleForm.php'
// callback: object content tow functions
// ej: {error: function(){...}, success: function(){...}}
$.fn.sendGoogleForm = function(url, callback) {
this.submit(function(event){
event.preventDefault();
var emailInput = $('#email',this).val();
$.ajax({
url: url,
data: {
email: emailInput
},
type: 'post',
error: callback.error.bind(this),
success: callback.success.bind(this)
});
});
return this;
};
})(jQuery);
!function(t){t.fn.sendGoogleForm=function(e,s){return this.submit(function(i){i.preventDefault();var r=t("#email",this).val();t.ajax({url:e,data:{email:r},type:"post",error:s.error.bind(this),success:s.success.bind(this)})}),this}}(jQuery);
<?php
header('Content-Type: application/json');
// Check for empty fields
if(empty($_POST['email']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
$response = array(
'message' => 'Error empty fields!',
'errors' => array(
"resource" => "Issue",
"field" => "email",
"code" => "missing_field"
)
);
header("HTTP/1.0 404 Not Found");
$response = json_encode($response);
exit($response);
}
//extract data from the post
extract($_POST);
//Add Google From ID
$GFORM_ID = 'MyGoogleFormID';
//set POST variables
// 'entry.{{id-item}}' => urlencode($value)
// ej: 'entry.2536387' => urlencode($email)
$fields = array(
'entry.{{item-id}}' => urlencode($email)
);
$response = SendDataToGoogleForm($fields, $GFORM_ID);
exit($response);
function SendDataToGoogleForm($fields, $GFORM_ID){
//Complete URL form
$url = 'https://docs.google.com/forms/d/'. $GFORM_ID .'/formResponse';
$fields_string="";
//url-ify the data for the POST
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
// remove last '&' item
rtrim($fields_string, '&');
//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, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post action
$curlResponse = curl_exec($ch);
//Get status code
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response = array('message' => 'saved fields' );
//Check curl object for errors
if(curl_errno($ch) || $httpCode == 404){
$response = array(
'message' => curl_error($ch) ? curl_error($ch) : 'Server no found 404',
'errors' => array(
"resource" => "Error",
"field" => "curl",
"code" => "error_connection"
)
);
header("HTTP/1.0 404 Not Found");
$response = json_encode($response);
exit($response);
}
//close connection
curl_close($ch);
return json_encode($response);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<style>
.error{
color: red;
}
.ok{
color: green;
}
</style>
</head>
<body>
<form id="emailForm">
<input type="email" id="email" required>
<span class="error"></span>
<span class="ok"></span>
<div>
<input type="submit" value="Enviar">
</div>
</form>
<script src="GoogleForm.min.js"></script>
<script>
$('#emailForm').sendGoogleForm("GoogleForm.php", {
//response equals to $.ajax method
error: function(response, statusCode, errorThrown){
var error = response.responseJSON.message;
$error = $('.error');
$error.html(error);
$error.show(0).delay(1000).hide(0);
},
success: function(response){
$okItem = $('.ok');
$okItem.html(response.message);
$okItem.show(0).delay(1000).hide(0);
this.reset();
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment