Posted: October 23rd, 2008 | Author: Mihai Bojin | Filed under: PHP | Tags: code, coding, PHP, php5, zend, zend framework, zf | No Comments »
I’ve been busy lately, lot of work to do, so little time… I wasn’t able to write as much as I would like on the blog, but I’m trying to.
In the last couple of days I’ve been reading PHP Architect’s Guide to programming with Zend Framework.

It’s a moderately good book written by Cal Evans for those of us that are beginning their journey into the Zend Framework.
It is very light, only 200 pages and it sheds a light into the ins and outs of the Zend Framework. Unfortunately it is more or less useless for the seasoned coder that knows his way into PHP.
The author presents some simple things that you probably can find out yourself from the Zend Framework Manual or the Zend Quickstart Application, given you have some time to research it.
I’d recommend this book if you have to learn how to use ZF fast and you don’t have time to dig into the Zend Documentation (which is in my opinion excellent). You won’t be able to use this book as a reference later on and it will only serve as bookshelf decoration.
Also I feel that the book stresses the Web Services topic a little too much.
The next book I want to read is called Zend Framework in Action and I hope will be more complex and advanced. It’s not out yet but I came upon a prerelease so I’ll see how it is.
Do you know any other good ZF books ?
Share them in the comments ! Thanks !
Posted: September 20th, 2008 | Author: Mihai Bojin | Filed under: PHP | Tags: form, heredoc, PHP, simple | No Comments »
I was just thinking what new tutorial to write about PHP when I came upon this post on talkfreelance.
A user needed help, I needed to write something so why not combine the two needs and create something elegant and useful.
You can download the full project files here.
Basically the problem is this:
We have this PHP file:
codefile.php
<?php
$value1='content1';
$value2='content2';
?>
We need to be able to edit the two variables from within a form without using any databases.
The code to do this is very simple and you can see it below. Hope it helps !
index.php
<?php
// name of file that you want to save the content in
$fileName = 'codefile.php';
// Load code from filename
$code = file_get_contents($fileName);
// Extract PHP tags with str_replace
$array_strip_tags = array('');
$array_strip_with = array('', '', '');
$code = str_replace( $array_strip_tags, $array_strip_with, $code );
// Execute code from file and import $value1 and $value2 into the GLOBAL namespace
eval( $code );
// Load values sent from the form (and check if form was submitted)
$submitted = isset($_POST['submitted'])?$_POST['submitted']:false;
$strValue1 = isset($_POST['submitted'])?$_POST['value1']:$value1;
$strValue2 = isset($_POST['submitted'])?$_POST['value2']:$value2;
// If the form was submitted save the file with the new data
if ($submitted) {
// Create string in heredoc syntax
$strToSave = <<<RTT;
<?php
\$value1='$strValue1';
\$value2='$strValue2';
?>
RTT;
// Save string to file
$return = @file_put_contents($fileName, $strToSave);
// Set success or error message
if ($return) {
$errorMessage = 'File was saved sucesfully';
} else {
$errorMessage = 'There was an error saving the file! Check file permissions.';
}
}
?>
<form method="post" action="<?=$PHP_SELF?>">
<fieldset>
<legend>Change values from PHP - form</legend>
< ?php if (isset($errorMessage) && strlen($errorMessage) > 0): ?><span>< ?=$errorMessage?></span>
< ?php endif; ?>
<input type="hidden" value="1" name="submitted" />
<label for="value1">Value 1:</label><input type="text" value="<?=$strValue1?/>" name="value1" /><br />
<label for="value2">Value 2:</label><input type="text" value="<?=$strValue2?/>" name="value2" /><br />
<input type="submit" value="Submit" />
</fieldset>
</form>
As usual, code comments are included and will help you better understand what every part of it does.
You can download the full project files here.
If you have any other solutions to this problem feel free to post it in the comments.
Posted: September 15th, 2008 | Author: Mihai Bojin | Filed under: PHP, Web | Tags: form, form generator, PHP, simple, SimpleForm | No Comments »
I’ve been meaning to do something like this for a long time.
I think I can safely say that forms are a web coder’s worst nightmare. Everytime you think you covered all the aspects of a project you realize that you forgot something about the website’s forms.
You can’t really standardize forms because they come in a lot of shapes and colors. The best implementation I know of is Zend_Form, but it implies that you use the whole Zend Framework and that brings a lot of overhead to the table.
If you have a small website, or you are in need of a simple lightweight form system, stay tuned !
Another reason for developing this generator is a problem faced by a good friend of mine, who’s a marketer and PPC specialist.
He told me how he searched the web for a simple to use, form tutorial for a non-programmer like himself. He wasn’t able to find one but he wanted to do it himself, so he struggled for a week and created… something. Something that works but that won’t help him at his next project.
I guess there are a lot of guys out there in the same position, so I’m starting this series for the web professionals and passionates that need a simple to use Form Generator. Later on in the series I will address the seasoned web coder that maybe wants to help develop this FormGenerator into a complete form management solution.
I’ve created the first simple version of the FormGenerator and as you might have guessed it’s called SimpleForm.
It’s small, lightweight, secure and it can send the form results after completion to one or more email addresses or your liking.
All you have to do to make it work in your scripts is to create a configuration file like the one below. In it you’ll find all the features of the form explained. You can also download the full project files from www.MihaiBojin.com/wp-content/uploads/SimpleForm/SimpleForm-v1.00.zip
SimpleForm.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
| < ?php
require_once('SimpleForm.php');
/**
* Initialize SimpleForm with parameters
* Syntax: SimpleForm($formName, $method)
* $formName - You only have to fill this in if you have more than 1 form on the page
* $method - Form's method: default is 'post', you can also use 'get', but it's not recommended
*/
$form = new SimpleForm('mihai', 'post');
/**
* Adds an element to the form
* Syntax: addElement($label, $type, $required, $errorMessage)
* $label - Label that represents the element
* $type - Type of element; right now only input-text, input-password, textarea and input-submit
* are implemented
* $required - Determines if field completion is required; if true you will have to set $errorMessage
* $errorMessage - Error message to show in case field is not filled in
*/
$form->addElement('Name', 'input-text', true, 'Please complete the name field!');
$form->addElement('Email', 'input-text', true, 'Please complete the email field!');
$form->addElement('Subject', 'textarea');
$form->addElement('Send', 'input-submit');
/**
* If you want to show a success message after the form is shown, set it through this method
* Syntax: addSuccessMessage($message)
* $message - Message that you want shown after the form is completed
*/
$form->addSuccessMessage('Congratulations! You will be contacted as soon as possible!');
/**
* Runs the form generator
* At first it will just show the form
* If the users submits the form with the required fields uncompleted, it will reshow the form plus the error message(s)
* If everything is okay, it will just show the success message (if filled in) or nothing
* Syntax: runForm($returnValues)
* $returnValues - default is false; if set to true you'll have to capture the output from the return value
* like $output = $form->runForm(true); and echo it yourself
*/
$form->runForm(false);
/**
* If you need to send an email (or emails) with the form's content, do it with this method
* Syntax: mailTo($to, $from, $subject)
* $to - can be a form element(for example 'Email') in which case it will use the value entered by the user
* or a plain email address
* $from - can be a form element(for example 'Email') in which case it will use the value entered by the user
* or a plain email address
* $subject - Email's subject message
*/
$form->mailTo('office@example.com', 'Email', 'Contact form email' ); |
The example configuration file sets up a simple contact form with Name, Email and Address fields.
To show the form in a web page, just include this init file (SimpleForm.init.php) in your webpage. For example I added an index.php that shows how to include the form generator:
index.php
1
2
3
4
5
6
| < ?php
/**
* Simple way of including the form in your website
*/
require_once( 'SimpleForm.init.php' );
?> |
As you can see, you just have to call the require_once feature of PHP and the generator will do it’s magic.
Some implementation details:
- the generator is implemented in PHP 5 it will not work on any subversion of PHP 4
- if you don’t use PHP on your website for anything else, you’ll have to rename the target filename from "filename.html" to "filename.php" and just include the php tags from the index.php example above
- I tested the script and it seems to work but that may not be the case on other servers, versions of PHP etc; if you run into any problems, drop me a comment or an email and I will help you sort things out
Have fun with it and please share your experience with SimpleForm version 1.00 !