clone แล้วไม่สามารถส่งค่า array ที่clone ไปอีกหน้าได้ครับ แต่table ที่ไม่ได้clone สามารถส่งค่า array ได้ปกติ

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา clone แล้วไม่สามารถส่งค่า array ที่clone ไปอีกหน้าได้ครับ แต่table ที่ไม่ได้clone สามารถส่งค่า array ได้ปกติ

clone แล้วไม่สามารถส่งค่า array ที่clone ไปอีกหน้าได้ครับ แต่table ที่ไม่ได้clone สามารถส่งค่า array ได้ปกติ
อันนี้หน้าส่งค่า
<table id="myTbl" width="100%" border="0" cellspacing="0" cellpadding="0">
   <tr>
    <td align="left" valign="top">
        
<form id="form1" name="form1" method="post" action="lo.php">
 
 <div class="firstTr" >
<h3><font color="#09ba32">ประเภทอาหาร</font></h3>
 <table class="table table-hover"  width="100%" border="0" cellspacing="0" cellpadding="0">  
     <tr > 
        <td><input type="checkbox" name="res[]" id="res1[]" value="ก๋วยเตี๋ยว"/>
            <span class="style1"><img src="images/category/ก๋วยเตี๋ยว.png" width="25" height="25">ก๋วยเตี๋ยว</span></td>
        <td><input type="checkbox" name="res[]" id="res1[]" value="สุกี้ชาบู"/>
            <span class="style1"><img src="images/category/สุกี้ชาบู.png" width="25" height="25">สุกี้ ชาบู</span></td>
        <td><input type="checkbox" name="res[]" id="res1[]" value="ร้านกาแฟ"/>
            <span class="style1"><img src="images/category/ร้านกาแฟ.png" width="25" height="25">ร้านกาแฟ</span></td>
        <td><input type="checkbox" name="res[]" id="res1[]" value="หมูกะทะ"/>
            <span class="style1"><img src="images/category/หมูกะทะ.png" width="25" height="25">หมูกะทะ</span></td>
        <td><input type="checkbox" name="res[]" id="res1[]" value="สเต็ก"/>
            <span class="style1"><img src="images/category/สเต็ก.png" width="25" height="25">สเต็ก</span></td>
        <td><input type="checkbox" name="res[]" id="res1[]" value="อาหารทะเล"/>
            <span class="style1"><img src="images/category/อาหารทะเล.png" width="25" height="25">อาหารทะเล</span></td>
        <td><input type="checkbox" name="res[]" id="res1[]" value="เบเกอรี่เค้ก"/>
            <span class="style1"><img src="images/category/เบเกอรี่เค้ก.png" width="25" height="25">เบเกอรี่ เค้ก</span></td>

      </tr>
      <tr>
        <td><input type="checkbox" name="res[]" value="อาหารญี่ปุ่น"/>
            <span class="style1"><img src="images/category/อาหารญี่ปุ่น.png" width="25" height="25">อาหารญี่ปุ่น</span></td>      
        <td><input type="checkbox" name="res[]" value="อาหารอีสานร้านส้มตำ"/>
            <span class="style1"><img src="images/category/อาหารอีสานร้านส้มตำ.png" width="25" height="25">อาหารอีสาน ร้านส้มตำ</span></td>
        <td><input type="checkbox" name="res[]" value="ซูชิ"/>
            <span class="style1"><img src="images/category/ซูชิ.png" width="25" height="25">ซูชิ</span></td>
        <td><input type="checkbox" name="res[]" value="อาหารเช้า"/>
            <span class="style1"><img src="images/category/อาหารเช้า.png" width="25" height="25">อาหารเช้า</span></td>
        <td><input type="checkbox" name="res[]" value="บุฟเฟ่ต์"/>
            <span class="style1"><img src="images/category/บุฟเฟ่ต์.png" width="25" height="25">บุฟเฟ่ต์</span></td>
        <td><input type="checkbox" name="res[]" value="เนื้อย่างบาร์บีคิว"/>
            <span class="style1"><img src="images/category/เนื้อย่างบาร์บีคิว.png" width="25" height="25">เนื้อย่าง บาร์บีคิว</span></td>
        <td><input type="checkbox" name="res[]" value="พิซซ่า"/>
            <span class="style1"><img src="images/category/พิซซ่า.png" width="25" height="25">พิซซ่า</span></td>

      </tr>
        </table>
        </div>
            
  

</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
    <button class="btn btn-success" id="addRow" type="button">เพิ่มผู้ใช้</button>  
    &nbsp;
    <button class="btn btn-success" id="removeRow" type="button">ลบผู้ใช้คนล่าสุด</button>
    &nbsp;
    &nbsp;
    &nbsp; 
   
    </td>
    </tr>
</table>
 
 
    <table width="100%" border="0" cellspacing="0" cellpadding="0" 
  <tr>
    <td>  
   <div align="center"><input  class="btn btn-success" type="submit" name="Submit" id="Submit" value="ค้นหาข้อมูล" /></td></div> 
  </tr>
</table>
</form>



<br />
</div>     
<script type="text/javascript">
$(function(){
    
    $("#addRow").click(function(){
        // ส่วนของการ clone ข้อมูลด้วย jquery clone() ค่า true คือ
        // การกำหนดให้ ไม่ต้องมีการ ดึงข้อมูลจากค่าเดิมมาใช้งาน
        // รีเซ้ตเป็นค่าว่าง ถ้ามีข้อมูลอยู่แล้ว ทั้ง select หรือ input
        $(".firstTr:eq(0)").clone(true) 
        .find("input").attr("value","").end(true)
        .find("select").attr("value","").end(true)
        .appendTo($("#myTbl"));
    });
    $("#removeRow").click(function(){
        // // ส่วนสำหรับการลบ
        if($("#myTbl div").size()>1){ // จะลบรายการได้ อย่างน้อย ต้องมี 1 รายการ
            $("#myTbl div:last").remove(); // ลบรายการสุดท้าย
        }else{
            // เหลือ 1 รายการลบไม่ได้
            alert("ต้องมีรายการข้อมูลอย่างน้อย 1 รายการ");
        }
    }); 
    

});
</script>


อันนี้หน้ารับค่าข้อมูล พอกดเพิ่มผู้ใช้แล้ว เลือก ข้อมูลค่า ARRAy  ไม่ถูกส่งมาหน้านี้

    <?
  if ( isset( $_POST[res] ) )//รับค่า checkbox แบบ array
  {
   for($i=0;$i<count($_POST["res"]);$i++)
{
    $value[$i] = $_POST["res"][$i];
    
    echo $value[$i]."<br>";
}
}
?>



Jame Panuwat Suksamran 202.29.48.xxx 08-12-2016 16:46:01

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
คำสั่งนี้ หมายถึง clone() แล้วให้ล้างค่าของข้อมูล

        .find("input").attr("value","").end(true)  
        .find("select").attr("value","").end(true)  


ถ้าเป็น checkbox ไม่ต้องใส่ ตัดออกได้

        $(".firstTr:eq(0)").clone(true) 
        .appendTo($("#myTbl"));


อีกอย่างการกำหนด name ให้กับ checkbox สำหรับรายการ clone จะใช้ชื่อเดียวกันไม่ได้
เพราะพอ clone แล้วจะแยกข้อมูลไม่ออก ถ้ารายการเริ่มต้นเป็น res[]  รายการ clone ต้องเป็น res1[] เป็นต้น

ประมาณนี้

        var n = $(".firstTr").length;
        $(".firstTr:eq(0)").clone(true) 
        .find(":checkbox").attr("name","res"+n+"[]").end(true)
        .appendTo($("#myTbl"));


ninenik 1.46.165.xxx 08-12-2016
 ความคิดเห็นที่ 2
ลองเอาไปใส่แล้วครับ 

var n = $(".firstTr").length;  
$(".firstTr:eq(0)").clone(true)   
.find(":checkbox").attr("name","res"+n+"[]").end(true)  
.appendTo($("#myTbl"));  


ก็ค่าที่ถูกส่งมาก็ยังเป็นแค่ตารางที่ไม่ได้ clone ส่วนตารางที่ clone นั้นไม่มีค่าส่งมาอีกหน้าเลยครับ ต้องแก้ส่วนไหนอีก



Jame Panuwat Suksamran 182.53.118.xxx 08-12-2016 19:58
 ความคิดเห็นที่ 3
  อันนี้แนะนำต่อไม่ได้เหมือนกัน เช็คดีๆ ว่าค่าไม่ไปจริงไหม หรือแค่ แสดงค่าส่งไปไม่ถูก
เช็คที่ไฟล์รับค่า อาจจะใช้คำสั่ง ดูค่าที่ถูกส่งมาด้วยคำสั่ง

<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>


ninenik 1.46.165.xxx 08-12-2016
1






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ