doomm4711 2015-12-09 04:30
浏览 117
已采纳

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

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条回答 默认 最新

  • douxiong1994 2015-12-09 05:04
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 游戏盾如何溯源服务器真实ip?
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥15 抖音看过的视频,缓存在哪个文件