form to database php

I have written a collection of classes, that are capable of accepting a submission from a get (REST) or post (form) and output them to an email, the screen or to a database table.

The class can be used to output a CSV, an email or XML (REST).

The code is very straight forward for example create a form.html and add the following.

<form method="post" action="save.php">
  <label for="firstname">Firstname</label>
  <input type="text" name="firstname" id="firstname"><br>
  <label for="surname">Surname</label>
  <input type="text" name="surname" id="surname"><br>
  <input type="submit" value="Submit">
</form>

This form will submit to save.php, we need to now create this, create the file save.php and add the following

require_once('classes.php'); // load the classes

This loads the classes, and is required to make this work, next

        listener::getInstance();

Gets an instance of the listener, the listener has the responsibility of handling the data, it sets a number of values by default, one of which is the default submission type (GET) this needs to be changed to POST, so

        listener::getInstance()->method('POST');

Next, the listener needs to know which fields are valid fields, otherwise every bit of junk somebody sends to this script will be placed into the database, this is done in the form of an array, with the array key being the fieldname, and the value being whether or not is it required, if a field is defined as required and not codesent, the submission will fail with an error.

array(
    'firstname'=>true
    'surname'=>true
)

tell the listener about these fields

        listener::getInstance()
                ->method('POST')
                ->setFields(
                        array(
                            'firstname'=>true
                            'surname'=>true
                            )
                        );

next we have to define, what we wish to do with the submission, in this case, save it to a database, first get the database instance.

database::getInstance();

by default, the database will default to certain values, typically it will connect with the username “root”, password “”, database “data”, host “localhost”, table called “submissions” and the field “submitted”.
if you need to change any of these values, it can be done by setting a config of type stdConfig with an array of new parameters.
If you do not decalre say a username, it just defaults, the avaialble fields are: username, password, database, table, field and host

database::getInstance()->setConfig(
                        database::stdConfig(
                                array(
                                    'password'=>'1234'
                                    )
                                )
);

The database instance is placed in setAction like this:

        listener::getInstance()
                ->method('POST')
                ->setFields(
                        array(
                            'firstname'=>true
                            'surname'=>true
                            )
                        )
                ->setAction(
                    database::getInstance()->setConfig(
                        database::stdConfig(
                                array(
                                    'password'=>'1234'
                                    )
                                )
                    )
                );

Next we need some sort of feedback, about whether or not it was successful, this can be done by asking the instance what happened, for example:

// some visual feedback
if (listener::getInstance()->success){
     echo 'Thankyou!';
} else {
     echo 'The following problems were encountered';
     print_r(listener::getInstance()->errors);
}

Lets put it all together!, complete code below:

 
        require_once('classes.php'); // load the classes
 
        listener::getInstance()
                ->method('POST')
                ->setFields(
                        array(
                            'firstname'=>true
                            'surname'=>true
                            )
                        )
                ->setAction(
                    database::getInstance()->setConfig(
                        database::stdConfig(
                                array(
                                    'password'=>'1234'
                                    )
                                )
                    )
                );
 
// some visual feedback
if (listener::getInstance()->success){
     echo 'Thankyou!';
} else {
     echo 'The following problems were encountered';
     print_r(listener::getInstance()->errors);
}

You will need the class library found in here (right click save as)

Should you require a REST service, a wrapper is included to output the response as xml, for example:

<?php
require_once('classes.php'); // load the classes
 
Output::toXml(
        listener::getInstance()
                ->setFields(
                        array(
                            'bob'=>true
                            )
                        )
                ->setAction(
                    database::getInstance()->setConfig(
                        database::stdConfig(
                                array(
                                    'password'=>'1234'
                                    )
                                )
                    )
                )
            );
 
//foreach(listener::getInstance()->errors AS $err) echo $err;

to get the submissions as a Csv, similiarly:

<?php
require_once('classes.php'); // load the classes
 
Output::toCsv(
        database::getInstance()->setConfig(
            database::stdConfig(array('password'=>'1234'))
            )->fetchAll()
        );
 
//echo database::getInstance()->error;

A rest Example can be found here, along with the ability to CSV export

VN:F [1.9.9_1125]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.9_1125]
Rating: 0 (from 0 votes)

7,501 views

This entry was posted on Monday, October 15th, 2012 at 11:27 am and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed.