ส่งค่า Checkbox ผ่านฟอร์ม จะให้ฟอร์มที่รับค่า checkbox ได้หลายๆค่าเพื่อนำไปทำคำสั่ง sql ได้อย่างไรครับ
Code
CREATE TABLE color (
color_no int(2) NOT NULL default '0',
color_name varchar(10) NOT NULL default ''
) TYPE=MyISAM;
INSERT INTO color VALUES (1, 'สีดำ');
INSERT INTO color VALUES (13, 'สีครีม');
INSERT INTO color VALUES (2, 'สีขาว');
INSERT INTO color VALUES (3, 'สีฟ้า');
INSERT INTO color VALUES (4, 'สีชมพู');
INSERT INTO color VALUES (5, 'สีเหลือง');
INSERT INTO color VALUES (6, 'สีเขียว');
INSERT INTO color VALUES (7, 'สีน้ำตาล');
INSERT INTO color VALUES (8, 'สีส้ม');
INSERT INTO color VALUES (9, 'สีน้ำเงิน');
INSERT INTO color VALUES (10, 'สีแดง');
INSERT INTO color VALUES (11, 'สีเทา');
INSERT INTO color VALUES (12, 'สีม่วง');
Sample3.php
<html>
<head>
<body bgcolor="#FFFFFF">
<form name="form1" method="post" action="sample4.php">
<?
$host="localhost";
$username="";
$pass_word="";
$db="mydatabase";
$tb="color";
mysql_connect( $host,$username,$pass_word) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");
$sql="Select * From $tb order by color_no asc";
$db_query=mysql_db_query($db,$sql);
$num_rows=mysql_num_rows($db_query); /* นับ Reccord ที่พบ */
?>
<table width="24%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td width="24%">รหัสสี</td>
<td width="52%">รายละเอียดสี</td>
<td width="24%">เลือก</td>
</tr>
</table>
<?
$a=0;
while($a < $num_rows)
{
$result = mysql_fetch_array($db_query);
$color_no=$result[color_no];
$color_name=$result[color_name];
?>
<table width="24%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td width="24%">
<?echo $color_no;?>
</td>
<td width="52%">
<?echo $color_name;?>
</td>
<td width="24%">
<input type="checkbox" name="color_array[]" value="<?echo "$color_no"; ?>">
</td>
</tr>
</table>
<?
$a++;
}
?>
<br>
<input type="submit" name="Submit" value="ตรวจสอบ">
</form>
</body>
</html>
Sample4.php
<html>
<head>
<body bgcolor="#FFFFFF">
<?
$host="localhost";
$username="";
$pass_word="";
$db="mydatabase";
$tb="color";
mysql_connect( $host,$username,$pass_word) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");
for ($i=0;$i<count($color_array);$i++) {
$color_no = $color_array[$i];
$sql="Select * From $tb where color_no=$color_no";
$db_query=mysql_db_query($db,$sql);
$result = mysql_fetch_array($db_query);
$color_no=$result[color_no];
$color_name=$result[color_name];
echo "$color_no \t $color_name<br>";
}
?>
</body>
</html>
Date :
2009-04-25 18:53:48
By :
webmaster
Code (form.php)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<script language="JavaScript">
function onSave()
{
if(confirm('Do you want to save ?')==true)
{
return true;
}
else
{
return false;
}
}
</script>
<form name="frmMain" action="save.php" method="post" OnSubmit="return onSave();">
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
<th width="30"> <div align="center">Select </div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
<td align="center"><input type="checkbox" name="chkEmail[]" value="<?=$objResult["Email"];?>"></td>
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
<input type="submit" name="btnSave" value="Save">
</form>
</body>
</html>
Code (save.php)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
for($i=0;$i<count($_POST["chkEmail"]);$i++)
{
if($_POST["chkEmail"][$i] != "")
{
$strSQL = "INSERT INTO table_name (Mail) VALUES ('".$_POST["chkEmail"][$i]."')";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
}
}
echo "Record Insert.";
mysql_close($objConnect);
?>
</body>
</html>
Go to : PHP Multiple Checkbox
Date :
2011-09-18 17:17:11
By :
thaicreate
Load balance : Server 03