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

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());

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

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

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

6 Years
Discussion Span
Last Post by Akash Saikia

Hi,this is a simple javascript to get start with it

if ( document.getElementById('name').value == "" )
msg=msg + "\n--> Your name";

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

<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";
if ( document.getElementById('data2').value == "" )
msg=msg + "\n--> Your data2";
if ( document.getElementById('data3').value == "" )
msg=msg + "\n--> Your data3";
return false;
return true;

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.