Blog Archives

Making accessible Ajax web form

I am a fan of jquery and I am used to of it so I am going to use jquery as a library for examples I provide here. OK let’s start on how to make ajax form first. What we usually do is as follows.

First create a form

A simple form that accepts full name of the user.

<form id="ajaxform" method="post" action="action.php">

<input type="text" name="full_name" id="full_name" />

<input type="submit" name="submit" value="submit" />

</form>

Now js(ajax) part

$('#ajaxform').submit(function(){

$.ajax({

url:'action.php',

type:'POST',

data:'full_name='+$('#full_name').val(),

success:function(response){

if(response=='success')

{

alert('we have recieved your first name');

}

else

{

alert('something went wrong we are looking in to it');

}

}

});

return false;//returning false so that form is not submitted as we will be submitting it through js

});

OK now this is a simple ajax form that we did. For accessibility the question comes is how we handle data that is sent from js enabled browser and not enabled browser. However the request is sent to the action.php is both condition with the post data we will need to distinguish the request coming from the js enabled browser and the ones where js is not enabled. So what we do is add a parameter to the js data field and tell action.php that it is coming from the js enabled browser. please look at the difference in the js code below.

Now js(ajax) part

$('#ajaxform').submit(function(){

$.ajax({

url:'action.php',

type:'POST',

data:'full_name='+$('#full_name').val()+'&is_js_enabled=1',

success:function(response){

if(response=='success')

{

alert('we have recieved your first name');

}

else

{

alert('something went wrong we are looking in to it');

}

}

});

return false;//returning false so that form is not submitted as we will be submitting it through js

});

Now we can make action.php as follows

<?php

if(isset($_POST['is_js_enabled']))

{

//we will show user some fancy html here so that they don't just see success and failed text

if(isset($_POST['full_name']))

{

<div class="success">

<p>We got your full_name</p>

</div>

}

else

{

<div class="failed">

<p>We were not able to get your full_name</p>

</div>

}

}

else

{

//we will return success or failed message here

if(isset($_POST['full_name']) and $_POST['full_name']!='')

{

echo 'success';

}

else

{

echo 'failed';

}

}

OK all Done this can be your accessible Ajax web from now

Storing your session to database in php

I am developing a custom solutions for my projects. I was finding solution to secure my php applications. As security is top concerned when I develop my applications. I had to develop a solution for a shared hosting environment and found out that the session hijacking was a big problem while on shared server. The best solution was to store the session in some where secured place. I Researched about it and fund out that there was already a solution in php to do it. we could actually use session_set_save_handler() function to do it. And for the location to store session database was the suitable place. As database has it’s own authentication layer. So i decided to use it for my applications.

First create a databse table with following sql in you application datatbase:


CREATE TABLE IF NOT EXISTS `yourAppPrefix_sessions` (
`id` varchar(32) NOT NULL,
`access` int(10) unsigned default NULL,
`data` text,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM;

Below i wil provide you the code to use it on your own application:


session_set_save_handler('_open',
'_close',
'_read',
'_write',
'_destroy',
'_clean');

function _open()
{
return true;
}

function _close()
{

return true;
}

function _read($id)
{

$id = mysql_real_escape_string($id);

$sql = "SELECT data
FROM   `[p]sessions`
WHERE  id = '$id'";

if ($result = mysql_query($sql))
{
if (mysql_num_rows($result))
{
$record = mysql_fetch_array($result);

return $record['data'];
}
}

return '';
}

function _write($id, $data)
{

$access = time();

$id = mysql_real_escape_string($id);
$access = mysql_real_escape_string($access);
$data = mysql_real_escape_string($data);

$sql = "REPLACE
INTO    `[p]sessions`
VALUES  ('$id', '$access', '$data')";

return mysql_query($sql);
}

function _destroy($id)
{

$id = mysql_real_escape_string($id);

$sql = "DELETE
FROM   `[p]sessions`
WHERE id = '$id'";

return mysql_query($sql);
}

function _clean($max)
{

$old = time() - $max;
$old = mysql_real_escape_string($old);

$sql = "DELETE
FROM   `[p]sessions`
WHERE  access < '$old'";

return mysql_query($sql);
}

Save the above code and name it session.php or anything you want.

After this where you want to use session in your application. just add following to start session:


//Store session to database
require_once("path_to/session.php");
session_start();

Also be sure you have already connected your database before the above code in your applicaiton as it uses database to store session.

Now after this is done you can use session as usual. This just changes the place where your session is stored i.e in database. Other things are as same as you would store session in the local machine. I hope it will make you clear about what I am talking about.

Best tutorials site for web development

Today I am going to post some of the best available tutorial sites for web development and designing.

1. www.tizag.com

I find this site very useful for starter. It has tutorials on html, css, javascript, php, asp, xml, mysql etc. I refer this site if you are beginner and want to get some fundamentals on any of the language above.

2. www.w3schools.com

This site provides tutorials on most of the web languages. It also has some quiz to test your skills. Get in to this school before you get ready for web arena.

[ad#yalamber]

3. www.tutorialized.com

Offers wide variety of tutorials categorized in various category. It actually doesn’t provide it’s own tutorials. What it actually does is provide links to various site that are providing tutorials. The links are opened in the iframe within the site. It will be very useful if you once follow the sites I listed above and then use this site to get some practice.

These should be enough. If you want to add any please comment. It will be useful.

starter’s tutorial for php

Here are the link to the best php tutorials available for starter.

tizag.com’s php tutorial

-Indeed a very good tutorial. Will help you learn faster with clear examples.

Php manual

-What else can be better than the well documented manual itself. Visit the manual page or download it from here

These two can be enough for you to start of with. If you want to do more advanced stuffs, make content sites with php you will need to know about mysql databases. I will try to include best links for mysql in my next posts.

powered by WordPress | theme by yalamber S.