Archive for category Beginners

Change text box value in uppercase using jQuery

We can bind this functionality with text box KeyUp event. So, when type anything this code will change the text box value in uppercase with each keystroke.

Code Snippet:-

<input type="text" id="variable" name="variable" />
<script type="text/javascript">
$(document).ready(function() {
	$('#variable').keyup(function() {
		$(this).val($(this).val().toUpperCase());
	});
});
</script>

, , ,

No Comments


jQuery get selected text from SELECT (or DROPDOWN) list box

Most of the time in JavaScript we want to do following things with Select (or dropdown) list box.

- Get the value of selected option
- Get the text of selected option
- Get the text of option using its value

We can use jQuery for all of the above tasks. jQuery provide very simple one line solution for all of them. Find below code snippet of all three one by one.

Code Snippet:

// Select list box
<select id="dropdown">
    <option value="0">Sunday</option>
    <option value="1">Monday</option>
    <option value="2">Tuesday</option>
    <option value="3" selected="selected">Wednesday</option>
    <option value="4">Thursday</option>
    <option value="5">Friday</option>
    <option value="6">Saturday</option>
</select>
 
// Get the value of selected option
var selectedvalue = $('#dropdown').val();
alert(selectedvalue); // 3
 
 
// Get the text of selected option
var selectedText = $('#dropdown option:selected').text();
alert(selectedText); // Wednesday
 
// Get the text of option using its option value (eg: 5)
var textThroughValue = $("#dropdown option[value='5']").text();
alert(textThroughValue); // Friday

, ,

No Comments


Extract/Import Object properties into Currect Symbol Table in PHP

If we want to import an associative array into current symbol table then we can use extract function. This function treats associative array keys as variable name and values as variable values.

But we can not use extract function directly if we want to import class object properties into current symbol table in similar way. So, for this first we need to get the properties of object as an associated array. We can use get_object_vars function for this purpose. get_object_vars function take object as parameter and return associative array of all non-static properties of passed object. For any unassigned property it will return NULL value.

Now we can pass this returned associative array into extract function to import objects non-static properties into current symbol table.

Code Snippet:-

<?php
// For associative array
 
$sampleArray = array('name' => 'John', 'age' => 25);
 
extract($sampleArray);
 
echo $name; // Output is "John"
echo $age; // Output is "25"
 
// For object
$sampleObject = new stdClass;
 
$sampleObject->name = 'Peter';
$sampleObject->age = 27;
 
$returnArray = get_object_vars($sampleObject);
 
extract($returnArray);
 
echo $name; // Output is "Peter"
echo $age; // Output is "27"
?>

,

No Comments


Generate Random Alphanumeric Password in PHP

This is a very simple and one of the way to generate random alphanumeric password in PHP.

Code Snippet:-

<?php
function generateCharacter() {
	$possible = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	$char = substr($possible, mt_rand(0, strlen($possible) - 1), 1);
	return $char;
}
 
function generatePassword($length) {
	static $password;
 
	if ($length) {
		$password .= generateCharacter();
 
		generatePassword(--$length);
	}
 
	return $password;
}
 
// Use of this function
 
echo generatePassword(10); // Qz1GUfkWFT
echo generatePassword(15); // GPB1hA8pVVWsgLZ
echo generatePassword(8);  // 20e3Bvec
?>

,

1 Comment