jQuery Ajax Tutorial and Example

Before we are going with example let’s read something about jQuery and AJAX.

In this example we are submitting a simple form using AJAX.

To create a form first we are creating a database with name “ajax”.

Create a config file with name config.php.

<?
$dbhost = "localhost";   //replace it with your host name
$dbuser="root";               //replace it with your database username
$dbpassword="";             // replace it with your database password
$dbname="ajax";            // replace it with your database name

mysql_connect($dbhost,$dbuser,$dbpassword);
mysql_select_db($dbname);
?> Continue reading 

Easy way to data filter and validate in php

Before php 5.2.0 when we have to validate or filter user data, we normally use regex and some php functions. Some of those regex are difficult to understand. So normally most of the coders search in google to collect the correct regex to validate data and also use some php functions to filter data.

In php 5.2.0 a new extension is provided named filter to make these filter tasks much easy.  You can install it in your linux distro by simply typing in shell  pecl install filter

Before proceeding next at first check the available filters in your system:

echo '<pre>';
print_r(filter_list());
echo '</pre>';

Output in my system:

Array
(
    [0] => int
    [1] => boolean
    [2] => float
    [3] => validate_regexp
    [4] => validate_url
    [5] => validate_email
    [6] => validate_ip
    [7] => string
    [8] => stripped
    [9] => encoded
    [10] => special_chars
    [11] => unsafe_raw
    [12] => email
    [13] => url
    [14] => number_int
    [15] => number_float
    [16] => magic_quotes
    [17] => callback
)

Continue reading

[RFC] PHP 5.4 Strict Error Reporting

Testing with PHP 5.4 at E_ALL produces strict warning messages such as:

Strict (2048): Declaration of Product::beforeSave() should be compatible with Model::beforeSave($options = Array) [APP/Model/Product.php, line 4]

It’s an easy fix, for example in APP/Model/Product.php change:

public function beforeSave() {

to:

public function beforeSave($options = array()) {

Difference between Single quote & Double quote used in PHP ?

Single quotes will escape anything other than a backslash followed by either a single quote or another backslash literally.

Double quotes, however, will evaluate variable values and function calls before producing the final output, and include them within it. This is called (at least in Perl’s context), “interpolation”. So, given:

$var = ‘123’;
echo ‘$var’; // outputs $var
echo “$var”; // outputs 123

Single quotes reads the variable as a string but the Double quote reads the variable as a variable and returns the value for that variable.

For more information, take a look at the official documentation: http://php.net/manual/en/language.types.string.php

Pagination in CakePHP

In the controller file like I have Employeecontroller.php, write the following code:

<?php
class EmployeeController extends AppController
{
public $paginate = array(
‘limit’ => 2,
‘order’ => array(
‘Employee.id’ => ‘asc’
)
);

public function index()
{
$data = $this->paginate(‘Employee’);
$this->set(’employeeList’, $data);
}
}
?>

Now in the view file like I have index.ctp, write the following code:

<div align=”center”>
<!– Shows the page numbers –>
<?php echo $this->Paginator->numbers(); ?>
<!– Shows the next and previous links –>
<?php echo $this->Paginator->prev(‘« Previous ‘, null, null, array(‘class’ => ‘disabled’)); ?>
<?php echo $this->Paginator->next(‘ Next »’, null, null, array(‘class’ => ‘disabled’)); ?>
<!– prints X of Y, where X is current page and Y is number of pages –>
<?php echo $this->Paginator->counter(); ?>
</div>