Does anyone have any code that they would share to:
- use form to accept name, email address, languauge (English or Spanish)
- send back an email with file attachment in the selected lauguage. files are stored in permanent file in online (url location).
- populate permanent speadsheet online (url location) with name, email, language and date/time sent.

I am very unskilled at this. It's for our charity and we don't have the funds to hire someone.
(I don't see recent help on this on your site. If there is, please let me know.)

Thank you,
Robin LeoGrande
www.txcpsh.org

Recommended Answers

All 5 Replies

Member Avatar for diafol

Good luck with that. Why do you need a spreadsheet to store that data? Can't you use a MySQL DB if you're using php?

@robin 2,

Diafol is precisely correct, a features like this requires a database. Is your organization have a mysql installed in their server? If you can manage to set up a msyql server this should be an easy task for you. Here is a tutorial on how to create spreadsheets from database. Always remember google can make things a lot easier.

Another thoughts: If you don't have mysql server, will you be willing to use flat file? I don't recommend this because of the security issues, but there are always some methods in protecting flat files.

As far as readily available script is concern, that one is a little tricky to find. However, you can practice on simple example shown below. I just pulled them out from the top of my head. It's been a while, since the last time I wrote in pure procedural. So, my coding techniques may not look good as others. Make sure to sanitize the form data, before sending them to your database insert query.

Looking at my example script below, you will noticed two files english.pdf and spanish.pdf. Change this to your requirements. The mail() function I used in this example is the most basic. For more php mail function, please read here.

<?php

## define your form
    $this_form = '<form method="post" action="">
        <label>Name</label>
        <input type="text" name="name" placeholder="enter your name">
        <br/>
        <label>Email </label>
        <input type="text" name="email" placeholder="enter your email">
        <br/>
        <label>Select your language</label>
        <select name="lang">
        <option>English</option>
        <option>Spanish</option>
        </select>
        <br/>
        <input type="submit" name="submit" value="submit"/>
        </form>';

        ## define errors
        $error_one = '<h2>You must fill up the form completely</h2>';
        $email_error = '>>> Email is not valid';
        $name_error = '>>> Please provide us your Name';

        ## define location of the file based on the selected language of post
        $spanish_file = '<a href="spanish.pdf" target="_blank"> Spanish</a>';
        $english_file = '<a href="english.pdf" target="_blank"> English</a>';

        ## define your email contents
        $yourSiteEmail = 'webmaster@txcpsh.org';
        $subject = 'Your Requested Information';
        $headers = 'From: '.$yourSiteEmail . "\r\n" .
        'Reply-To: webmaster@txcpsh.org' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();




## process form if it is set.
            ## confirm the form submission and make sure everything are ok
    if(isset($_POST['submit'])&&(!empty($_POST['email'])&&(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)))&& (!empty($_POST['name'])&&(filter_var($_POST['name'],FILTER_SANITIZE_STRING)))&&(!empty($_POST['lang']))){

        ## collected form data below needs to be sanitized properly, before adding to your database
        echo 'Email: '. $_POST['email'] .'<br/>';
        echo 'Name: '. $_POST['name']  .'<br/>';
        echo 'Language: '. $_POST['lang']  .'<br/>';

        ## define files for language selections
        if ($_POST['lang'] == 'English') $file = $english_file;
        if ($_POST['lang'] == 'Spanish') $file = $spanish_file;
        ## you must comment this on production server
        echo 'File Download: '. $file;

        ## use simple php mail function
            ## define email message body
            $message = 'Thank you very much for contacting us. Here is the link to download file : '. $file ;
            $mail = @mail($_POST['email'], $subject, $message, $headers);
    }

        ## form is not ok something maybe missing
    else if((empty($_POST['email'])||(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false))|| (empty($_POST['name']))){

    ## if either email or name is not valid show them the error one
    echo $error_one ;

    ## show error for the email
    if((empty($_POST['email'])||(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false))) echo  $email_error.'<br/>';

    ## show the error for the name
        if((empty($_POST['name']))) echo $name_error .'<br/>';

         ## show the form again
        echo '<br/>'. $this_form ;
    }


    else{

     ## form is not submitted, show the default form on the page
        echo '<h2>Your Form</h2>';
        echo $this_form;
    }




?>

Please Read: Since that your organization is for non-profit, feel free to send me a PM, if you need more assistance in writing your script. I truly appreciate the services and assistance that your organization is providing in your community. Such form of American generosity is highly needed in times like this.

veedeoo, Thank you for your response and offer to help. I am using goggle docs so the xls capability to store data exists. I will look at the code above and test it in my environment and share my results. Thank you so much!

Hi,

To use the google doc API, you will need the following.

  1. Zend_Gdata- Only this file. Unzipped this file in the root directory of your site.

  2. Download this file Google_Spreadsheet.php. Place this file in the same directory as your site's index.php

  3. You must have a google gmail account...

If we are going to port my example script above to the zend_Gdata, we can add the following codes . ( please refer to the line numbers of my example codes above ).

add codes on line 2

    ## this is the common Zend coding convention when including any Zend libraries.
    ## if you have not used Zend libraries in the past, this can get a bit tricky.

    set_include_path(get_include_path() . PATH_SEPARATOR . "$_SERVER[DOCUMENT_ROOT]/ZendGdata-1.12.1/library");

    ## include the google spreadsheet class you downloaded from github
    require_once('Google_Spreadsheet.php');

add codes on line 36

    ## define your google email account ! make sure you are allowed to access google doc.. normally, this is included by default.

    $gmail_user = 'yourEmail@gmail.com';
    $gmail_pass = 'your_gmail_password';
    ## define the name of your spreadsheet's file name 
    $sheet_name = 'txcpsh';
    ## If you have an existing spreadsheets, you must change the above filename.

    ## define the columns of your spreadsheets. I will be using what we already have right now as shown on my example codes.
    ## again, if you have an existing spreadsheet, make sure the column name matched that of the exsiting column names.
    $col_1 = 'Name';
    $col_2 = 'Email';
    $col_3 = 'Language';

on line 47 add,

    ## let's fire up an instance of the Google_spreadsheet class
    ## the spreadsheet class wanted us to provide at least two info. user and pass
    ## function __construct($user,$pass,$ss=FALSE,$ws=FALSE)

    $this_sheet = new Google_Spreadsheet($gmail_user, $gmail_pass);

    ## let's point it to your our spreadsheet file in in google
        ## I will be using the method useSpreadsheet($ss,$ws=FALSE),

    $this_sheet->useSpreadsheet($sheet_name);

    ## Assign the columns and its values into an array

    $this_values = array(
                      # name column
                    $col_1 => filter_var($_POST['name'],FILTER_SANITIZE_STRING),
                      # email column
                    $col_2 => $_POST['email'],
                       # language column
                    $col_3 => $_POST['lang']   

    );

    ## let's add this_values using the method addRow(), and at the same time show confirmation on success or error on failure

    echo (($this_sheet->addRow($this_values) ? 'Data Successfully Added to your Google spreadsheets' : 'The attempted operation FAILED' ) ;

Test the script and make sure it is adding data flawlessly.. I hope I could find a better documentation for it, so that I don't have to write it. Anyways, I managed to analyze what the author of the spreadsheet class purpose.

Before making this script public, make sure comment these rows. commented as shown

    //echo 'Email: '. $_POST['email'] .'<br/>';
    //echo 'Name: '. $_POST['name'] .'<br/>';
    //echo 'Language: '. $_POST['lang'] .'<br/>';

, and this one also..

    //echo 'File Download: '. $file;

One final note, Google_Spreadsheet.php was last updated three years ago. If you encountered any problem using this, please let me know and I will fork it my github account then write an update if it is necessary.

How to change things in Google_Spreadsheet.php?

  1. I you want to change the default file name for new document, change this on line 35

    change Sheet1 to your own default file naming requirements

    private $worksheet = "Sheet1";

Update! I already forked the class, just in case modification is needed. The forked version is here, but I didn't do anything with it, until someone bring up any errors and the report the script's unable to do its job.

!Correction !

I just found a syntax error... change the above to this

    echo (($this_sheet->addRow($this_values)) ? 'Data Successfully Added to your Google spreadsheets' : 'The attempted operation FAILED' ) ;

My keyboard battery charged is getting low, so it is not catching up on me pretty well... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.