<?php
//connect to database
function db_link()
{
$access_id = "root";
$accesss_pwd = "831025";
$db_name = "inv";
@ $db = mysql_connect('localhost', $access_id, $accesss_pwd) or
die("Could not connect to database. Please contact with IT supporting team ASAP.");
mysql_query("SET NAMES 'GBK'");
mysql_select_db($db_name);
return $db;
}
$link = db_link();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>select all/unselect all</title>
<script type="text/javascript">
function setCheckboxesRange(the_form, do_check, basename, min, max){
for (var i = min; i <= max; i++) {
if (typeof(document.forms[the_form].elements[basename + i]) != 'undefined') {
document.forms[the_form].elements[basename + i].checked = do_check;
}
}
return true;
}
</script>
</head>
<body>
<?php
$sn_plant = $_POST['sn_plant'];
$sn_sales = $_POST['sn_sales'];
?>
<form name="form1" action="" method="post">
<fieldset>
<legend>criteria</legend>
<table cellpadding="0" cellspacing="1">
<tr><th>Plant:</th>
<td>
<input type="text" name="sn_plant" value="<?php echo $sn_plant;?>" maxlength="12" />
<td>
</tr>
<tr><th>Sales:</th>
<td>
<input type="text" name="sn_sales" value="<?php echo $sn_sales;?>" maxlength="12" />
<input type="submit" name="search" value="Search" />
<td>
</tr>
</table>
</fieldset>
<?php
if($_POST['search']=='Search'){
$sn_sql = "SELECT sn_id,sn_plant,sn_sales FROM sn WHERE sn_plant='".$sn_plant."' AND sn_sales='".$sn_sales."' ";
$sn_res = mysql_query($sn_sql);
$sn_num = mysql_num_rows($sn_res);
if($sn_num==0){
echo '<font color="red">Record doesn/'t exsit.</font>';
}else{
echo'<fieldset><legend>list</legend>
<table border=1>
<tr><th>chk</th><th>id</th><th>plant</th><th>sales</th></tr>';
for($i=0; $i<$sn_num; $i++){
$sn_row= mysql_fetch_array($sn_res);
echo '<tr>';
echo '<td><input type="checkbox" name="sn['.$i.']" id="rows'.$i.'" value="'.$sn_row['sn_id'].'" ></td>';
echo '<td>'.$sn_row['sn_id'].'</td>';
echo '<td>'.$sn_row['sn_plant'].'</td>';
echo '<td>'.$sn_row['sn_sales'].'</td>';
echo '</tr>';
}
echo ' <input type="hidden" name="sn_plant" value="'.$sn_plant.'">
<input type="hidden" name="sn_sales" value="'.$sn_sales.'">
</table>
</fieldset>
<a href="#" οnclick="setCheckboxesRange(/'form1/', true, /'rows/', 0, '.$i.'); return false;">全选</a>/
<a href="#" οnclick="setCheckboxesRange(/'form1/', false, /'rows/', 0, '.$i.'); return false;">全不选</a>
<input type="submit" name="add" value="Add">';
}
echo '</form>';
}
if($_POST['add']=='Add'){
$sn = $_POST['sn'];
$sn_plant = $_POST['sn_plant'];
$sn_sales = $_POST['sn_sales'];
//if user didn't choose it ,it's value is null,otherwise it's value equals sn_id.
//for checkbox,what we need to do is give it an unique value ,then deal with it in array.
echo 'You have chosen following items:';
foreach($sn as $key => $value){
if($sn[$key] != ""){
echo $sn[$key].',';
}
}
}
?>
</body>
</html>