I'm a totally beginner in javascript. i need some chunks of code of it.

i have a form. i'm going to store the field into mysql. there are 2 conditions : one or all fields empty OR none is empty.

when there's no empty field, the data will be stored. but, when there's empty field, no data will be stored and there will come up the alert "please fill all the fields!"

here's the form :

<form method=POST enctype='multipart/form-data' onclick='show_alert()' action=insert.php>
	<table>
	<tr><td>Data 1</td>			<td><input type=text name='data1'> </td></tr>
	<tr><td>Data 2</td>			<td><input type=text name='data2'> </td></tr>
<tr><td>Data 3</td>			<td><input type=text name='data3'> </td></tr>
	<tr><td colspan='2'><input type=submit value='Save'><input type=button value=Cancel onclick=self.history.back()></td></tr>
	</table></form>";

here's the insert script:

if(!empty($_POST['data1'])&& !empty($_POST['data2'])&& !empty($_POST['data3'])){		
	mysql_query("INSERT INTO tabel_data(dataA,dataB,dataC)		
		VALUES('$_POST[data1]','$_POST[data2]','$_POST[data3]')") or die(mysql_error());
	header('location:form.php');}
	
else{?>

	<script type="text/javascript">
	function show_alert()
	{
		alert("Please fill all fields!");
	}
	</script> 

<?php header('location:form.php');
	}

i don't know how to show the alert with javasript. Please help me with the code..thank you.

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 <input type="text" value='Your name'>

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....

i'm sorry it's been soooo long i don't reply... i finally use form validation using js. i got combo there actually and i only check whether the option is selected or not.

thaks a lot for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.