Jump to content

PHP Forms / PHP Form Builder Class


richgcook

Recommended Posts

Hi guys,

 

I'm having a bit of PHP problems trying to combine two bits of code which is definitely possible as it's simple if statements, but I'm a bit confused. Not sure if you guys are familiar, but there's a PHP Form Builder Class part of the Google Code Hosting Project. There's two forms I'm playing with one 'Google Spreadsheets' form which allows the data submitted via a form to be entered in a Google Spreadsheet, and one which sends the data to an email address.

 

http://www.imavex.com/php-form-builder-class/examples/email.php

http://www.imavex.com/php-form-builder-class/examples/google-spreadsheets.php

 

I have these working separately, but ideally want I need is to be able to combine them both, per se, and allow the data submitted to go to both the spreadsheet and the email address defined.

 

The code for the email is:

 

<?php
error_reporting(E_ALL);
session_start();
include("../class.form.php");

if(isset($_POST["cmd"]) && in_array($_POST["cmd"], array("submit_0"))) {
$form = new form("email_" . substr($_POST["cmd"], -1));
if($form->validate()) {
	$result = $form->email("my_username", "my_password", array(
		"to" => "my_recipient(s)", 
		"subject" => "my_subject", 
		"from" => "my_from", 
		"replyto" => "replyto", 
		"cc" => "my_cc", 
		"bcc" => "my_bcc", 
		"preHTML" => "my_prehtml", 
		"postHTML" => "my_posthtml",
		"css" => '<style type="text/css">...</style>',
		"cssFile" => "my_css.css or http://www.my_domain.com/my_css.css",
		"textonly" => "true/false"
	));

	if($result)
		header("Location: email.php?errormsg_" . substr($_POST["cmd"], -1) . "=" . urlencode("Congratulations! The information you enter has been emailed from your Google Gmail account."));
	else
		header("Location: email.php?errormsg_" . substr($_POST["cmd"], -1) . "=" . urlencode("Oops! The following error has occurred while sending information from your Google Gmail account.  " .  $form->getEmailError()));
}
else
	header("Location: email.php");

exit();
}
elseif(!isset($_GET["cmd"]) && !isset($_POST["cmd"])) {
$title = "Email w/PHPMailer + Google's Gmail Service";
include("../header.php");
?>

<p><b>Email w/Google's Gmail Service</b> - This project's email function provides the ability to email a form's submitted data using PHPMailer and Google's Gmail service.
This function has four parameters as seen below.</p>

<ul style="margin: 0;">
	<li>Google Account Email Address - Email address (including domain) of your Google Gmail account.</li>
	<li>Google Account Password - Password of your Google Gmail account.</li>
	<li>Additional Parameters (optional) - This parameter allows you to set various email settings through an associative array of key/value pairs.  Available settings are provided below.
		<ul style="margin: 0;">
			<li>to - Sets the email's to address.  If blank, this parameter will be set to the Google Gmail account email address used in the first parameter.</li>
			<li>subject - Sets the email's subject.</li>
			<li>from - Sets the email's from address.  If blank, this parameter will be set to the Google Gmail account email address used in the first parameter.</li>
			<li>replyto - Sets the email's reply to address.  If empty, the from address will be used.</li>
			<li>cc - Sets the email's CC address.</li>
			<li>bcc - Sets the email's BCC address.</li>
			<li>preHTML - Allows you to prepend html content above the form's submitted data.</li>
			<li>postHTML - Allows you to append html content below the form's submitted data.</li>
			<li>textonly - Sends text-only version of the form's submitted data.  By default, the email function will send an email containing both an html and text version.</li>
			<li>css - Gives you the ability to style the html email as needed.  This parameter should be passed as a string beginning with <style type="text/css"> and ending with </style></li>
			<li>cssFile - Gives you the ability to style the html email as needed by specifying a css include file.</li>
		</ul>
	</li>
</ul>

<p>Before getting started, you'll want to review the checklist of information provided below to ensure you have a good understanding on how this functionality works.</p>

<ol style="margin: 0;">
	<li>You'll need a Google Gmail account.  If you don't have one, you can create one by clicking the "Create an account" link at <a href="http://mail.google.com">http://mail.google.com</a>.</li>
	<li>to, replyto, cc, and bcc can contain multiple email addresses - just separate them with commas.</li>
	<li>to, from, replyto, cc, bcc can contain email addresses formatted as either "my@email.com" or "My Email <my@email.com>"</li>
	<li>Within the email function, a call is made to another public function - getEmail - to get the email's html/text content.  If you already have an existing system in place for sending email, you can use this function instead
	of the project's email function to build a string containing an html/text representation of the form's submitted data.  By default, this function will return html, but you can pass true as the first and only parameter to return
	text.</li>
</ol>	

<p>In the php source code of this example file, you'll see that the email function call currently contains demo authentication/email settings ("my_email", "my_password", etc).
You'll want to replace these with your information.  Another important thing to note is that the various email settings that can be applied through the email function's fourth parameter
are optional.  In the php source code of this example file, you'll find all of them listed for reference in the email function call.  Feel free to include as many or few as needed.</p>

<?php
$form = new form("email_0");
$form->setAttributes(array(
	"map" => array(2, 2, 1, 3),
	"width" => 500
));

if(!empty($_GET["errormsg_0"]))
	$form->errorMsg = filter_var(stripslashes($_GET["errormsg_0"]), FILTER_SANITIZE_SPECIAL_CHARS);

$form->addHidden("cmd", "submit_0");
$form->addTextbox("First Name:", "FName");
$form->addTextbox("Last Name:", "LName");
$form->addEmail("Email Address:", "Email");
$form->addTextbox("Phone Number:", "Phone");
$form->addTextbox("Address:", "Address");
$form->addTextbox("City:", "City");
$form->addState("State:", "State");
$form->addTextbox("Zip Code:", "Zip");
$form->addButton();
$form->render();

include("../footer.php");
}
?>

 

And for the spreadsheet:

 

<?php
error_reporting(E_ALL);
session_start();
include("../class.form.php");

if(isset($_POST["cmd"]) && in_array($_POST["cmd"], array("submit_0"))) {
$form = new form("googlespreadsheets_" . substr($_POST["cmd"], -1));
if($form->validate()) {
	if($form->sendToGoogleSpreadsheet("my_email", "my_password", "my_spreadsheet_title", "(optional) my_worksheet_title"))
		header("Location: google-spreadsheets.php?errormsg_" . substr($_POST["cmd"], -1) . "=" . urlencode("Congratulations! The information you enter has been sent your Google Docs spreadsheet."));
	else
		header("Location: google-spreadsheets.php?errormsg_" . substr($_POST["cmd"], -1) . "=" . urlencode("Oops! The following error has occurred while sending information to your Google Docs spreadsheet.  " .  $form->getGoogleSpreadsheetError()));
}	
else
	header("Location: google-spreadsheets.php");
exit();
}
elseif(!isset($_GET["cmd"]) && !isset($_POST["cmd"])) {
$title = "Google Spreadsheets";
include("../header.php");
?>

<p><b>Google Spreadsheets</b> - This project's sendToGoogleSpreadsheet function provides the ability to send a form's submitted data direclty to a Google Docs spreadsheet using the Google Spreadsheet API.
This function has four parameters as seen below.</p>

<ul style="margin: 0;">
	<li>Google Account Email Address - Email address (including domain) of your Google account.</li>
	<li>Google Account Password - Password of your Google account.</li>
	<li>Google Docs Spreadsheet - The title of the spreadsheet where you'd like the form's submitted data to be sent.</li>
	<li>Worksheet (optional) - The title of the worksheet to be used within the specified spreadsheet.
	This parameter will default to the the spreadsheet's first worksheet.</li> 
</ul>

<p>Before getting started, you'll want to review the checklist of information provided below to ensure you have a good understanding on how this functionality works.</p>

<ol style="margin: 0;">
	<li>You'll need a Google account.  If you don't have one, you can create one by clicking the "Create an account now" link at <a href="http://docs.google.com">http://docs.google.com</a>.</li>
	<li>If the spreadsheet title you specify in the sendToGoogleSpreadsheet function does not exist, a new spreadsheet will be created for you with the appropriate title and column headers.</li>
	<li>If you're creating your spreadsheet manually through the Google Docs GUI, an important thing to keep in mind is that Google will treat the initial row of cells as column identifiers.
	These column identifiers must match an element's label used in your form ("First Name:", "Last Name:", etc), which enables the form's submitted 
	data to be correctly placed within the appropriate column.  If the spreadsheet you specify in the sendToGoogleSpreadsheet function exists but has no inital row of column headers, your form's data will not
	populated upon submission.  Your spreadsheet does not need to contain a column for every element used in the form - data for those elements that are
	not included will just not be collected.  Likewise, your spreadsheet can contain column identifiers that don't match an element's label used in the form - data for those columns will
	be left blank.</li>
	<li>Elements of type hidden, captcha, button, html, and htmlexternal will not be included in the information that is sent to your Google spreadsheet.</li>
	<li>The "ignoreGSSend" element attribute can be applied to form elements that you do not want to be send to your Google Docs spreadsheet.  The hidden field "cmd" has this attribute set in the form below.</li>
	<li>If you're populating an existing spreadsheet in your Google Docs account, you can store the web server's date/time at the moment the form's data is submitted
	by adding "Timestamp" as a column header.</li>
</ol>

<p>In the php source code of this example file, you'll see that the sendToGoogleSpreadsheet function call currently contains demo authentication/spreadshet settings ("my_email", "my_password", etc).
You'll want to replace these with your information.</p>

<?php
$form = new form("googlespreadsheets_0");
$form->setAttributes(array(
	"map" => array(2, 2, 1, 3),
	"width" => 500
));

if(!empty($_GET["errormsg_0"]))
	$form->errorMsg = filter_var(stripslashes($_GET["errormsg_0"]), FILTER_SANITIZE_SPECIAL_CHARS);

$form->addHidden("cmd", "submit_0");
$form->addTextbox("First Name:", "FName");
$form->addTextbox("Last Name:", "LName");
$form->addEmail("Email Address:", "Email");
$form->addTextbox("Phone Number:", "Phone");
$form->addTextbox("Address:", "Address");
$form->addTextbox("City:", "City");
$form->addState("State:", "State");
$form->addTextbox("Zip Code:", "Zip");
$form->addButton();
$form->render();

include("../footer.php");
}
?>

 

I have managed to speak to one of the admins, who can't help me further, but states:

 

'My recommendation would be to use both the $form->email() and $form->sendToGoogleSpreadsheet() methods after your form has been submitted.  It's not a "one or the other" type of scenario.'

 

Could you help me out and show me how this would be possible which in turn I can learn from?

 

Thanks, and sorry for the long post but felt it was better as a code include rather than an attachment?

 

Richard

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.