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”

Gauss Elimination method in C language using Lower triangular matrix

This is a simple C language program that calculates solution of n-linear equations using Non-pivotal Gauss Elimination method. It uses lower triangular matrix to do so, for upper triangular matrix visit here.

#include<stdio.h>
#include<conio.h>
 
float matrix[10][10], m, temp[10];
int i, j, k, n;
 
void lower_traingularisation() {
    for (i=n-1; i>0; i--)
        for (j=i-1; j>=0; j--) {
            m	= matrix[j][i]/matrix[i][i];
            for (k=0; k<n+1; k++) {
                matrix[j][k]	= matrix[j][k]-(m*matrix[i][k]);
            }
        }
} //lower_traingularisation
 
void back_subsitution() {
    for (i=0; i<n; i++) {
        m	= matrix[i][n];
        for (j=0; j<i; j++)
            m	= m - temp[j] * matrix[i][j];
        temp[i]	= m/matrix[i][i];
        printf("\n x%d => %f", i+1, temp[i]);
    }
} // back_subsitution
 
void main() {
    printf("Enter number. of variables :: ");
    scanf("%d", &n);
 
    printf("Enter the augmented matrix: \n");
    for (i=0; i<n; i++)
        for (j=0; j<n+1; j++)
            scanf("%f", &matrix[i][j]);
 
    lower_traingularisation();
 
    printf("The lower traingular matrix is : \n");
 
    for (i=0; i<n; i++) {
        for (j=0; j<n+1; j++)
            printf("%f \t", matrix[i][j]);
        printf("\n");
    }
 
    printf("The required result is : \n");
 
    back_subsitution();
 
    getch();
} // main

Gauss Elimination method for solving n-linear equations in C language

This is a simple C language program that calculates solution of n-linear equations using Non-pivotal Gauss Elimination method. It uses upper triangular matrix to do so, for solution using lower triangular matrix visit here.

#include<stdio.h>
#include<conio.h>
 
float matrix[10][10], m, temp[10];
int i, j, k, n;
 
void upper_triangularization() {
	for (i=0; i<n-1; i++)
		for (j=i+1; j<n; j++) {
			m	= matrix[j][i]/matrix[i][i];
			for (k=0; k<n+1; k++) {
				matrix[j][k]	= matrix[j][k]-(m*matrix[i][k]);
			}
		}
} //upper_traingulisation
 
void back_subsitution() {
	for (i=n-1; i>=0; i--) {
		m	= matrix[i][n];
		for (j=n-1; j>i; j--)
			m	= m - temp[n-j] * matrix[i][j];
		temp[n-i]	= m/matrix[i][i];
		printf("\n x%d => %f", i+1, temp[n-i]);
	}
} // back_subsitution
 
void main() {
	printf("Enter number. of variables :: ");
	scanf("%d", &n);
 
	printf("Enter the augmented matrix: \n");
	for (i=0; i<n; i++)
		for (j=0; j<n+1; j++)
			scanf("%f", &matrix[i][j]);
 
	upper_triangularization();
 
	printf("The upper traingular matrix is : \n");
 
	for (i=0; i<n; i++) {
		for (j=0; j<n+1; j++)
			printf("%f \t", matrix[i][j]);
		printf("\n");
	}
 
	printf("The required result is : \n");
 
	back_subsitution();
 
	getch();
} // main

Insertion And Deletion Operation Over Multiple Queue In C language

A simple C language program to implement multiple queues in a single dimension array.
This is a revised version of Insertion And Deletion Operation Over Multiple Queue | multiple queue in data structure.

#include
#include
# define max 20

int insq (int queue[max], int qno, int rear[], int limit[], int *data) {
	if (rear[qno] == limit[qno])
		return(-1);
	else {
		rear[qno]++; //... rear[qno] = rear[qno] + 1;
		queue[ rear[qno] ] = *data;
		return(1);
	} // else
} // insq

int delq (int queue[max], int qno, int front[], int rear[], int *data) {
	if( front[qno] == rear[qno] )
		return(-1);
	else {
		front[qno]++; //... front[qno] = front[qno] + 1;
		*data = queue[ front[qno] ];
		return(1);
	} // else
} // delq

int getQueueNumber(int n) {
	int qNo=0;
	Inva:
	printf("\n Enter a Logical Queue Number (1 to %d) : ", n);
	scanf("%d", &qNo);
	if (qNo<1 || qNo >n) {
		printf(" Invalid Queue Number. Please try again.\n");
		goto Inva;
	}
	return qNo;
}

void main() {
	int queue[max],  data;
	int bott[10], limit[10], f[10], r[10];
	int i, n, qno, size, option, reply;

	printf("\n C Language program to implement the Multiple Queues \n");
	printf("\n How Many Queues ? : ");
	scanf("%d", &n);
	size = max / n; //... Get Max. size for each Queue

	//... Initialize bottom for each Queue

	bott[0] = -1; //... Bottom of first Queue is -1
	for(i = 1; i < n; i++)
		bott[i] = bott[i-1] + size;

	//... Initialize Limit of each Queue

	for(i = 0; i < n; i++) //... Limit of i'th Queue is equal to bottom of i'th Queue + Size
		limit[i] = bott[i] + size;

	//... Initialize Front & Rear of each Queue
	//... Initial value of Front & Rear of each Queue is same as its Bottom Value

	for(i = 0; i < n; i++)
		f[i] = r[i] = bott[i];

	//... Process the Queues

	do {
		printf("\n\n C Language program to implement the Multiple Queues \n");
		printf("\n 1. Insert in a Queue");
		printf("\n 2. Delete from a Queue");
		printf("\n 3. Print from a Queue");
		printf("\n 3. Exit \n");
		printf("\n Select proper option ( 1 / 2 / 3 / 4) : ");
		scanf("%d", &option);
		switch(option) {
			case 1 : //... Insert
				qno	= getQueueNumber(n);
				printf("\n Enter Data : ");
				scanf("%d", &data);
				reply = insq(queue, qno-1, r, limit, &data);
				if( reply == -1)
					printf("\n Queue %d is Full \n", qno);
				else
					printf("\n %d is inserted in a Queue No. %d \n", data, qno);
				break;
			case 2 : //... Delete
				qno	= getQueueNumber(n);
				reply = delq(queue, qno-1, f, r, &data);
				if( reply == -1)
					printf("\n Queue %d is Empty \n", qno);
				else
					printf("\n %d is deleted from Queue No. %d \n", data, qno);
				break;
			case 3:
				qno	= getQueueNumber(n);
				printf("\n Elements of Queue %d are as : ", qno);
				if (f[qno-1]==r[qno-1]) {
					printf("\n Queue is empty");
					break;
				}
				for (i=f[qno-1]+1; i<=r[qno-1]; i++)
					printf("%d\t", queue[i]);
				printf("\n");
				break;
			case 4 :
				break;
			default:
				printf("\n Invalid input. Please try again.");
		} // switch
	}while(option!=4);
} // main

Php file_exists, does it really exists?

“Php functions are sometimes so confusing”, I don’t know how true that is, but I just found it to be true in this very case. Php’s file_exists, a function that seems like it is made to check if a file exists or not, is the one I encountered recently.

So I created this little test for this very purpose. I created an images folder with 2 files and 1 folder. Next I used file_exists function in 3 conditions with respective relative paths, to see which of those existed.

$file1="images/two.jpeg";
$file2="images/icon/one.jpg";
$file3="images/small/icon/";

if (file_exists($file1))
    echo "File one exists";

if (file_exists($file2))
    echo "File two exists";

if (file_exists($file3))
    echo "File three exists";

To my surprise all the conditions returned true.

File one exists
File two exists
File three exists

file_exists — Checks whether a file or directory exists.

According to php.net manual:

Irrespective of existence of file, file_exists function return true even if the path provided exists, which is in accordance with php manual’s definition, it’s just the name that is confusing. So, its better to use is_file() together with file_exists() if the objective is to check if the file really exists or is_dir()to check for directory.

Cheers!