Ajax tip: Use JSON format

Hi all,

The benefit of JSON on other formats is that it is recognized natively by JavaScript. This is huge advantage because you don’t need to write a code for parsing an XML document to extract the data and get it throught the net. So it saves time and it is faster.

The JSON format is very simple:

{
  "menu": "File",
  "commands": [
      {
          "title": "New",
          "action":"CreateDoc"
      },
      {
          "title": "Open",
          "action": "OpenDoc"
      },
      {
          "title": "Close",
          "action": "CloseDoc"
      }
   ]
}

and you use the eval() JavaScript function to  parse it.
var req = new XMLHttpRequest();
req.open("GET", "file.json", true);
req.onreadystatechange = myCode;   // the handler
req.send(null);

The JavaScript handler:

function myCode()
{
   if (req.readyState == 4)
   {
        var doc = eval('(' + req.responseText + ')');
   }
}

Online booking system

Hello all,

If you need an online booking system to your website, you can use S-Development ltd. They can quickly add it to your site according to your needs.

This is good solution for dental practices, hair dressers and anyone who needs appointment booking.

And it is not expensive either!

Javascript split numbers from string

Hi,

From time to time you have a string that contains numbers and letters and you need to split them.

This is very easy to do using Javascript.

Lets say you have this  string:

var string  = “due_date47″;

to create to vars that one will have “due_date” as value and the other “47″ do this:

<script type="text/javascript">var field = "due_date37";

var number=field.match(/\d/g).join('');

var nonnumber=field.match(/[^\d]/g).join('');

</script>

Easy!

Backup your database

Hi all,

No matter how good your script is if you don’t have backups it worth nothing.

I am using Ajax to develop very complicated databases to companies. Sometimes I am scared by the amount of data they store on the db and how important it is. If my clients losing their data then first they will kill me then they will kill themselves.

That’s why you need a backup! I would recommend using Websites backup. This will do the job for you…

How to change form target using Javascript?

Well sometime you need that. If you have two submit buttons, one you want to open in the same page and the other on a new one.

Just use this:

document.getElementById(form_id).target= “_blank”;

Hope it helps!

How to create a blinking effect using Javascript

Hi all,

Some time when you use Ajax to do an action you want to give the user an


indication that the action took place and everything went well. For example you have a text field that onchange action calls for a Javascript function that takes the value, sends it to PHP file, the PHP file gets the value and update the db and sends back to the Javascript function ‘Save’ message.

Now you want to display this message, but you also want that it will blink and disappear lets say after 3 seconds. Easy. All you have to do is from the Javascript function that gets the PHP value call to blinky function

<script type="text/javascript">
function blinky(it) {
  obj = document.getElementById(it);
  if (obj.style.visibility == "visible")
    {obj.style.visibility = "hidden";}
  else
    {obj.style.visibility = "visible";}
}

blink = setInterval("blinky's1')", 250);
</script>
<span id="s1">Save...</span>

and don’t forget to pass the div id as well…

Hope it helps!

Ajax Problem with encoding on ie7

Hi all,

I think I spent hours to find a solution to this problem:

On IE when you send a var to the php in non Latin chars (like Hebrew and Arabic), the php gets it like ???

today I found someone who found a solution! it is simple and works:

http://www.phpfreaks.com/forums/index.php?topic=211522.0

Hope it helps!

Small tip to get the status of checkbox

Hi all,

It  took me long time to get it… I wanted to check if a checkbox was checked or not.
So I used:
var checkbox = document.getElementById(&quot;checkbox_id&quot;).value;

Wrong! Wrong! Wrong!

the right way is:

var checkbox = document.getElementById(&quot;checkbox_id&quot;).checked;

which return true or false. Minor details wasted me lots of time…

Admin

How to submit a form using post and Ajax?

Hi all,

This is a very simple, powerful and useful Ajax code  that lets you submit a form using Ajax and ‘post’ method.

lets start with the form.

The most important thing is to give an ID to the form and then call to a Javascript function on submit.  We will have something like:

<form action=”add_client.php” method=”post” id=”add_client”  name=”add_client” onsubmit=”post_form(‘add_client’); return false;”>

<input type=”text” name=”client_name” id=”client_name”>

<input type=”submit” value=”Add”>

</form>

So when you submit this form the script calls the post_form function and send the form’s id (in this case add_client).

Now on the Javascript we will use the zxml library

<script type=”text/javascript” src=”zxml.js”></script>

and we will create the function post_form:

function post_form(form_id){

var oForm = document.getElementById(form_id);
var sBody = getRequestBody(oForm);
var oXmlHttp = zXmlHttp.createRequest();
oXmlHttp.open(“post”, oForm.action, true);
oXmlHttp.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);
oXmlHttp.onreadystatechange = function () {
if (oXmlHttp.readyState == 4) {
if (oXmlHttp.status == 200) {
view(oXmlHttp.responseText,page_name,display_type,div_name,job_id,span_id);
}
else {
view(“An error occurred: ” + oXmlHttp.statusText);
}
}
};
oXmlHttp.send(sBody);
}
function getRequestBody(oForm) {
var aParams = new Array();
for (var i=0 ; i < oForm.elements.length; i++) {
var sParam = encodeURIComponent(oForm.elements[i].name);
sParam += “=”;
if(oForm.elements[i].checked){
sParam += “1″;
}
else {
sParam += encodeURIComponent(oForm.elements[i].value);
}
aParams.push(sParam);
}
//alert (aParams);
return aParams.join(“&”);
}

}

post_form calls to getRequestBody that gets all the form’s elements and send it back. Then post_form will submit the form to the action location (add_client.php) in the php file you should create a script that connect to the db gets the new client name from the form and insert it. Then you can generates a confirmation message on the php file (something like: echo “Client has been added”;)

Now the post_form gets back the php message and we need to decide what to do with it. Lets create a div where the message will be displayed

<div id=”messege”></dib>

Now post_form calls to the function view and sends the php’s messege to it. So function view will looke like:

function view(sText){

var el = document.getElementById(‘message’);
el.innerHTML = sText;

}

that’s it… you submitted a form and got the results on the same page. Easy :)

Javascript element value

Hi all,

For beginners this could be helpful – How to get the value of each element in your page using Javascript?

Lets say you have <div id=”newid”></div>

use this:

var element_value = document.getElementById(&quot;newid&quot;).value;

Hope it helps!