อยากสอบถามเรื่องการ insert checkbox จาก Multiple fields ครับ
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา อยากสอบถามเรื่องการ insert checkbox จาก Multiple fields ครับ
อยากสอบถามเรื่องการ insert checkbox จาก Multiple fields ครับ
ฟอร์มที่ผมเขียน คือ Multiple fields สามารถกด + เพิ่ม input ไปได้เรื่อยๆ ถึง 10 fields
คราวนี้ input อื่นไม่มีปัญหา แต่เมื่อ check ที่ checkbox แล้ว โดย field แรก ผม check 1 และ 2
field ต่อมา check 3 และ 4 แล้วกดบันทึก
ข้อมูลที่เข้า db ควรจะเป็น
แต่กลับได้แบบนี้
คือไปอยู่แถวเดียวกันหมดเลยครับ ไม่ทราบว่าต้องมทำอย่างไรครับ ขอบคุณครับ
โค้ด form
1 2 3 4 5 6 7 8 9 10 11 | <div class = "row" > <input type= "checkbox" name= "txtTime_plan1[]" id= "txtTime_plan1" value= "1" > 1 <input type= "checkbox" name= "txtTime_plan2[]" id= "txtTime_plan2" value= "2" > 2 <input type= "checkbox" name= "txtTime_plan3[]" id= "txtTime_plan3" value= "3" > 3 <input type= "checkbox" name= "txtTime_plan4[]" id= "txtTime_plan4" value= "4" > 4 </div> |
โค้ด insert
1 2 3 4 5 6 7 8 9 10 11 12 | for ( $i =0; $i < count ( $_POST [ "txtindex_plan" ]); $i ++) { $ch1 = $_POST [ "txtTime_plan1" ][ $i ]; $ch2 = $_POST [ "txtTime_plan2" ][ $i ]; $ch3 = $_POST [ "txtTime_plan3" ][ $i ]; $ch4 = $_POST [ "txtTime_plan4" ][ $i ]; $sql_ac = "INSERT INTO activity(P_id, ac_desc, activity_results, p_of_time1, p_of_time2, p_of_time3, p_of_time4, p_budget, p_per, ac_budget) VALUES( '$P_id' , '".$_POST["txtindex_plan"][$i]."' , '".$_POST["txtactivity_results"][$i]."' , '".$ch1."' , '".$ch2."' , '".$ch3."' , '".$ch4."' , '".$_POST["txtbudget_plan"][$i]."' , '".$_POST["txtper_plan"][$i]."' , '".$_POST["txtac_budget_plan"][$i]."' )"; $result5 = mysqli_query( $con , $sql_ac ) or die ( "Error in query: $sql_ac " . mysqli_error()); } |

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ความคิดเห็นที่
1
ใส่ key ให้กับ ชื่อ array ของ checkbox แบบเจาะจงเข้าไป จะทำให้ค่าตอนอ้างอิงในการรับค่าถูกต้อง
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <div class = "row" > <input type= "checkbox" name= "txtTime_plan1[0]" id= "txtTime_plan1" value= "1" > 1 <input type= "checkbox" name= "txtTime_plan2[0]" id= "txtTime_plan2" value= "2" > 2 <input type= "checkbox" name= "txtTime_plan3[0]" id= "txtTime_plan3" value= "3" > 3 <input type= "checkbox" name= "txtTime_plan4[0]" id= "txtTime_plan4" value= "4" > 4 </div> <div class = "row" > <input type= "checkbox" name= "txtTime_plan1[1]" id= "txtTime_plan1" value= "1" > 1 <input type= "checkbox" name= "txtTime_plan2[1]" id= "txtTime_plan2" value= "2" > 2 <input type= "checkbox" name= "txtTime_plan3[1]" id= "txtTime_plan3" value= "3" > 3 <input type= "checkbox" name= "txtTime_plan4[1]" id= "txtTime_plan4" value= "4" > 4 </div> |
บทความแนะนำที่เกี่ยวข้อง | |
---|---|
แนวทางการส่งค่า จาก checkbox เพื่อบันทึกข้อมูลแบบ array ใน php | อ่าน 34,605 |

ขอบคุณทุกการสนับสนุน
![]()