如何在php中实现多个复选框

时间:2015-12-09 04:30:00

标签: javascript php html mysql

这是我的index.html

如果我将复选框名称作为数组,那么javascript将无法工作,如果我将复选框名称不在数组中,则多复选框值不起作用,它仅显示数据库中的最后一个检查值。

我认为我的所有脚本都是正确的,但我不知道为什么我的代码无法正常工作。 :(

<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 个答案:

答案 0 :(得分:1)

我建议在html中使用数组,因为你在第一时间尝试做..修复javascript很容易......

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

validateForm 功能中,您仍然可以检索该值并检查是否选中了复选框。

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