remove snippets

How to set a cookie, remove a cookie and get the value of a cookie in PHP

Tagged php, cookie, management, set, remove, get  Languages php

PHP cookie management is a good example of how incredibly badly designed PHP is:

Set a cookie that expires when browser is closed

Note that you most probably want to set the path, which we do here:

$value = 0;
$expires = 0;
setcookie('cookie_name', $value, $expires, '/');

Remove cookie

setcookie ('cookie_name', FALSE, time()-10000);

Get cookie value

$_COOKIE['cookie_name']

How to add and remove validation rules (jQuery validate plugin)

Tagged jquery, validation, plugin, remove, add  Languages javascript

This snippet shows how to change the jQuery validation plugin's validation rules dynamically:

$(document).ready(function() { 
  // Setup the default validation rules
  var validator = $('form').validate({ 
    rules: { 
      username: { 
        required: true, 
        minlength: 2
      }
    }
  };

  // Drop-down selection changes validation rules
  $('#account-type').change(function () {
    // Get the jQuery validation plugin's settings
    var settings = $('form').validate().settings;

    // Modify validation settings
    $.extend(true, settings, {
       rules: {
           // Add validation of year
           "year": {
               required: true,
               min: 2010,
               max: 2020,
               number: true
           }, 
           // Remove validation of username
           "username": {} 
       }
    });
  });
});

You can also use CSS classes to achieve the same:

$('#channel-title').addClass("{required:true,messages:{required:'required field'}}")
$('#channel-title').removeClass("{required:true,messages:{required:'required field'}}")