Azharkamar.com

JavaScript

17 Mar 12

Automatic Redirect to Mobile Website using JavaScript

Developing a Mobile site? Want users to be automatically redirected when they access it from a mobile device? Well I think I’ve got a nice piece of JavaScript that will do just that. Read more

24 Nov 11

Magento – Modifying the Default Message Alert Box to look like a Popup Dialog

Magento developers out there, here’s a simple way to replace the default error/success notification box (the message that appears after you submit forms) to one that looks like a modal dialog popup using some simple JavaScript and CSS. Read more

6 Jan 11

Hide DIV/SPAN Element Automatically After a Few Seconds Using jQuery

One of the many ways to get 100% attention from your website visitors is to interact with them using popups. Nope, not the 90s, ugly, annoying popup but the neat jQuery modal one. And sometimes you wanna automatically fade out that DIV / SPAN popup element in effort to improve user experience, so here’s something you might find useful.
Read more

3 Nov 10

Solving jQuery Conflicts with other JavaScript Libraries

I was developing a page that for some reason had to use 3 different JavaScript libraries (jQuery, MooTools and Highslide) and got into so much problems. Largely because the different libraries were conflicting with each other. Read more

22 Mar 10

Auto Update the Copyright Year in a Website using PHP or JavaScript

If you have been creating websites for years (or more than a year) I am pretty sure you have encountered the ‘copyright year’ issue. Read more

12 Mar 10

Fabrik Date Input – Get and Set the Week Number using PHP and JavaScript

This piece of short and sweet code is quite useful when you need to get the week number of a certain date for special records, joins or calculations. I use it for one of my projects involving an Inventory Management System that is based only on weekends, so most of the orders and transactions need to be recorded and filtered by week numbers.

How to get the Week Number from a Date Input

1. Create an element – ‘Element Type: Date‘.

2. Under the ‘Table date format’ and ‘Form date format’ fields, key in your desired format and include %W anywhere in the format string. The %W will return the week number for the date that you choose. For me, I do it like this: %d-%m-%Y, Week %W which will return a date like so: 01-01-2010, Week 01.

3. If you want to play around with more PHP date functions, go to php.net/strftime.

Now that was very simple and I’m pretty sure most of you already know it. But how do you extract the Week number from that string and save it to a database table? Or use it for calculations? As a table filter maybe? Ok I’ll show you how.

How to Get the Week Number and Set it to an Input Field

Here’s the code:
var orderForm = oPackage.blocks.get('form_17').formElements;
var orderDate = orderForm.get('order_information___order_date').getValue();
var orderWeek = orderDate.split('k ');
orderForm.get('order_information___order_week').update(orderWeek[1]);

Place this code in the JavaScript tab of your Date element and set the action: blur (onBlur).

So what in the world does this piece of code above do?
var orderForm = oPackage.blocks.get('form_17').formElements;[adsense1]
This is the standard variable assignment for getting the form elements and data. Replace ‘form_17′ with your form ID. View the source code of your web page to find your Form ID or get it from the Fabrik component Forms page in the Joomla! backend.

var orderDate = orderForm.get('order_information___order_date').getValue();
This one simply assigns the value of the date input field to ‘orderDate’. Change ‘order_information___order_date’ to the name of your date input field.

var orderWeek = orderDate.split('k ');
Split? Split what? Ok, this JavaScript split() method is used to split a string into an array of substrings. orderDate.split(‘k ‘) means that the orderDate string will be split into two at point ‘k ‘ and thrown into the orderWeek array. Ok you guess, what would orderWeek[0] return you? Yup, whatever string at and before ‘k ‘.

orderForm.get('order_information___order_week').update(orderWeek[1]);
So this brings us to the final line that updates the value of another input field in your form to orderWeek[1], which is…… the Week number!

So there you have it – How to display a week number using PHP and then send its’ value to another field using JavaScript. Try it, it’s fun.