|
|
|
สอบถามเรื่อง checkbox ครับ กับการบันทึกใน Mysql ขอถามทุกท่านที่มีความรู้หน่อยนะครับ |
|
|
|
|
|
|
|
จริงๆ ใช้ int นั่นแหล่ะครับ
ก็แก้ไขได้ปกติ นะครับ
ลองเอาโค้ด มาโพสต์ดูสิครับ
|
|
|
|
|
Date :
2010-03-21 16:11:44 |
By :
yomaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
รายละเอียดนะครับ
table name other
field name 1. no(int) primary Key auto number
field name 2. a1(int)
form input
<input type="checkbox" name="a1" id="a1" value="1"/><label for="a1"> • A1</label>
insert.php
$sqlinsert = "insert into other values($a1);
mysql_query($sqlinsert) or die ("คุณเพิ่มข้่อมูล table ไม่สมบูรณ์");
code form Edit ครับ
<form name"edit" action="edit.php">
$sqledit="select * from other where no=$no";
$result=mysql_query($sqledit);
$arrEdit=mysql_fetch_array($result);
if ( $arrEdit[1] == 1) {
echo"<input type=\"checkbox\" name=\"a1\" value=\"1\" checked=\"checked\" id=\"a1\" /><label for=\"a1\"> • A1</label>"; }
else if ( $arrEdit[1] == 0) {
echo"<input type=\"checkbox\" name=\"a1\" value=\"0\" id=\"a1\" /><label for=\"a1\"> • A1</label>"; }
</form>
code Edit ครับ
edit.php
$edit="update other set a1=$a1 where no=$no;
ปัญหาผมอยู่ตรงที่ form Edit.php ครับ ผมอยากให้ 1=check , 0=uncheck ครับ
|
|
|
|
|
Date :
2010-03-22 11:15:35 |
By :
tongmew02 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่มีใครตอบเลยครับ
|
|
|
|
|
Date :
2010-03-23 07:29:21 |
By :
tongmew02 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
รบกวนหน่อยครับ
|
|
|
|
|
Date :
2010-03-23 17:06:27 |
By :
tongmew02 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
โดนปกติ value ของ checkbox คือ checked value ซึ่งก็คือ value เมื่อมีการ checked
หมายความว่า ถ้าคุณกำหนด value="1" เมื่อคุณติ๊กเลือก ก็จะได้ 1 ถ้าไม่เลือก ก็จะไม่มีค่าใด ๆ ค่ะ
|
|
|
|
|
Date :
2010-03-23 17:21:13 |
By :
ultrasiam |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เก็บค่าลงเป็น 1 กับ 0 เฉยๆแทนได้ไหมครับ
ลงในฐานข้อมูลนะ
|
|
|
|
|
Date :
2010-03-23 17:24:35 |
By :
teerapuch |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เก็บค่าเป็น 1 กับ 0 ก็ได้ครับ
|
|
|
|
|
Date :
2010-03-24 17:31:51 |
By :
tongmew02 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2010-03-31 17:07:10 |
By :
tongmew02 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-08-11 22:12:26 |
By :
thaicreate |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|