Я хотел бы сохранить значения флажка в базе данных в форме (0 или 1). Если это чек, тогда отправьте значение 1 другой мудрый 0. но он всегда отправляет 1 в базу данных filed.I пытался, но не отправлял значение.
select.php
<script src="jquery.js"></script> <!--<script src="jquery.min.js"></script> --> <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>--> <html> <head><title>Checkbox Demo</title></head> <form class="form-horizontal" action="selectall.php" method="post"> <input type='button' name='Check_All' value='Check All' onClick='$(":checkbox").attr("checked",true);'> <input type='button' name='Un_CheckAll' value='Un Check All' onClick='$(":checkbox").attr("checked",false);'> <table align="center" style="width:100%"> <tr> <th width="450"><strong>Form Name</strong></th> <th width="150"><strong>select</strong></th> <th width="150"><strong>Edit</strong> </th> <th width="150"><strong>Delete</strong></th> <th width="150"><strong>View</strong></th> </tr> <tr> <td style="padding-left:20%"><input name="a1" type="hidden" value="Authentication">Authentication</td> <td style="padding-left:8%"><input type="checkbox" id="" name="emailid[]" value="1" class="case"/> </td> </tr> <tr> <td style="padding-left:20%"><input name="a2" type="hidden" value="User">User</td> <td style="padding-left:8%"><input type="checkbox" id="" name="emailid[]" value="1" /></td> </tr> <tr> <td style="padding-left:20%"><input name="a3" type="hidden" value="Change User_Password">Change User_Password</td> <td style="padding-left:8%"><input type="checkbox" id="" name="emailid[]" value="1" /></td> <tr> <tr> <td style="padding-left:20%"><input name="a4" type="hidden" value="Add Employee Details">Add Employee Details</td> <td style="padding-left:8%"><input type="checkbox" id="" name="emailid[]" value="1" /></td> </tr> <tr> <td style="padding-left:20%"><input name="a5" type="hidden" value="All Employee">All Employee </td> <td style="padding-left:8%"><input type="checkbox" id="" name="emailid[]" value="1" /></td> <td style="padding-left:8%"><input type="checkbox" id="" name="emailid[]" value="1" /></td> <td style="padding-left:8%"> <input type="checkbox" id="" name="emailid[]" value="1"/></td> <td style="padding-left:8%"> <input type="checkbox" id="" name="emailid[]" value="1"/></td> </tr> <tr> <td style="padding-left:20%"><input name="a6" type="hidden" value="Company Profile">Company Profile </td> <td style="padding-left:8%"><input type="checkbox" id="" name="emailid[]" value="1" /></td> <td style="padding-left:8%"><input type="checkbox" id="" name="emailid[]" value="1" /></td> <td style="padding-left:8%"> <input type="checkbox" id="" name="emailid[]" value="1"/></td> <td style="padding-left:8%"> <input type="checkbox" id="" name="emailid[]" value="1"/></td> </tr> </table> </div> </div> </div> </div> </div> <input type="submit" name="submit" value="SAVE" class="btn btn-info"> </form> </html>
selectall.php
<?php include('DB.class.php'); //create an instance of the DB class $db = new DB(); $db->connect(); if(isset($_POST['emailid'])){ foreach($_POST['emailid'] as $selected){ echo $selected; $data["as1"]=$selected; $data["as2"]=$selected; $data["as3"]=$selected; $data["as4"]=$selected; $data["as5"]=$selected; $data["ae5"]=$selected; $data["ad5"]=$selected; $data["av5"]=$selected; $data["as6"]=$selected; $data["ae6"]=$selected; $data["ad6"]=$selected; $data["av6"]=$selected; } } $db->Insert("selectdemot",$data); ?>
в<?php include('DB.class.php'); //create an instance of the DB class $db = new DB(); $db->connect(); if(isset($_POST['emailid'])){ foreach($_POST['emailid'] as $selected){ echo $selected; $data["as1"]=$selected; $data["as2"]=$selected; $data["as3"]=$selected; $data["as4"]=$selected; $data["as5"]=$selected; $data["ae5"]=$selected; $data["ad5"]=$selected; $data["av5"]=$selected; $data["as6"]=$selected; $data["ae6"]=$selected; $data["ad6"]=$selected; $data["av6"]=$selected; } } $db->Insert("selectdemot",$data); ?>
DB.class.php
<?php //DB.class.php /***************************************************************************** /*Copyright (C) 2013 Narwade Jaywant For any details please feel free to contact me at info@technopia.in /*****************************************************************************/ class DB { protected $db_name = 'selectdemo'; protected $db_user = 'root'; protected $db_pass = ''; protected $db_host = 'localhost'; /*protected $db_name = 'infortte_yogesh'; protected $db_user = 'infortte_yogesh'; protected $db_pass = 'Yogesh@123'; protected $db_host = 'localhost';*/ //Constructor is called whenever a new object is created. function __construct() { $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass); mysql_select_db($this->db_name); return true; } public function connect(){ $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass); mysql_select_db($this->db_name); return true; } //takes a mysql row set and returns an associative array, where the keys //in the array are the column names in the row set. If singleRow is set to //true, then it will return a single row instead of an array of rows. public function processRowSet($rowSet, $singleRow=false) { $resultArray = array(); while($row = mysql_fetch_assoc($rowSet)) { array_push($resultArray, $row); } if($singleRow === true) return $resultArray[0]; return $resultArray; } //Select rows from the database. //returns a full row or rows from $table using $where as the where clause. //return value is an associative array with column names as keys. public function SelectTable($table, $where="",$fieldarray="",$debug="") { if ($fieldarray=="") { $f_list = "*"; } else { $f_list = $fieldarray ; } $sql = "SELECT $f_list FROM $table "; if( ! empty( $where ) ) $sql .= " WHERE $where"; if($debug==1){echo $sql;exit();} $result = mysql_query($sql); if( ! $result ) return 0; return $this->processRowSet($result); } public function SelectSingle($table, $where,$fieldarray="",$debug="") { if ($fieldarray=="") { $f_list = "*"; } else { $f_list = $fieldarray ; } $sql = "SELECT $f_list FROM $table "; if( ! empty( $where ) ) $sql .= " WHERE $where"; if($debug==1){echo $sql;exit();} $result = mysql_query($sql); if( ! $result ) return 0; if(mysql_num_rows($result) == 1) return $this->processRowSet($result, true); } //Updates a current row in the database. //takes an array of data, where the keys in the array are the column names //and the values are the data that will be inserted into those columns. //$table is the name of the table and $where is the sql where clause. public function Update($table,$where,$data,$debug="") { foreach ( $data as $column=>$value ) { if($value !="now()"){ $fv[] = "$column = \""."$value"."\""; }else{ $fv[]= "$column = "."$value".""; } } $fv_list = trim(implode(", ", $fv)); $sql = "UPDATE $table SET "."$fv_list"." WHERE $where"; if($debug==1){echo $sql;exit();} mysql_query($sql) or die(mysql_error()); return true; } //Inserts a new row into the database. //takes an array of data, where the keys in the array are the column names //and the values are the data that will be inserted into those columns. //$table is the name of the table. public function Insert($table,$data,$debug="") { $columns = ""; $values = ""; foreach( $data as $column=>$value ) { $field[] = $column; if($value !="now()") $values[] = "'$value'"; else $values[] = "$value"; } $columns = trim( implode(", ", $field) ); $values = trim( implode(", ", $values) ); $sql = "insert into $table ($columns) values ($values)"; if($debug==1){echo $sql;exit();} mysql_query($sql) or die(mysql_error()); //return the ID of the user in the database. return mysql_insert_id(); } public function Delete($table, $condition) { $query = "DELETE FROM $table WHERE $condition"; $result = mysql_query( $query); if( ! $result ) return 0; return 1; } } ?>
Включите jquery.js
База данных:
CREATE TABLE IF NOT EXISTS `selectdemot` ( `sr_no` int(11) NOT NULL, `as1` enum('0','1','','') NOT NULL, `as2` enum('0','1','','') NOT NULL, `as3` enum('0','1','','') NOT NULL, `as4` enum('0','1','','') NOT NULL, `as5` enum('0','1','','') NOT NULL, `ae5` enum('0','1','','') NOT NULL, `ad5` enum('0','1','','') NOT NULL, `av5` enum('0','1','','') NOT NULL, `as6` enum('0','1','','') NOT NULL, `ae6` enum('0','1','','') NOT NULL, `ad6` enum('0','1','','') NOT NULL, `av6` enum('0','1','','') NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; INSERT INTO `selectdemot` (`sr_no`, `as1`, `as2`, `as3`, `as4`, `as5`, `ae5`, `ad5`, `av5`, `as6`, `ae6`, `ad6`, `av6`) VALUES (1, '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'), (2, '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1');
Непроверенные поля не будут отправлять какие-либо данные, поэтому один из способов сделать это – сделать скрытые входы с одинаковыми значениями и именем массива. Это некрасиво, но это работает. Вот и пример (только одна часть вашей формы):
<form class="form-horizontal" action="selectall.php" method="post"> <!--selectall.php--> <input type='button' name='Check_All' value='Check All' onClick='$(":checkbox").attr("checked",true);'> <input type='button' name='Un_CheckAll' value='Un Check All' onClick='$(":checkbox").attr("checked",false);'> <table align="center" style="width:100%"> <tr> <th width="450"><strong>Form Name</strong></th> <th width="150"><strong>select</strong></th> <th width="150"><strong>Edit</strong> </th> <th width="150"><strong>Delete</strong></th> <th width="150"><strong>View</strong></th> </tr> <tr> <td style="padding-left:20%"><input name="a1" type="hidden" value="Authentication"> Authentication</td> <td style="padding-left:8%"> <input type="hidden" name="auth[1]" value="0" /> <input type="checkbox" id="" name="auth[1]" value="1" class="case"/> </td> </tr> <tr> <td style="padding-left:20%"><input name="a2" type="hidden" value="User"> User</td> <td style="padding-left:8%"> <input type="hidden" name="user[1]" value="0" /> <input type="checkbox" id="" name="user[1]" value="1" /> </td> </tr> <tr> <td style="padding-left:20%"><input name="a3" type="hidden" value="Change User_Password"> Change User_Password</td> <td style="padding-left:8%"> <input type="hidden" name="passmod[1]" value="0" /> <input type="checkbox" id="" name="passmod[1]" value="1" /> </td> <tr> <tr> <td style="padding-left:20%"><input name="a4" type="hidden" value="Add Employee Details"> Add Employee Details</td> <td style="padding-left:8%"> <input type="hidden" name="employee[1]" value="0" /> <input type="checkbox" id="" name="employee[1]" value="1" /> </td> </tr> <tr> <td style="padding-left:20%"><input name="a5" type="hidden" value="All Employee"> All Employee </td> <td style="padding-left:8%"> <input type="hidden" name="emp_select[1]" value="0" /> <input type="checkbox" id="" name="emp_select[1]" value="1" /> </td> <td style="padding-left:8%"> <input type="hidden" name="emp_edit[1]" value="0" /> <input type="checkbox" id="" name="emp_edit[1]" value="1" /> </td> <td style="padding-left:8%"> <input type="hidden" name="emp_delete[1]" value="0" /> <input type="checkbox" id="" name="emp_delete[1]" value="1"/> </td> <td style="padding-left:8%"> <input type="hidden" name="emp_view[1]" value="0" /> <input type="checkbox" id="" name="emp_view[1]" value="1"/> </td> </tr> <tr> <td style="padding-left:20%"><input name="a6" type="hidden" value="Company Profile"> Company Profile </td> <td style="padding-left:8%"> <input type="hidden" name="profile_select[1]" value="0" /> <input type="checkbox" id="" name="profile_select[1]" value="1" /> </td> <td style="padding-left:8%"> <input type="hidden" name="profile_edit[1]" value="0" /> <input type="checkbox" id="" name="profile_edit[1]" value="1" /> </td> <td style="padding-left:8%"> <input type="hidden" name="profile_delete[1]" value="0" /> <input type="checkbox" id="" name="profile_delete[1]" value="1"/> </td> <td style="padding-left:8%"> <input type="hidden" name="profile_view[1]" value="0" /> <input type="checkbox" id="" name="profile_view[1]" value="1"/> </td> </tr> </table> <input type="submit" name="submit" value="SAVE" class="btn btn-info"> </form>
Эта форма создаст что-то вроде:
Array ( [a1] => Authentication [auth] => Array ( [1] => 1 ) [a2] => User [user] => Array ( [1] => 0 ) [a3] => Change User_Password [passmod] => Array ( [1] => 0 ) [a4] => Add Employee Details [employee] => Array ( [1] => 1 ) [a5] => All Employee [emp_select] => Array ( [1] => 0 ) [emp_edit] => Array ( [1] => 0 ) [emp_delete] => Array ( [1] => 1 ) [emp_view] => Array ( [1] => 0 ) [a6] => Company Profile [profile_select] => Array ( [1] => 1 ) [profile_edit] => Array ( [1] => 1 ) [profile_delete] => Array ( [1] => 0 ) [profile_view] => Array ( [1] => 1 ) [submit] => SAVE )