AngularJS: Prevent form submission or stop page refresh on submit

I have recently started learning AngularJS and came across form that would refresh every time I click the submit button. So, here are a couple of ways to prevent that from happening.

HTML Form

<div ng-app="my-app">
  <div ng-controller="myFormController">
    <form action="test_submit.php" method="post" accept-charset="utf-8" name="myTestForm" ng-submit="myTestForm.$valid && submit()" novalidate>
      <div>
        <label for="fname">First Name</label>
        <input type="text" ng-model="dataForm.fname" name="fname" id="fname" required>
      </div>

      <div>
        <label for="lname">Last Name</label>
        <input type="text" ng-model="dataForm.lname" name="lname" id="lname" required>
      </div>

      <div>
        <label for="email">Email</label>
        <input type="text" ng-model="dataForm.email" name="email" id="email" required>
      </div>

      <div>
        <button name="submit" ng-disabled="myTestForm.$invalid" type="submit">Submit</button>
      </div>
    </form>
  </div>
</div>

Angular JS

var myApp = angular.module('my-app', []);

myApp.controller('myFormController', function($scope, $http) {
  $scope.dataForm = {};

  $scope.submit = function() {
    // Ajax
  };
});

Continue reading “AngularJS: Prevent form submission or stop page refresh on submit”

Getting started development on android with eclipse

Android ImageThis post is just a quickie on how to get yourself started with Android development and the step-by-step guide about the basic tools needed to do so. Now there are many ways using which you can start development. But the simplest way is using Eclipse IDE. So, here’s a simple guide for a complete beginner.

For a complete beginner, it would be best to get eclipse and android sdk using this full-felged suite (eclipse, ADT, SDK Manager combined all together), in which case the first four steps of this tutorial can be skipped. Or download each tool separately, and continue with the steps given below.

Step 1. Download Eclipse
Although there are a lot of IDE’s that can be used to start development but eclipse is the most recommended to do so. You can download Eclipse using this link. Continue reading “Getting started development on android with eclipse”

Error handling in PHP with Error logger working

Error handling in PHP with Error logger working

Logging errors in php is easy, cool and helpful when it comes to test the site for errors as I explained it in my earlier post Logging errors to file in PHP but only till the site is at developer’s end and you working on it. Once you take the site live its just not good anymore. Once you take it on the production end, you cannot show the errors to the user or in case if its even 100% error free you will find that 1 in a 100 person who likes to messup with the sites. Being a programmer you gotta be ready for it. This is the place where comes error handling into play.

Here is a simple script. Just place these two functions in a file and include it at the right place. Probably including from the point onward from where you expect and error would be just perfect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Define a custom error handler
function userErrorHandler($errno, $errstr, $errfile = '', $errline = 0, $errcontext = array()) {
	// Getting error type
	$errorType = array (
			E_ERROR				=> 'ERROR',
			E_WARNING			=> 'WARNING',
			E_PARSE				=> 'PARSING ERROR',
			E_NOTICE			=> 'NOTICE',
			E_CORE_ERROR		=> 'CORE ERROR',
			E_CORE_WARNING		=> 'CORE WARNING',
			E_COMPILE_ERROR		=> 'COMPILE ERROR',
			E_COMPILE_WARNING	=> 'COMPILE WARNING',
			E_USER_ERROR		=> 'USER ERROR',
			E_USER_WARNING		=> 'USER WARNING',
			E_USER_NOTICE		=> 'USER NOTICE',
			E_STRICT			=> 'STRICT NOTICE',
			E_RECOVERABLE_ERROR	=> 'RECOVERABLE ERROR'
			);
 
	if (array_key_exists($errno, $errorType)) {
		$err = $errorType[$errno];
	} else {
		$err = 'CAUGHT EXCEPTION';
	}
 
	// Getting the error log file from php.ini
	$file 		= ini_get('error_log');
 
	// Creating the error log script, same as normal logger would do
	$error_string 	= "[" . date("d-M-Y H:i:s", $_SERVER['REQUEST_TIME']) . '] PHP ' . $err . '::' . $errstr . " in " . $_SERVER['SCRIPT_FILENAME'] . " on line " . $errline . "\r\n";
 
	// Logging error to a certain file
	error_log($error_string, 3, $file);
 
	// Check if the error code is not included in error_reporting
	if (!(error_reporting() &amp; $errno)) {
		return;
	}
 
	// Restore default handlers to prevent errors in errors
	restore_error_handler();
 
	if (function_exists('restore_exception_handler')) {
		restore_exception_handler();
	}
 
	// Load error page
	require('_errors/error.php');
	exit();
}
set_error_handler('userErrorHandler');
 
// Define a custom handler for uncaught exceptions
if (function_exists('set_exception_handler')) {
	function userExceptionHandler($exception) {
		// Restore default handlers to prevent errors in errors
		restore_error_handler();
		if (function_exists('restore_exception_handler')) {
			restore_exception_handler();
		}
 
		// Load error page
		require('error.php');
		exit();
	}
	set_exception_handler('userExceptionHandler');
}

Note: Once you start error handling in real time error logging stops. To overcome this a short code is included inside the function that uses PHP’s error_log function to log errors. So if you have a specifc error log file, just point it towards that.

Good luck!!!

Generating a simple jquery popup

Recently I needed to make a simple pop up box in wordpress and I searched for plugins to achieve what I wanted but failed. Well, if you look for it, you will surely find many plugins for jquery popup as well as wordpress popup but none helped me. In my lookout for the plugin I found this really cool and simple script that would do the required work for you with just simple tweaks.

Demo: Click me.

Continue reading “Generating a simple jquery popup”

Increase PHP Script Execution Time Limit

Every once in a while I need to process a HUGE file. Though PHP probably isn’t the most efficient way of processing the file, I’ll usually use PHP because it makes coding the processing script much faster. To prevent the script from timing out, I need to increase the execution time of the specific processing script. Here’s how I do it.

1
ini_set('max_execution_time', 60); //60 seconds = 1 minutes

Place this at the top of your PHP script and let your script loose!