|
|
|
php -- ต้องการเก็บจ้อมูลเฉพาะที่ Checkbox และเลือกข้อมูลที่ dropdown ที่เลือก checkbox เท่านั้น |
|
|
|
|
|
|
|
php -- ต้องการเก็บจ้อมูลเฉพาะที่ Checkbox และเลือกข้อมูลที่ dropdown ที่เลือก checkbox เท่านั้น สามารถเก็บข้อมูล Checkbox ที่เลือก แต่ dropdown กลับเก็บค่าแรก ไม่เก็บค่าตามที่เลือก checkbox
Code
<div id="page-wrapper" >
<div id="page-inner">
<div class="row"></div>
<!-- /. ROW -->
<div class="row"></div>
<!-- /. ROW -->
<div class="row"></div>
<!-- /. ROW -->
<div class="row">
<form name="form1" method="post" action="save_course_class.php" onSubmit="JavaScript:return fncSubmit();">
<div class="col-md-9 col-sm-12 col-xs-12" align="center">
<div class="panel panel-default" align="center">
<div class="panel-heading">เพิ่มรายวิชา</div>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td colspan="2"><center><label>
<select class="form-control" name="Class_ID" OnChange="window.location='?item='+this.value;">
<option value=""> -- กรุณาเลือกปีการศึกษา -- </option>
<?php
$strSQL = "SELECT * FROM class_student";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery))
{
if($_GET["item"] == $objResult["Class_ID"])
{
$sel = "selected";
}
else
{
$sel = "";
}
?>
<option value="<?php echo $objResult["Class_ID"];?>" <?php echo $sel;?>><?php echo $objResult["Class_name"];?></option>
<?php
}
?>
</select>
</label></center></td>
</tr>
<tr>
<td width="437"> <div class="form-group" align="center">
<label>ห้องเรียน : </label></div></td>
<td width="437"><label>
<select class="form-control" name="classroom_id" >
<option value=""><-- Please Select Item --></option>
<?php
$sql = mysql_query("SELECT * FROM classroom_student");
while ($row = mysql_fetch_array($sql)){
echo "<option name=". $row['classroom_id']." value=". $row['classroom_id'] .">" . $row['classroom_name'] . "</option>";
}
?>
</select>
</label> </td></tr>
<tr>
<td width="437"> <div class="form-group" align="center">
<label>ปีการศึกษา : </label></div></td>
<td width="437"><label>
<select class="form-control" name="year_id" >
<?php
$sql = mysql_query("SELECT * FROM year ORDER BY year_id DESC");
while ($row = mysql_fetch_array($sql)){
echo "<option name=". $row['year_id']." value=". $row['year_id'] .">พ.ศ. " . $row['year_BE'] . "</option>";
}
?>
</select>
</label> </td></tr>
</thead>
</table>
<div class="panel panel-default">
<div class="panel-heading">
ข้อมูลรายวิชา </div>
<div class="panel-body">
<div class="table-responsive">
<table width="26%" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th width="17%">รหัสรายวิชา</th>
<th width="33%">ชื่อรายวิชา</th>
<th width="17%">ประจำชั้น</th>
<th width="12%"></th>
<th width="21%"></th>
</tr>
</thead>
<tbody>
<?php
if($_GET["item"] != "")
{
$strSQL = "SELECT distinct course.course_id, course.course_name, class_student.Class_name FROM course, class_student
WHERE course.Class_ID='".$_GET["item"]."'
AND course.Class_ID=class_student.Class_ID";
$objQuery = mysql_query($strSQL);
$item = $_GET["item"];
while ($row = mysql_fetch_array($objQuery)){
echo "<tr><td>". $row['course_id'] ."</td>
<td>". $row['course_name'] ."</td>
<td>". $row['Class_name'] ."</td>
<td><center><input type='checkbox' name='course_id[]' value='". $row['course_id'] ."' /></center></td>
<td><select class='form-control' name='UserID_teacher[". $row1['UserID_teacher']."]' ><option value='0'>-- กรุณาเลือก --</option>";
$sql = mysql_query("SELECT * FROM member_teacher");
while ($row1 = mysql_fetch_array($sql)){
echo "<option name='UserID_teacher[". $row1['UserID_teacher']."]' value=". $row1['UserID_teacher'].">" . $row1['TeacherName'] . "</option>";
}
echo "</select></td></tr>";
}
}
else {
$strSQL = "SELECT distinct course.course_id, course.course_name, class_student.Class_name FROM course, class_student
WHERE course.Class_ID=class_student.Class_ID";
$objQuery = mysql_query($strSQL);
$item = $_GET["item"];
while ($row = mysql_fetch_array($objQuery)){
echo "<tr><td>". $row['course_id'] ."</td>
<td>". $row['course_name'] ."</td>
<td>". $row['Class_name'] ."</td>
<td></td>
<td></td>
</tr>";
}
}
?>
<tr><td colspan="5"><div align="center">
<button type="button" class="btn btn-primary" onClick="history.back();">Back Button</button>
<button type="submit" class="btn btn-primary">Save Button</button>
</div></td></tr>
</tbody>
</table>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- /. ROW -->
<div class="row" ></div>
<!-- /. ROW -->
<div class="row"></div>
<!-- /. ROW -->
</div>
<!-- /. PAGE INNER -->
<!-- /. PAGE WRAPPER -->
<!-- /. WRAPPER -->
<!-- SCRIPTS -AT THE BOTOM TO REDUCE THE LOAD TIME-->
<!-- JQUERY SCRIPTS -->
<script src="assets/js/jquery-1.10.2.js"></script>
<!-- BOOTSTRAP SCRIPTS -->
<script src="assets/js/bootstrap.min.js"></script>
<!-- METISMENU SCRIPTS -->
<script src="assets/js/jquery.metisMenu.js"></script>
<!-- MORRIS CHART SCRIPTS -->
<script src="assets/js/morris/raphael-2.1.0.min.js"></script>
<script src="assets/js/morris/morris.js"></script>
<!-- CUSTOM SCRIPTS -->
<script src="assets/js/custom.js"></script>
</body>
</html>
save_course_class.php
<?
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("plookpanya");
for($i=0;$i<count($_POST["course_id"]);$i++)
{
if($_POST["course_id"][$i] != "")
{
$strSQL = "INSERT INTO course_class (course_class_id, Class_ID, classroom_id, year_id, course_id, UserID_teacher) VALUES (NULL, '".trim($_POST['Class_ID'])."', '".trim($_POST['classroom_id'])."', '".trim($_POST['year_id'])."','".$_POST["course_id"][$i]."', '".$_POST["UserID_teacher"][$i]."')";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
}
}
//echo "Record Insert.";
echo '<script language="javascript">alert("ADD Data Completed!");</script>';
echo "<meta http-equiv='refresh' content='0;url=add_course_class.php'>";
mysql_close($objConnect);
?>
Tag : PHP, MySQL, HTML
|
ประวัติการแก้ไข 2019-02-11 09:49:29 2019-02-11 09:50:41
|
|
|
|
|
Date :
2019-02-11 09:46:11 |
By :
aummyasia |
View :
1002 |
Reply :
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มีใครพอแนะนำบ้างคะ
|
|
|
|
|
Date :
2019-02-23 09:03:35 |
By :
aummyasia |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อยากจะช่วย แต่อ่านโค๊ดดำๆ แล้วปวด หัว ก๊อปไปทดสอบก็ยาก
|
|
|
|
|
Date :
2019-02-23 12:21:25 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<div id="page-wrapper" >
<div id="page-inner">
<div class="row"></div>
<!-- /. ROW -->
<div class="row"></div>
<!-- /. ROW -->
<div class="row"></div>
<!-- /. ROW -->
<div class="row">
<form name="form1" method="post" action="save_course_class.php" onSubmit="JavaScript:return fncSubmit();">
<div class="col-md-9 col-sm-12 col-xs-12" align="center">
<div class="panel panel-default" align="center">
<div class="panel-heading">เพิ่มรายวิชา</div>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td colspan="2"><center><label>
<select class="form-control" name="Class_ID" OnChange="window.location='?item='+this.value;">
<option value=""> -- กรุณาเลือกปีการศึกษา -- </option>
<?php
$strSQL = "SELECT * FROM class_student";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery))
{
if($_GET["item"] == $objResult["Class_ID"])
{
$sel = "selected";
}
else
{
$sel = "";
}
?>
<option value="<?php echo $objResult["Class_ID"];?>" <?php echo $sel;?>><?php echo $objResult["Class_name"];?></option>
<?php
}
?>
</select>
</label></center></td>
</tr>
<tr>
<td width="437"> <div class="form-group" align="center">
<label>ห้องเรียน : </label></div></td>
<td width="437"><label>
<select class="form-control" name="classroom_id" >
<option value=""><-- Please Select Item --></option>
<?php
$sql = mysql_query("SELECT * FROM classroom_student");
while ($row = mysql_fetch_array($sql)){
echo "<option name=". $row['classroom_id']." value=". $row['classroom_id'] .">" . $row['classroom_name'] . "</option>";
}
?>
</select>
</label> </td></tr>
<tr>
<td width="437"> <div class="form-group" align="center">
<label>ปีการศึกษา : </label></div></td>
<td width="437"><label>
<select class="form-control" name="year_id" >
<?php
$sql = mysql_query("SELECT * FROM year ORDER BY year_id DESC");
while ($row = mysql_fetch_array($sql)){
echo "<option name=". $row['year_id']." value=". $row['year_id'] .">พ.ศ. " . $row['year_BE'] . "</option>";
}
?>
</select>
</label> </td></tr>
</thead>
</table>
<div class="panel panel-default">
<div class="panel-heading">
ข้อมูลรายวิชา </div>
<div class="panel-body">
<div class="table-responsive">
<table width="26%" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th width="17%">รหัสรายวิชา</th>
<th width="33%">ชื่อรายวิชา</th>
<th width="17%">ประจำชั้น</th>
<th width="12%"></th>
<th width="21%"></th>
</tr>
</thead>
<tbody>
<?php
if($_GET["item"] != "")
{
$strSQL = "SELECT distinct course.course_id, course.course_name, class_student.Class_name FROM course, class_student
WHERE course.Class_ID='".$_GET["item"]."'
AND course.Class_ID=class_student.Class_ID";
$objQuery = mysql_query($strSQL);
$item = $_GET["item"];
while ($row = mysql_fetch_array($objQuery)){
echo "<tr><td>". $row['course_id'] ."</td>
<td>". $row['course_name'] ."</td>
<td>". $row['Class_name'] ."</td>
<td><center><input type='checkbox' name='course_id[]' value='". $row['course_id'] ."' /></center></td>
<td><select class='form-control' name='UserID_teacher[". $row1['UserID_teacher']."]' ><option value='0'>-- กรุณาเลือก --</option>";
$sql = mysql_query("SELECT * FROM member_teacher");
while ($row1 = mysql_fetch_array($sql)){
echo "<option name='UserID_teacher[". $row1['UserID_teacher']."]' value=". $row1['UserID_teacher'].">" . $row1['TeacherName'] . "</option>";
}
echo "</select></td></tr>";
}
}
else {
$strSQL = "SELECT distinct course.course_id, course.course_name, class_student.Class_name FROM course, class_student
WHERE course.Class_ID=class_student.Class_ID";
$objQuery = mysql_query($strSQL);
$item = $_GET["item"];
while ($row = mysql_fetch_array($objQuery)){
echo "<tr><td>". $row['course_id'] ."</td>
<td>". $row['course_name'] ."</td>
<td>". $row['Class_name'] ."</td>
<td></td>
<td></td>
</tr>";
}
}
?>
<tr><td colspan="5"><div align="center">
<button type="button" class="btn btn-primary" onClick="history.back();">Back Button</button>
<button type="submit" class="btn btn-primary">Save Button</button>
</div></td></tr>
</tbody>
</table>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- /. ROW -->
<div class="row" ></div>
<!-- /. ROW -->
<div class="row"></div>
<!-- /. ROW -->
</div>
<!-- /. PAGE INNER -->
<!-- /. PAGE WRAPPER -->
<!-- /. WRAPPER -->
<!-- SCRIPTS -AT THE BOTOM TO REDUCE THE LOAD TIME-->
<!-- JQUERY SCRIPTS -->
<script src="assets/js/jquery-1.10.2.js"></script>
<!-- BOOTSTRAP SCRIPTS -->
<script src="assets/js/bootstrap.min.js"></script>
<!-- METISMENU SCRIPTS -->
<script src="assets/js/jquery.metisMenu.js"></script>
<!-- MORRIS CHART SCRIPTS -->
<script src="assets/js/morris/raphael-2.1.0.min.js"></script>
<script src="assets/js/morris/morris.js"></script>
<!-- CUSTOM SCRIPTS -->
<script src="assets/js/custom.js"></script>
</body>
</html>
|
|
|
|
|
Date :
2019-02-23 13:28:03 |
By :
aummyasia |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("plookpanya");
for($i=0;$i<count($_POST["course_id"]);$i++)
{
if($_POST["course_id"][$i] != "")
{
$strSQL = "INSERT INTO course_class (course_class_id, Class_ID, classroom_id, year_id, course_id, UserID_teacher) VALUES (NULL, '".trim($_POST['Class_ID'])."', '".trim($_POST['classroom_id'])."', '".trim($_POST['year_id'])."','".$_POST["course_id"][$i]."', '".$_POST["UserID_teacher"][$i]."')";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
}
}
//echo "Record Insert.";
echo '<script language="javascript">alert("ADD Data Completed!");</script>';
echo "<meta http-equiv='refresh' content='0;url=add_course_class.php'>";
mysql_close($objConnect);
?>
|
|
|
|
|
Date :
2019-02-23 13:28:33 |
By :
aummyasia |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<td><center><input type='checkbox' name='course_id[ตรงนี้ใส่idให้เหมือนกับข้างล่าง]' value='". $row['course_id'] ."' /></center></td>
<td><select class='form-control' name='UserID_teacher[". $row1['UserID_teacher']."]' ><option value='0'>-- กรุณาเลือก --</option>";
กำหนด index ให้เหมือนกันด้วยเวลา ตรวจสอบจะได้ อ้างอิงเหมือนกัน
Code (PHP)
foreach($_POST["UserID_teacher"] as $k=>$v){
if(isset($_POST['course_id'][$k])){ //checkbox ถ้าไม่ติ๊ก ข้อมูลมันไม่ส่งมาด้วย
// ใช้ข้อมูล อ้างอิง index $k ถ้ามีการติ๊ก จะมี data
}
}
checkbox ถ้าไม่ติ๊ก ข้อมูลมันไม่ส่งมาด้วย
ปล เปลี่ยนการใช้ for เป็น foreach ลองไปอ่านดูว่า การทำงานมันต่างกันอย่างไร จะได้ประโยชน์
ขีเกียจพิมพ์มันยาว 5555 หาอ่านเองดีกว่า
คำค้น PHP for vs foreach มีให้เพียบ
|
ประวัติการแก้ไข 2019-02-23 13:50:15
|
|
|
|
Date :
2019-02-23 13:47:47 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|