Archive

Archive for November, 2011

jquery focus after blur

November 21, 2011 Leave a comment

Need to focus() on the input field after the user has clicked or tabbed off the input field?

See below:
http://forum.jquery.com/topic/jquery-focus-after-blur

You might also want to add a .select() to re-select the input field causing the problem (if it’s a text field)

 

Categories: jquery

jquery 1.2 and ajax parameter serialization – passing a php array

November 15, 2011 Leave a comment

php It just sucks when I miss the fine print in the documentation. Recently I’ve been working on a project that passes product parameters to a jquery $.ajax call. The parameters are variable, so I wanted to pass them back to the server in a PHP style array:

/process.php?ProductOption[color]=123&ProductOption[size]=455&ProductOption[edition]=711

This seems to work with jquery 1.3 and greater (http://api.jquery.com/jQuery.param/) but the system I’m on requires 1.2 for a few other dependencies. So the workaround I used for the problem of passing php style array query string components was as follows:


//static variables to pass to ajax call
var ajaxData = {
  'ProductType' : $('#Type').val(),
};

//variable product options to pass back, all fields
//are wrapped in a <div id="ProductOptions"> for easy selection
$('#ProductOptions :input').each(function(){
  if($(this).val().length) {
    //the input field names are in PHP array style too: i.e.
     //<input type="text" name="ProductOption[color]" value="444" />
     var name = $(this).attr('name').replace(/.+\[([^\]]+)\]/, "$1");
     ajaxData['ProductOption[' + name + ']'] = $(this).val();
  }
});

$.ajax({
   async: false,
   url: '/admin/ctl_Process.php',
   data: ajaxData,
   success: function(data) {
     //do something here...
   }
});

Enjoy!

Categories: php

php date add – date math compounded

November 15, 2011 Leave a comment

php
This will probably be an elementary post for most, but recently, I was working on a problem that required determining the expiration of a membership and to write some rules about auto-renewing that membership 10 days prior to expiration. Instead of writing any kind of painful date math, or having to rely on mysql for dateadd functions, strtotime accomplished what I needed:


$now = time();
//expire the membership in a year
$MembershipExpiration = date('Y-m-d', strtotime('+1 year', $now));
//renew the membership 10 days prior to expiration:
$MembershipRenewal = date('Y-m-d', strtotime('+1 year -10 day', $now));

I just threw in the “-10 day” to see what would happen, and those smart cookies who built php already have it all under control.

Categories: mysql, php