Form validation that contains arrays from checkboxes and radio buttons with jQuery

I’m using the jQuery plugin from http://bassistance.de/jquery-plugins/jquery-plugin-validation. The plugin is greate. I did run into a problem with arrays. Say your form has a group of checkboxes or radio buttons and their names are checkbox or radio. To have them be in the same group, their names have to be the same and to process it using PHP as post variables you have to throw them in arrays. So in the form their names will be checkbox[] or radio[].

In your validation you probably have something like

rules: {
  checkbox: { required: true },
  radio: { required: true }
}

The element name no longer matches because in your form they are checkbox[] and radio[] while in your javascript they are checkbox and radio. To solve this, use quotes and add the []. So here’s a quick example.


$(document).ready(function() {
  $('#form1').validate({
    rules: {
      "checkbox[]": { required: true },
      "radio[]: { required: true }
    }
  });
});


<form id="form1">
<input name="checkbox[]" type="checkbox" value="checkbox1" /> checkbox 1
<input name="checkbox[]" type="checkbox" value="checkbox2" /> checkbox 2

<input name="radio[]" type="radio" value="radio1" checked="checked" />radio  1
<input name="radio[]" type="radio" value="radio2" />radio  2
</form>

The radio button is an overkill since you normally have 1 checked by default unless you forget. The plugin is great so if you haven’t checked it out, I highly recommend it.

Artisteer 2 and Dreamweaver CS5

I’ve been trying out the demo for Artisteer 2 and it’s great. Creating templates for popular CMS softwares such as WordPress, Joomla, and Drupal have never been easier. I’ve done some playing around with manually creating a WordPress template and it’s not too bad. The WordPress documentation is fairly easy to follow. But doing it in Artisteer is a lot quicker. There’s 2 versions of the software, a home/academic edition that will create WordPress templates for $50 and a standard edition that will allow you to create templates for the other CMS (Joomla, Drupal, etc) for $130. So far I only need the home edition since I use WordPress but at work we also use Joomla. I don’t normally create the layouts at work so I’m hesitating to buy the home edition. I have sent them an email to see if there’s an upgrade path from home to standard and the cost. My work will not pay for the software and if they do, it will take such a long time to get since they only purchase things through purchase orders (sigh).

Now I’ve been curious about Dreamweaver CS5. I’m currently using CS3 with Espresso. I rarely use Dreamweaver’s auto coding – it’s too bulky. I like coding by hand. Espresso is great for that and it’s lightweight and fast. It just doesn’t have the design view tab like Dreamweaver does. Adobe also has a student/teacher version of the software for only $150. I’m downloading a trial version now and will check it out. I’m curious about the CMS code “sniffer” that they have.

Creating a wrapper page within WordPress 2.9

I’ve been searching for a way to create a wrapper page within WordPress without having to install a plugin. I browsed the WordPress files and found wp-blog-header.php in the root install directory. It’s really simple.

<?php
require_once(wp-blog-header.php');
get_header();

// this is the content layout based on the current theme you are using

get_sidebar();
get_footer();

?>

I’m not sure when this was implemented. All of the articles I found online mentions plugins that had to be installed or creating a new page within the WordPress admin GUI then include a file, etc. I just need to create a custom PHP file that will display the header, sidebar, and footer.

Depending on the theme you are using will determine additional code between each section (header, sidebar, footer). So check out the page.php or index.php of the theme you are using to see how it is structured.

Using PEAR Pager with PHP PDO

This post will be addressing 2 things.

  1. How to use PEAR’s Pager class with PHP PDO
  2. Addressing PDO bug #44639 http://bugs.php.net/bug.php?id=44639

Two queries will be used for this task. The first query, you will count the rows of the recordset. The second query is the same as the first but it will have the LIMIT – this is what will be used to display the data on each page.

Let’s say we have the following table.

    CREATE TABLE IF NOT EXISTS`blog`
    (
    `blog_id` int(11) not null auto_increment,
    `blog_text` varchar(4000) not null default ,
    PRIMARY KEY `pk_blog_id` (`blog_id`)
    ) ENGINE=INNODB;

Now we’ll query the database and get the row count using PDO.

    <?php

    require_once(file-with-pdo-db-connection.php');

    $sql = “SELECT * FROM `blog`”;
    $stmt = $dbh->prepare($sql);

    if ($stmt->execute())
    {
    // begin pager
    require_once(‘Pager/Pager.php');
    $params = array
    (
    totalItems' => $stmt->rowCount,
    ‘perPage' => 10,
    delta' => 5,
    ‘mode' => Sliding'
    );

    $pager =& Pager::factory($params);

    $links = $pager->getLinks();
    echo $links['all'];

    // offset setup
    list($from, $to) = $pager->getOffsetByPageId();
    $from = $from – 1;
    $perPage = $params['perPage'];

    // 2nd query based on 1st with LIMIT – this will be displaying data per page
    $stmt2 = $db->prepare($sql.' LIMIT :from, :perPage');

    // address bug 44639 – forces the variables to have the property of integer instead of string so no quotes will surround it
    $stmt2->bindValue(‘:from', $from, PDO::PARAM_INT);
    $stmt2->bindValue(:perPage', $perPage, PDO::PARAM_INT);
    $stmt2->execute();

    while ($row_blog = $stmt2->fetch())
    {
    echo ‘
'.$row_blog['blog_text'].'
'
; } } $dbh = null; ?>

If you don’t use the bindValue, you will get an error in your query. Your second query would look something like this….

SELECT * FROM `blog` LIMIT 0', '10'

…and the error would occur right after LIMIT. You cannot have quotes surrounding the numbers. It took a few hours of Googling but found the solution.

If you want to read more about PEAR Pager, visit http://pear.php.net/package/Pager/docs.

Here’s a tutorial I found that helped me out a lot – http://www.codediesel.com/php/simple-pagination-in-php/.

Updating PHP 5 with Godaddy and a Dedicated server

Jeff called me up asking for help with his dedicated server on Godaddy. He needed to update his PHP 5.1.6 to the latest stable version. His server is running Centos 5.2 (Final). He tried to call Godaddy for assistance and of course they were no help. I’m no expert myself but have played around with different systems. I found out that you can use the yum command to manage the installed packages. The problem is that the current repositories yum looks at does not contain new versions of PHP. This is why you get an error that says something like – cannot find PHP. What you need to do is add a repository that does have a newer version of PHP. We’re going to add Jason Litka‘s repository.

  • Log in to the Simple Control Panel
  • Log in using SSH
  • Type “su – ” to log in with root privileges
  • Type “nano -w /etc/yum.repos.d/utterramblings.repo”. If you’re not comfortable with nano, use whatever editor you’re more comfortable with.
  • In the utterramblings.repo file type the following [utterramblings] name=Jason’s Utter Ramblings Repo baseurl=http://www.jasonlitka.com/media/EL$releasever/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka
  • Save the file and close it
  • Now you can use “yum update php” and it should update it to the latest that the repositories have
  • Restart Apache

This tutorial is based on the one found at http://www.jasonlitka.com/yum-repository/. It took awhile for me to find the solution. I hope this helps out people who had the same problem as I did.

jQuery Form Variables

I have been messing around with jQuery for a couple of days now and I think I’m getting the hang of it. Before using jQuery, I was using Mootools for processing forms to divs. It worked as intended but what I couldn’t do was create new javascript within the loaded pages. As interfaces get more complicated, the code started to grow and was getting more difficult to follow. On top of that, I wanted to do more with Mootools than just load pages in divs. I tried to read documents and articles from their website but was hard to follow. I bought a book but to me it was still a bit confusing. I even posted a couple on the forums but didn’t get much help. So I started to look for another framework and found jQuery.

jQuery has many examples and demos on their website and has a large community of developers creating plugins. One of the things I needed to figure out was how to pass form variables using jQuery. I found a sample code on their website but I just couldn’t get it to work – http://docs.jquery.com/Ajax/jQuery.post. I couldn’t pass the form variable over. Their sample code was

$.post(page.php, {var1: var1", var2: “var2"}, function(data) { alert(data);});

So I started to play around with it more and was able to get it to work. Here’s the new code now based on the example.

$.post(page.php, {var1: $(‘#var1).val(), var2: $(‘#var2).val()},function(data){alert(data);});

After reading a few comments about this function, a couple suggested to use serialize() but I have yet to figure that out. At least my variables are passing as they should. The same syntax for the variables can be applied to $.get.

If you are going to copy to code above, make sure you are using apostrophes. For some reason, it’s displaying the “tick” symbol which is the one sharing the key with the tilde ~ on your keyboard. It looks like an apostrophe but more slanted. So be aware of that.

MSSQL Stored Procedures and Classic ASP

A client at work wanted us to create a way to duplicate existing records on their website with a click of a button. These records involve multiple tables in the database. Some of the tables contained a lot of columns. We tried to do most of work using ASP but sometimes the browser would time out and Dreamweaver would crash.

Then I thought, why don’t we have our SQL server do most of the heavy lifting. I created a stored procedure that query the database for the record(s) that need to be duplicated and have them duplicated. Below is a short example to get you started.

First you need to create a stored procedure in SQL Server.

CREATE PROCEDURE stored_procedure_sample
@record_id INT OUTPUT

AS
BEGIN

INSERT INTO table_name (column1, column2, column3)
SELECT column1, column2, column3
FROM table_name
WHERE record_id = @record_id

END;

GO

Next you need to create the ASP page that process the request. You can use form variable or URL query string. For the example, I will use a form variable. Let’s imagine on page1.asp there is a form with a hidden variable that contains the record_id you want to duplicate. You submit this to page2.asp and here’s a sample of what page2.asp should look like.

<%

Dim connString, rsCmd

connString = your connection string
Set rsCmd  = Server.CreateObject(ADODB.Command)
rsCmd.ActiveConnection = connString
rsCmd.CommandType = 4
rsCmd.CommandText = stored_procedure_sample

rsCmd.Parameters.Append rsCmd.CreateParameter("@record_id", 200, 1, 4, Request.Form("record_id"))

rsCmd.Execute

%>

What page2.asp does is it takes the form variable “record_id” from page1.asp and tells the stored procedure to get the values for this record and insert it as a new record. That’s pretty much it. Just a short, simple example on how you can create a stored procedure in MSSQL Server and use it with classic ASP.