Greyhead.net

Home
Adding a barcode to a form e-mail PDF Print E-mail
Written by Bob Janes   
Friday, 05 November 2010 14:03

Sometimes it's important to add a unique identifier to the form response, for example travel or event tickets. In this recipe we will look at generating a "random" identifier and adding it to the form e-mail as a scannable barcode.

This article is from Chapter 7 “Adding features to your form” of The ChronoForms Book and is included here with permission from Packt Publishing.

Note: this recipe was written for ChronoForms v3. See this forum post for short notes on using it with ChronoForms v4.

Getting ready

We're going to need a simple form. Our newsletter form will be perfect although we'll be adding to the code in the Form HTML box.

We'll need a simple function to create the "random identifier" which we will see shortly.

Lastly we'll need code to generate a barcode. Rather than taking time reinventing this particular wheel, we're going to use a PHP program created by Charles J Scheffold and made available for use or download from http://www.sid6581.net/cs/php-scripts/barcode/.

How to do it...

  1. First, grab a copy of the barcode.php file from sid6581.net.
  2. We'll need to make this file accessible to our form. So let's create a new folder inside the ChronoForms front-end folder.

    You'll probably need to use an FTP client to do this, or install the "exTplorer" Joomla! extension which will allow you to create folders from within the Joomla! Site Admin interface.

    • Browse to [root]/components/com_chronocontact and create a new includes folder
    • Copy the standard Joomla! index.html file from the com_chronocontact folder into the new folder
    • Upload the barcode.php file into the new includes folder
  3. Now, we are going to add the function to create a "random" identifier to the Form HTML. This is a small function that creates an alphanumeric string when it is called.

    <?php
    if ( !$mainframe->isSite() ) { return; }
    /*
    function to generate a random alpha-numeric code
    using a specified pattern
    *
    * @param $pattern string
    * @return string
    */
    function generateIdent($pattern='AA9999A')
    {
    $alpha = array("A","B","C","D","E","F","G","H",
    "J","K","L","M","N","P","Q","R","S","T","U","V","W",
    "X","Y","Z");
    $digit = array("1","2","3","4","5","6","7","8","9");
    $return = "";
    $pattern_array = str_split($pattern, 1);
    foreach ( $pattern_array as $v ) {
    if ( is_numeric($v) ) {
    $return .= $digit[array_rand($digit)];
    } elseif ( in_array(strtoupper($v), $alpha) ) {
    $return .= $alpha[array_rand($alpha)];
    } else {
    $return .= " ";
    }
    }
    return $return;
    }
    ?>

    We call this function using generateIdent() or generateIdent('pattern') where the pattern is a string of As and 9s that defines the shape of the ident we want. The default is AA9999A, giving idents like KX4629G. This will be perfectly fine for our example here.

    We also want to add the ident into the form and we'll use a hidden field to do that, but to make it visible we'll also display the value.

    <?php
    $ident = generateIdent();
    echo "<div>Ident is $ident</div>";
    ?>

    <input type='hidden' name='ident' id='ident'
    value='<?php echo $ident; ?>' />

    In day to day use we probably wouldn't generate the ident until after the form is submitted. There is often no useful value in displaying it on the form and essentially the same code will work in the OnSubmit boxes. However, here it makes the process clearer to generate it in the form HTML.

  4. We can add both these code snippets to our form just before the submit button element. Then apply or save the form and view it in your browser.

    The layout may not be very elegant but the Ident is there. Refresh the browser a few times to be sure that it is different each time.

    It's simpler and tempting to use serial numbers to identify records. If you are saving data in a table then these are generated for you as record IDs. It does create some problems though; in particular, it can make it very easy to guess what other IDs are valid and if, as we often do, we include the ID in a URL it may well be possible to guess what other URLs will be valid. Using a random string like this makes that kind of security breach more difficult and less likely.

  5. We said though that we'd generate a barcode, so let's develop this form one more step and show the barcode in the form.

    If you look at the code in barcode.php, it shows a list of parameters and says what we can use. For example:

    <img src="barcode.php?barcode=123456&width=320&height=200">

  6. We need to modify this a little to link to the new folder for the file and to add our generated ident value:

    <img src="/components/com_chronocontact/includes/barcode.
    php?barcode=<? php echo $ident;?>&width=320&height=8">

    This code can go in place of the "echo" line we used to display the ident value:

    <?php
    $ident = generateIdent();
    echo "<img src='".JURI::base()
    ."components/com_chronocontact/includes/barcode.php?barcode="
    .$ident."&width=320&height=80' />";
    ?>

  7. Apply or save the form and view it in your browser.

    There we have it—a bar code in our form showing the random ident that we have created.

    If you don't see any graphic and the code appears to be correct then you may not have the PHP GD graphics library installed. Check on the AntiSpam tab for any of your forms and you will see a GD Info box. The GD library is now included in the vast majority of PHP installations. If you don't have it then check with your ISP to see if the library can be enabled.

  8. Now that's actually not of much use except to show that it works, you can't scan a bar code off the screen. Where we want it is in our Email template.

    The code to add to the template is:

    <div>Your code: {ident}</div>
    <img src="<?php echo JURI::base().'components/com_chronocontact/
    includes/'; ?>barcode.php?barcode={ident}&width=280&height=100"
    />

  9. As this includes some PHP, we can't add it using the Rich Text Editor. First we need to go to the Email Setup | Properties box and set Use Template Editor to No, apply the Properties, then apply the form and go to the Email Template tab.

    To avoid an "oddity" in the current release of ChronoForms it may be necessary to comment out the generateIdent() function code block in the Form HTML, while you create an Email Setup. Just put /* & */ before and after the block if you get a blank page or see a PHP Error message about re-declaring the function.

  10. Now click on the Email Template tab and paste the code at the end of the textarea.
  11. Submit the form to test.

    We now have a printable e-mail complete with a barcode showing our random ident.

How it works...

In this recipe we did a couple of things. We added some more complex PHP to the Form HTML that we had before and we imported a PHP script found on the internet and successfully used that in combination with ChronoForms.

There are many hundreds of useful scripts available for almost any conceivable function. Not all are of good quality and not all will work in this way but, with a little work, a surprising number will function perfectly well.

There's more...

We said earlier that it might be better to generate the ident after the form is submitted. Here's the code to use in the OnSubmit Before code box to get the same result in the e-mail:

<?php
if ( ! $mainframe->isSite() ) { return; }
JRequest::setVar('ident', generateIdent());

/*
function to generate a random alpha-numeric code
using a specified pattern
*
* @param $pattern string
* @return string
*/
function generateIdent($pattern='AA9999A')
{
$alpha = array("A","B","C","D","E","F","G","H",
"J","K","L","M","N","P","Q","R","S","T","U","V","W",
"X","Y","Z");
$digit = array("1","2","3","4","5","6","7","8","9");
$return = "";
$pattern_array = str_split($pattern, 1);
foreach ( $pattern_array as $v ) {
if ( is_numeric($v) ) {
$return .= $digit[array_rand($digit)];
} elseif ( in_array(strtoupper($v), $alpha) ) {
$return .= $alpha[array_rand($alpha)];
} else {
$return .= " ";
}
}
return $return;
}
?>

If you use this, then you can remove all of the additional code from the Form HTML box leaving just the basic HTML generated by the Form Wizard. The Email template code remains as we created it previously.


There are different styles of laying out both JavaScript and PHP and sometimes fierce debates about where line breaks and spaces should go. We've adopted a style here that is hopefully fairly clear, reasonably compact, and more or less the same for both JavaScript and PHP. If it's not the style you are accustomed to, then we're sorry.

Last Updated on Thursday, 05 April 2012 09:51
 
the best hostingpsd to html servicebuy vpn