Submitting a form via AJAX

Ok so here is a very simple yet descriptive example how web-forms can be submitted using AJAX. This is an advanced tutorial, so although I’ll try to explain everything, I am assuming that the reader has a basic understanding of php and javascript.

AJAX is Asynchronous JavaScript And XML. Its a client side method to communicate data with a server. Asynchronous means that this communication can take place without interfering with other client side processes. (As we’ll see later, the asynchronous part is optional). Javascript is the main framework where AJAX works and XML is a format in which data might be communicated. Although this is not necessary. Data might be communicated in text, JSON, etc formats also.

Lets jump right in and look at this live demo.

Ok, lets look at the code now. The HTML structure is something like this:
http://pastebin.com/embed_iframe.php?i=tQEjujan

Continue reading

Changing Body Background Image Using Javascript

Things are getting weirder.
So I was trying to set the background image of a website using javascript. The requirement was to set an image as a background and then setting the parameters background-position as center and background-repeat as no-repeat. So here was my initial code:

document.body.style.background = “url(images/middle-bg.jpg)”;
document.body.style.background-position = “center”;
document.body.style.background-repeat = “no-repeat”;

AND IT WAS NOT WORKING!!

I tried variations, searched the forums. There were variations like
Continue reading

Line space in HTML

Cool, so after some frustrated googling, I found out a way to give spaces in html. Usually if you give lots of space between text in html it shows up as only one space.
To give more than one space, one can use this character:  

So if in HTML editor, I write something like:
Hi            there

then in the html page it will show up as:
Hi there

That’s where   comes in handy. So instead of space you’ll have to write something like:

Hi       there

to produce the desired effect.

php redirecting

I was searching for a way to redirect in php scripts to other pages.
Having used Javascript before, the obvious answer in front of me was using js inside php as follows:

<?php echo ‘<script>window.location = “page.html”;</script>’; ?>

It works completely fine. But it isn’t php exactly. So I looked on.
Another option I came upon was the php function header(), which also works fine and has the following format:

<?php header(“location: page.html”); ?>

I had a little doubt though. If I am using the header function in the middle if a php script, then it works fine. But in the php manual of this function http://php.net/manual/en/function.header.php, they clearly state that this function should only be used before any html tag or any php print command. Will look for answers. If the reader knows, he is requested to comment.

include(), require() and require_once()

>So here’s the difference between the three most often used php functions:


include();
require();
require_once();
All the above have one common function: they are used to import a code written in an external file. For example in php-mysql applications, a connection needs to be established with the database for carrying out operations in database. This is done via the mysql_connect() function which takes in the parameters of host, user and password. Now this function has to be called in all the files doing mysql tasks. So instead of that, this code is written in a connection.php file and then included in all the other files requiring database connection using the above three functions.
Coming to the difference part, require() as the name suggests is different in the sense that it produces a fatal error if the external file is not found, while include() just produces a warning and moves on with the code. The later may be undesirable at times, so require() is most often used. So in a nutshell, require() makes sure that the file is strictly imported.
require_once() is almost similar to require() except that it checks if the required file has already been imported, in the event of which it skips to the next code. This helps preventing more “redeclared” errors.
Hope this article was useful.