Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Friday, August 31, 2012

Index PHP Array to use in Javascript / Javascript array

So here is what I wanted, I wanted to the text of check box when clicked on check box like
<input type='checkbox' value='1' name='chkbox'>First chk box</input>

So I wanted 'First chk box' when clicked on it, in here you might be able to simply get it using innerHTML but my case was bit different so I got a php array that had id=>value (1=>'First chk box') array now the issue is you can't use it in javascript

So step 2 is converting php array to javascript array, following are the things you can do

1. Simply make a javascript array and put php values in it
var jsArray = new Array('<?=$phpArr[0]?>',<?=$phpArr[1]?>',....);

That doesn't work in my case because my indexes were different

2. You can have insertion in JS Array using the splice function to insert in mid, you can google that, but the issue with that is you can't have a javascript array like

arr[1] = index1
arr[2] = index2
arr[5] = index5

means you need EACH index defined which is not my case, I could have skipping index so here is solution for me, or you if you have similar issue... or any of above might work for you...

3. var jsArray = <?php echo json_encode($phpArray) ?>;
Now if could do is simply using
jsArray[chkbox.value] 
and it would give me 'First chk box'

Thursday, July 26, 2012

Include js in head tag Joomla 2.5

If you are looking for how to include javascript in the head tag of html without having to write it in the template for features like sharethis, Google analytics here is how to

Simply use the following two lines:

$document = &JFactory::getDocument();
$document->addCustomTag('<script type="text/javascript">my javascript</script>');

And it will add javascript to the head tag.

You can also include js/css files by using

$document->addStyleSheet('path');
$document->addScript('path');

Tuesday, July 3, 2012

Adding javascript file in Joomla view file

If you want to use a javascript file in a view file (i.e. myComponent/views/student/tmpl/default.php) you will not be able to inlcude using

<script type="text/javascript" src="" />

As you will see it won't load the file and hence won't work. So to include a file you will need to use something like this:


$document = &JFactory::getDocument();
$document->addScript( 'myfilepath/myjsfile.js' );

Thursday, March 1, 2012

Javascript Validation

I have been a webdeveloper since a while now however recently I came across an issue with a client's website I was working on, the issue was that I was having invalid submissions that were not possible considering that I had validation over the form submission like authenticating the correctness of URL. The question I finally asked was that was javascript validation enough? And to my surprise disabling (and hence) by passing javascript is fairly simple, once the user disables javascript then he can submit anything and bypass all your javascript checks so make sure you always have validation both at client side as well as server end.

Happy coding.