插入动态创建的复选框的值

时间:2016-12-30 05:52:38

标签: php

在表格行中,我有两个选择菜单,两个复选框和一个加号按钮。在单击加号按钮时,该行将创建两个选择菜单和两个复选框,如果单击加号按钮,这将继续。

我已成功插入两个选择菜单的值,但我无法插入两个复选框的值。只有一个复选框值插入数据库中。我使用了DOM和PHP。我不知道我错在哪里。我需要你的帮助......

以下是代码:

function show_add_edit($row_id = '')
{   

    if ($row_id == '') 
    {
        $current_mode = "Add";  
    }   
    else 
    {
        $current_mode = "Edit";     

        $sql="SELECT * FROM `teacher` WHERE id = ".$row_id;   
        $rs=mysql_query($sql) or die(mysql_error()." Error in show edit: ");
        $rec=mysql_fetch_assoc($rs);

    }

?>
<script language="JavaScript" type="text/javascript">
function trim(str)
{
    return str.replace(/^\s+|\s+$/g,'');
}

function chkSubmit()
{

}

</script>

<div class="container-fluid">
<div class="row-fluid">
                    <div class="span12">
                       <!-- BEGIN STYLE CUSTOMIZER -->
                         <?php include("theme_colour.php");?>
                        <!-- END BEGIN STYLE CUSTOMIZER --> 
                        <!-- BEGIN PAGE TITLE & BREADCRUMB-->           
                        <h3 class="page-title">
                            Teacher Management
                        </h3>
                        <ul class="breadcrumb">
                     <li>
                        <i class="icon-home"></i>
                        <a href="admin_main.php">Home</a> 
                        <span class="icon-angle-right"></span>
                     </li>
                     <li>
                        <a href="manage_teacher.php">Teacher Management</a>
                        <span class="icon-angle-right"></span>
                     </li>
                     <li><a href="#"><?=$current_mode?> Teacher </a></li>
                  </ul>
                        <!-- END PAGE TITLE & BREADCRUMB-->
                    </div>
                </div>
                 <?php if($_SESSION['succ_msg']!='') { ?>
                <div class="alert alert-success">
                                    <button data-dismiss="alert" class="close"></button>
                                    <strong>Success!</strong> <?=$_SESSION['succ_msg']; unset($_SESSION['succ_msg']);?>
                </div>
                <?php } ?>
                <?php if($_SESSION['err_msg']!='') {
                 ?>
                <div class="alert alert-error">
                                    <button data-dismiss="alert" class="close"></button>
                                    <strong>Error!</strong> <?=$_SESSION['err_msg']; unset($_SESSION['err_msg']);?>
                </div>
                 <?php } ?>
<div class="row-fluid">
               <div class="span12">
                  <!-- BEGIN SAMPLE FORM PORTLET-->   
                  <div class="portlet box red">
                     <div class="portlet-title">
                        <h4><i class="icon-reorder"></i><?=$current_mode?> Teacher</h4>
                        <div class="tools">
                           <a href="javascript:;" class="collapse"></a>
                           <a href="javascript:;" class="reload"></a>
                        </div>
                     </div>
                     <div class="portlet-body form">
                        <!-- BEGIN FORM-->
                         <form name="frmedit" method="post" class="form-horizontal form-bordered" action="" onSubmit="return chkSubmit();" enctype="multipart/form-data">
        <input type="hidden" name="mode" value="update">
        <input type="hidden" name="current_mode" value="<?=$current_mode?>" />
        <input type="hidden" name="row_id" value="<?=$row_id?>" >
        <input type="hidden" name="pageNo" value="<?=$_REQUEST[pageNo]?>">
        <input type="hidden" name="url" value="<?=$currentPage?>">
        <input type="hidden" name="search_type" value="<?=$_REQUEST['search_type']?>">
        <input type="hidden" name="search_mode" value="<?=$_REQUEST['search_mode']?>">
        <input type="hidden" name="txt_alpha" value="<?=$_REQUEST['txt_alpha']?>">
        <input type="hidden" name="txt_search" value="<?=$_REQUEST['txt_search']?>">
        <input type="hidden" name="hold_page" value="<?=$_REQUEST['hold_page']?>">
                           <?php

                           ?>
                           <div class="control-group">
                              <label class="control-label" >Teacher Name </label>
                              <div class="controls">
                               <input tabindex="1" type="text" name="teacher_name" id="teacher_name"  class="m-wrap large" value="<?=trim(stripslashes($rec['teacher_name']))?>"  />
                              </div>
                           </div>

                           <div class="control-group">
                              <label class="control-label" >Teacher Phone </label>
                              <div class="controls">
                               <input tabindex="1" type="text" name="teacher_phone" id="teacher_phone"  class="m-wrap large" value="<?=trim(stripslashes($rec['teacher_phone']))?>"  />
                              </div>
                           </div >


                           <div class="control-group">
                           <label class="control-label" >Notice Board</label>
                           <div class="controls">
                           <label class="radio-inline">
    <input type="radio" name="notice_board" <?php if($rec['notice_board'] == "yes"){ echo "checked"; }?> value="yes" > Yes
                               </label>

    <label class="radio-inline">
      <input type="radio" name="notice_board"  <?php if($rec['notice_board'] == "no") { echo "checked"; }?> value="no">No
    </label>
      </div>                           
                           </div>

                            <p></p>
                         <div class="control-group">

                          <table class="table table-bordered table-responsive">
                           <tr>

                               <th width="20%">Class</th>
                               <th width="25%">Section</th>
                               <th width="25%">Rights</th>
                               <th width="35%">Row</th>
                           </tr>
                        <tbody class="detail">  

                               <?php

                            if($current_mode = "Add")
                              {
                            ?>
                            <tr>
                               <td width="20%">
                                 <select  class="m-wrap" name="class_id[]" id="class" style="display:block">
                                    <option value="">Select Class</option>  
                                <?php
                                $tres=mysql_query("SELECT  distinct(class_name) FROM class  ");
                                 $options="";
                                    while($rw=mysql_fetch_array($tres))
                                    {  
                                if($get_class['class_id']==$rw['class_name'])
                                {
                                    $check="selected";
                                }
                $options .="<option ".$check." value='$rw[class_name]'>$rw[class_name]</option>";                              

                                     } 
                                echo $options; 
                                 ?>
                                </select>  
                               </td>

                               <td width="25%">
                                  <span id="showSubSubcategory">

                                <select  class="m-wrap" name="sec_name[]"   style="display:block">
                                     <option value="">Select Section</option>
<?php   echo $options2 ="<option selected value='$get_class[sec_name]'>$get_class[sec_name]</option>";                             
 ?>                                  
                            </select>
                            </span>
                               </td>
                               <td width="35%">         

           <input type='checkbox'  name='rights[]'  <?php if($get_class['rights']=='1')echo "checked"; ?> value='1' id='checkbox-1'  /> diary
           <input type='checkbox'  name='rights[]'  <?php if($get_class['rights']=='2')echo "checked";?> value='2' id='checkbox-2'  /> calender

                               </td>
                               <td width="25%">
                            <a href="javascript:void(0);" onclick="add_row();" title="ADD">Add More</a> 
                            <input type="hidden"  name="row_val[]" value="0">
                            </td></tr>
                            <?                          
                              }

                             if($current_mode = "Edit")
                               {
                                 $class_allotted=mysql_query("SELECT * FROM set_teacher WHERE teacher_id='".$id."' ");
                               }


                $class_allotted=mysql_query("SELECT * FROM set_teacher WHERE teacher_id='".$id."' ");

                    while($get_class=mysql_fetch_assoc($class_allotted))
                    {
                               ?><tr>
                               <td width="20%">
                                 <select  class="m-wrap" name="class_id[]" id="class" style="display:block">
                                    <option value="">Select Class</option>  
                                <?php
                                $tres=mysql_query("SELECT  distinct(class_name) FROM class  ");
                                 $options="";
                                    while($rw=mysql_fetch_array($tres))
                                    {  
                                if($get_class['class_id']==$rw['class_name'])
                                {
                                    $check="selected";
                                }
                $options .="<option ".$check." value='$rw[class_name]'>$rw[class_name]</option>";                              

                                     } 
                                echo $options; 
                                 ?>
                                </select>  
                               </td>

                               <td width="25%">
                                  <span id="showSubSubcategory">

                                <select  class="m-wrap" name="sec_name[]"   style="display:block">
                                     <option value="">Select Section</option>
<?php   echo $options2 ="<option selected value='$get_class[sec_name]'>$get_class[sec_name]</option>";                             
 ?>                                  
                            </select>
                            </span>
                               </td>
                               <td width="35%">         

           <input type='checkbox'  name='rights[]'  <?php if($get_class['rights']=='1')echo "checked"; ?> value='1' id='checkbox-1'  /> diary
           <input type='checkbox'  name='rights[]'  <?php if($get_class['rights']=='2')echo "checked";?> value='2' id='checkbox-2'  /> calender

                               </td>
                               <td width="25%">
                            <a href="javascript:void(0);" onclick="add_row();" title="ADD">Add More</a> 
                            <input type="hidden"  name="row_val[]" value="0">
                            </td></tr>
                <?php
                    }
                    ?>                   

                          </tbody>
                      </table>

                        </div>

                           <div class="form-actions">
                              <button tabindex="2" type="submit" class="btn red">Submit</button>
                              <button tabindex="3" type="button" class="btn black" onclick="cancel();">Cancel</button>
                           </div>
                        </form>

                        <!-- END FORM--> 

                     </div>
                  </div>
                  <!-- END SAMPLE FORM PORTLET-->
               </div>
            </div>
            </div>

<script>
function add_row()
               {
                 n=$(".detail tr").length ;

        var tr='<tr>'+
                '<td width="20%">'+
                  '<select  class="m-wrap" name="class_id[]" id="class"  style="display:block">'+
                     '<option value="">Select Class</option>'+
       "<?php echo $options; ?>"+
          '</select>'+
          '</td>'+  
        '<td width="20%">'+
                '<span id="showSubSubcategory">'+
                                '<select  class="m-wrap" name="sec_name[]"   style="display:block">'+
                                     '<option value="">Select Section</option>'+
                            '</select>'+
          '</td>' + 
          '<td width="35%">'+
        "<input type='checkbox'  name='rights[]'  value='1' id='checkbox-1'  /> diary"+
           "<input type='checkbox'  name='rights[]' value='2' id='checkbox-2'  /> calender"+
          '</td>'+
          '<td width="25%">\
            <a href="javascript:void(0);"  onclick="add_row();" title="ADD">Add More</a>'+
            '<input type="hidden"  name="row_val[]" value="'+n+'">'+            
                              ' </td>'+
          '</tr>';
$(".detail").append(tr);           
             }
</script>   
<? 
}
function update_record($row_id='')
{     
    if($row_id == '') 
    {
       $id = 0;
        echo "<pre>";
        print_r($_REQUEST);

        $teacher_sql="INSERT INTO teacher (`teacher_name`,`teacher_phone`,`notice_board`,`created_date`) VALUES ('".$_REQUEST['teacher_name']."','".$_REQUEST['teacher_phone']."','".$_REQUEST['notice_board']."','".date('Y-m-d')."')";

        //mysql_query($teacher_sql);


        $get_teacher_id=mysql_fetch_assoc(mysql_query("SELECT * FROM teacher ORDER BY id DESC limit 1 "));
        $get_teacher_id['id'];
     $sql="INSERT INTO `set_teacher` (`teacher_id`,`class_id`, `sec_name`,`rights`) VALUES  ";

    $rights_arr=array();
    exit();
       for($i=0;$i<count($_REQUEST['class_id']);$i++)
       { 

          $sql.= "('".$get_teacher_id['id']."','".$_REQUEST['class_id'][$i]."','".$_REQUEST['sec_name'][$i]."','".$_REQUEST['rights'][$i]."'),";
       }
      $sql=rtrim($sql,",");
      $sql;



     mysql_query($sql) or die(mysql_error()." Error in insert.".$sql);

            // mysql_query($sql1) or die(mysql_error()." Error in insert.".$sql1);

      $_SESSION['succ_msg'] = "Class has been successfully Scheduled.";

      header("Location: manage_teacher.php");

    }
    else 
    {
                 $id = $row_id;

                 $sql1 = "update gender SET
                        `gender_name`   = '".addslashes(trim($_REQUEST['gender_name']))."'
                         where gender_id    =  ".$id;

          mysql_query($sql1) or die(mysql_error()." Error in update.".$sql1);

          $_SESSION['succ_msg'] = "Gender has been successfully updated.";

          disphtml("main();");

    }


}

0 个答案:

没有答案