This is my index.html
If i put check boxes names as arrays then javascript won't work, if i put check box names not in arrays then multi check box value didnt work, its shows only the last checked value in database.
All my scripts are right i think, but i dont know why my code won't work. :(
<html>
<head>
<script>
function validateForm()
{
var a=document.forms["emp"]["empid"].value;
var b=document.forms["emp"]["empname"].value;
var c=document.forms["emp"]["desig"].value;
var d=document.forms["emp"]["dept"].value;
var f=document.forms["emp"]["skill"].value;
if(a == null || a == "")
{
alert("Employee id must be filled out");
return false;
}
if(b == null || b == "")
{
alert("Employee name must be filled out");
return false;
}
if(c == null || c == "")
{
alert("Employee Designation must be filled out");
return false;
}
if(d == null || d == "")
{
alert("Employee department must be filled out");
return false;
}
if((emp.gender[0].checked==false) && (emp.gender[1].checked==false))
{
alert("Please select Any one gender"); return false;
}
}
function numeric(num)
{ var g=/^[0-9]+$/;
var h=document.getElementById(num).value;
if(h.match(g))
{
return true;
}
else
{
alert("Enter numeric values only");
document.getElementById(num).value=' ';
return false;
}
}
function alphabets(t)
{
var regex = /^[a-zA-Z ]*$/;
var y=document.getElementById(t).value;
if(y.match(regex)){
return true;
}
else
{
alert("Only Alphabetic characters");
document.getElementById(t).value=' ';
return false;
}
}
</script>
</head>
<body bgcolor="#74AFAD" >
<center>
<br><br><br><h2>
Employee Master<br><br>
<form method="post" action ="add.php" name="emp" >
<table border=2 cellpadding=10>
<tr><td>Employee code</td> <td><input type="text" name="empid" id="e_id" oninput="numeric(id)" ></td></tr>
<tr><td>Employee name</td> <td><input type="text" name="empname" id="e-name" oninput="alphabets(id)"></td></tr>
<tr><td>Designation</td> <td><input type="text" name="desig"></td></tr>
<tr><td>Department</td> <td><select name="dept">
<option value="">Select department</option>
<option value="sales">Sales</option>
<option value="purchase">Purchase</option>
<option value="production">Production</option></td></tr>
<tr><td>Gender</td>
<td><input type="radio" name="gender" value="male" >Male
<input type="radio" name="gender" value="female" >Female</td></tr>
<tr><td>Skill </td> <td>
<input type="checkbox" name="skill" value="sk1"> Sk1
<input type="checkbox" name="skill" value="sk2"> Sk2
<input type="checkbox" name="skill" value="sk3"> Sk3</td> </tr>
</table>
<br><br>
<input type="submit" name="add" value="Add" onclick="return validateForm();" >
<input type="submit" name="view" value="View" >
<input type="submit" name="truncate" value="Truncate" >
<input type="submit" name="delete" value="Delete" >
<input type="submit" name="update" value="Update" >
</form>
</center>
</body>
</html>
2.second_file.php
<body bgcolor="#74AFAD" >
<?php
$empid=$_POST['empid'];
$empname=$_POST['empname'];
$desig=$_POST['desig'];
$dept=$_POST['dept'];
@$gender=$_POST['gender'];
@$skill=$_POST['skill'];
//$_SESSION['user']=$_POST['empname'];
//echo "Welcome ".$_SESSION['user'];
$con=mysql_connect("localhost","root","") or die("Error connecting to MYSQl");
mysql_select_db("employee",$con);
if(isset($_POST['add']))
{
if(isset($_POST['skill']))
{
$skill=implode(",",$_POST['skill']);
}
$query="insert into emp_details values ('$empid','$empname','$desig','$dept','$gender','$skill')" ;
mysql_query($query) or die(mysql_error());
header("location:index.php");
}
if(isset($_POST['view']))
{
$query="select empid,empname,desig,dept,gender,skill from emp_details";
$results=mysql_query($query) or die(mysql_error());
echo "<center><br><br><br><table border=2>";
echo "<th>Employeeid</th><th>Employeename</th><th>Designation</th><th>Department</th><th>Gender</th><th>Skill</th>";
while($row=mysql_fetch_array($results,MYSQL_ASSOC)){
echo "<tr><td>".$row['empid']. "</td>";
echo "<td>".$row['empname']. "</td>";
echo "<td>".$row['desig']. "</td>";
echo "<td>".$row['dept']. "</td>";
echo "<td>".$row['gender']. "</td>";
echo "<td>".$row['skill']. "</td></tr>";
}
}
if(isset($_POST['truncate']))
{
$query="truncate table emp_details";
mysql_query($query) or die(mysql_query);
header("location:index.php");
}
if(isset($_POST['delete']))
{
header("location:delete.php");
}
if(isset($_POST['update']))
{
header("location:update.php");
}
?>