Hi,this is a simple javascript to get start with it
if ( document.getElementById('name').value == "" )
{
msg=msg + "\n--> Your name";
flag=1;
}
And your changed HTML,and the code you wrote is very buggy
<form method=POST onSubmit="validate_form ();" enctype='multipart/form-data' action='insert.php'>
<table>
<tr><td>Data 1</td> <td><input type='text' id='data1' name='data1'> </td></tr>
<tr><td>Data 2</td> <td><input type='text' id='data2' name='data2'> </td></tr>
<tr><td>Data 3</td> <td><input type='text' id='data3' name='data3'> </td></tr>
<tr><td colspan='2'><input type='submit'; value='Save'><input type='button' value='Cancel'></td></tr>
</table></form>;
<script language="javacript">
function validate_form ( )
{
var msg="Please fill in details...";
var flag=0;
//Declaration of msg variable
//Checking for all fields, whether they contain values or not
//If left blank and clicked on your submit button,it will show alert message
//Showing that the certain required fields are left as empty..
if ( document.getElementById('data1').value == "" )
{
msg=msg + "\n--> Your data1";
flag=1;
}
if ( document.getElementById('data2').value == "" )
{
msg=msg + "\n--> Your data2";
flag=1;
}
if ( document.getElementById('data3').value == "" )
{
msg=msg + "\n--> Your data3";
flag=1;
}
if(flag==1)
{
alert(msg);
return false;
}
else
{
return true;
}
}
<script> Few Important points..
1) Attribute's values are needed to be quoted in either single quote or double quotes,that you were not doing
e.g
2) i have specified a onsubmit attribute in FORM tag,what it will do is just before submitting your data to the server ,will check for validation,if any field is empty,then it will show alert and the funtion will return false.
So when onsubmit gets "false" as result,it cancels the submit event to the server,and so your invalid data does not reach to the server.
4 ) I have specified ID attribute to each element,to get it in the javascript as ame attribute is quite obsolete.
Hope it helpsss. :)
Mark it as solve if it helps,so it can help others....