PHP dynamic form elements

In a previous we explained how to use PHP to create a dynamic SELECT menu. The same way it’s possible to use PHP to create other form elements.

Create a radio group with PHP

Creating radio elements in some content management systems can result into writing a lot of code. Just in case your radio group with multiple options is getting a value from a database and/or a form. There need to be a check for every posted value for every option. This function will do all this work for you, just create two arrays, one for the values and labels and for the related html code. It’s up to the user if he uses 2 or more elements in one group. The function works with $_POST and $_GET data.

Continue reading PHP dynamic form elements

Create a select menu from files or directory

This simple function generates a select element for all files of a given directory. Use this function together with the PHP download script on this site. The function is modified to work with “register_globals = off “. Use this custom function in forms to update dynamic image names in a database table. We demonstrate the select menu on our upload page demo, just upload a new file and select the uploaded file from the select menu on the demo page. Continue reading Create a select menu from files or directory

PHP CAPTCHA, image validation

Use this script to protect your contact form, your whois query tool or just a form where some extra validation is needed. How does it work?
A session will be created inside a dynamic image file (requires GD library). The random value of this image appears inside the generated CAPTCHA image. The user has to enter this value into a form field. While processing the form, the entered value get checked against the session value. Without entering the correct string a form will not be processed. This mechanism is very useful to protect your form again (spam) bots. Continue reading PHP CAPTCHA, image validation

Prevent multiple form submission

Are you tired of people submitting the same information via your online forms multiple times and cluttering up your database and mail? If yes, this PHP snippet could be your solution. With this function (and native PHP session support) it’s possible to prevent a multiple form submission while using some server side form field validation. The script checks the unique string from the current submission against the post before and will stop if the submitted form or string is the same as before. Continue reading Prevent multiple form submission

Server side form field validation with PHP only

Almost for every submitted form you need a simple form check to test the required fields. Form field validation and error reporting becomes complicated if you have to handle more then three required fields. This function will do all this work: Validation of required fields and shows some user friendly errors. After submitting the form you get a message which fields are empty and the empty fields are in a different color.

The validation of your form is processed by PHP code only, it would be more user friendly if your web page will test the entries on the client side first!

PHP Snippet or function

$req_fields = array("_name"=>"name", "timestamp"=>"your time", "field"=>"some field", "check"=>"checkbox");
$msg = " ";

function check_empty_fields($method = "post") {
    global $req_fields, $msg;
    $use_method = ($method == "post") ? $_POST : $_GET;
    $errors = "";
    $count_empty = 0;
    foreach ($req_fields as $key => $val) {
        $field_name = (array_key_exists($key, $req_fields)) ?  $req_fields[$key] : $key;
        if (!array_key_exists($key, $use_method) || empty($use_method[$key])) {
            $_SESSION[$key] = true;
            $errors .= "|".$field_name;
        } else {
            $_SESSION[$key] = false;
    if ($count_empty == 0) {
        return true;
    } else {
        $msg = "The following (required) fields are empty:";
        $msg_parts = explode("|", ltrim($errors, "|"));
        $num_parts = count($msg_parts);
        $msg .= "<b>";
        for ($i = 0; $i < $num_parts; $i++) {
            $msg .= $msg_parts[$i];
            if ($i <= $num_parts - 2) {
                $msg .= ($i == $num_parts - 2) ? " & " : ", ";
        $msg .= "</b>\n";
        return false;

Example usage:

if (isset($_POST['submit'])) {
    if (check_empty_fields()) {
        // process the form data

If you like to color your form fields if the field is empty (session based), use the kind of code.

echo '<input type="text" name="fieldname"';
echo (isset($_POST['fieldname'])) ? ' value="'.$_POST['fieldname'].'"' : ' value=""';
if (!$_SESSION['fieldname']) echo ' class="invalid">';

You need to create a pseudo class with the name “invalid” in your CSS style sheet.
Try this working demo, where this server side validation script is used.