0

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");
}

?>

1 Answer 1

1

I would recommend to make use of array in the html as you were attempting to do in the first place.. Fixing the javascript is easy...

<input type="checkbox" name="skill[]" value="sk1"> Sk1
<input type="checkbox" name="skill[]" value="sk2"> Sk2
<input type="checkbox" name="skill[]" value="sk3"> Sk3

In you validateForm function you can still retreive the value and also check if the checkbox is checked..

var hasOneSkillSelected = false;
    for ( var i=0; i<document.forms["emp"]["skill[]"].length; i++ ) {
        var currentSkillOption = document.forms["emp"]["skill[]"][i].checked;
        hasOneSkillSelected = hasOneSkillSelected || currentSkillOption;
    }
    console.log(hasOneSkillSelected);
Sign up to request clarification or add additional context in comments.

5 Comments

Jas i have one more doubt, i have an update form, which will update the employee details. how can i put all details in the textboxes and dropdown menus,etc from database ?
Aravindh, to pull the record from the database first you need a way to pull record from the database, (I am assuming the database will have many records ) You can approach this two ways.. 1:- on entering the employee id, make an ajax call and retrieve the values and apply the html form. 2:- post the employee id to the server and then render the html based on the values received from the database. In this approach you want to either have a seperate page for performing update or introduce a "mode" variable to track on the operation.
bro i have one more doubt .
if((emp.skill[0].checked==false) && (emp.skill[1].checked==false) && (emp.skill[2].checked==false) { alert("Any one skill must be selected"); return false; }
var empskill = document.forms["emp"]["skill[]"]; if( (empskill[0].checked==false) && (empskill[1].checked==false) && (empskill[2].checked==false) ) { alert("Any one skill must be selected"); return false; } Note: storing the skill array into a local variable empskill ( without any dot )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.