จะเอาค่า GetContents(editorObj) จาก ckeditor มาใส่ตัวแปร php อย่างไร
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา จะเอาค่า GetContents(editorObj) จาก ckeditor มาใส่ตัวแปร php อย่างไร
1 | < button onclick = "alert(GetContents(editorObj));" >แสดงข้อมูลใน CKEditor</ button > |

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ผมไล่มาเป็นวันแล้ว..ใครก็ได้ช่วยหน่อนะครับ
จะเอาค่าจากโคดข้างบนมาใส่ในตัวแปร PHP เขียนยังไง
ขอบคุณครับ..(เว็บนี้ไม่มีผิดหวัง )

ไฟล์ test.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> <title></title> </head> <body> <SCRIPT language=javascript> function formCheck(){ alert(GetContents(editorObj)); } </script> <form name= "editor_f" method= "post" action= "test.php" enctype= "multipart/form-data" > <script type= "text/javascript" src= "cke_func.js" ></script> <?php include_once ( "ckeditor/ckeditor.php" ); include_once ( "cke_config.php" ); $initialValue = '<p>This is some <strong>sample text</strong>.</p>' ; // ค่าเริ่มต้น กรณีดึงข้อมูลมาแก้ไข $CKEditor = new CKEditor(); // คืนค่าสำหรับใช้งานร่วมกับ javascript $events [ 'instanceReady' ] = ' function (evt) { return editorObj=evt.editor; }'; // บรรทัดด้านล่าง เปรียบได้กับการสร้าง textarea ชื่อ editor1 // ตัวแปรรับค่า เป็น $_POST['editor1'] หรือ $_GET['editor1'] ตามแต่กรณี $CKEditor ->editor( "editor1" , $initialValue , $config , $events ); ?> <br /> <input type= "submit" name= "sub" value= "Submit" /> <button onclick= "InsertHTML('<p>แทรก HTML</p>',editorObj);" >แทรก HTML</button> <button onclick= "SetContents('<p>แทนที่ข้อความทั้งหมด</p>',editorObj);" >แทนที่ข้อความทั้งหมด</button> <button onclick= "alert(GetContents(editorObj));" >แสดงข้อมูลใน CKEditor</button> <?php $txt_html = "<script>GetContents(editorObj);</script>" ; ?> </form> </body> </html> |

ค่าใน ckeditor ก็คือค่าจากตัวแปรชื่อ editor1 หรือฃื่อที่กำหนด
ถ้าส่งเป็น post ค่าก็จะอยู่ในตัวแปร $_POST['editor1']
ถ้าส่งเป็น get ค่าก็จะอยู่ในตัวแปร $_GET['editor1']
ส่วนที่ถามว่า เอาค่าจาก ckeditor มาใส่ตัวแปร php
ถามต่อว่า ถ้าได้ค่ามาอยู่ในตัวแปร จะทำอะไรต่อกับตัวแปร php

ผมจะเอาลง text file ครับ
จะเอาค่าจาก alert(GetContents(editorObj)); ลงใน html.txt ครับ
พี่ลองเขียนตัวอย่างให้หน่อย..ผม
1 2 3 4 5 6 | <?php // การเขียนข้อมูลสู่ Text File $f = fopen ( "ตำแหน่งของ File" , "w" ); fputs ( $f , ข้อมูลที่จะเขียนลง File); fclose( $f ); ?> |

ค่าที่อยู่ในคำสั่ง alert(GetContents(editorObj))
ก็คือค่าเดียวกันกับค่าที่อยู่ใน ckeditor
ถ้าที่จะทำเป็น เป็นการ submit ข้อมูลไป โดยการกดปุ่ม Submit แล้วให้เอาค่าจาก
ค่าที่อยู่ใน ckeditor ( ค่าเดียวกันกับ ค่าที่อยู่ในคำสั่ง alert(GetContents(editorObj)) )
ก็ไม่จำเป็นต้องใช้ javascript หรือคำสั่ง GetContents(editorObj)
ให้เอาค่าจากชื่อ ckeditor ที่เรากำหนดไปใช้ได้ เพราะเป็นค่าเดียวกัน
จากที่จะเขียนไฟล์ พอกดปุ่ม submit รูปแบบโค้ดก็จะได้เป็น
1 2 3 4 5 6 | <?php // การเขียนข้อมูลสู่ Text File $f = fopen ( "ตำแหน่งของ File" , "w" ); fputs ( $f , $_POST [ 'editor1' ]); fclose( $f ); ?> |
แต่ถ้าที่จะทำ ไม่ใช้การกดปุ่ม submit เพื่อส่งค่าข้อมูล เป็นเพียงการกดปุมคล้ายปุ่ม
<button onclick="alert(GetContents(editorObj));">แสดงข้อมูลใน CKEditor</button>
แล้วให้เข้าข้อมูลไปบันทึก อันนี้คงต้องใช้ ajax แทน เช่น
ตัวอย่างไฟล์ทดสอบ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <button id= "mybutton1" >แสดงข้อมูลใน CKEditor</button> <script type= "text/javascript" > $( function (){ $( "#mybutton1" ).click( function (){ var data=GetContents(editorObj); $.post( "write_file.php" ,{ editor1:data }, function (){ alert( "OK Complete." ); }); }); // jQuery code }); </script> |
ตัวอย่างไฟล์เขียน write_file.php
1 2 3 4 5 6 7 8 | <?php if (isset( $_POST [ 'editor1' ])){ // การเขียนข้อมูลสู่ Text File $f = fopen ( "ตำแหน่งของ File" , "w" ); fputs ( $f , $_POST [ 'editor1' ]); fclose( $f ); } ?> |

สวัสดีครับคุณนิค
ส่งใสไปไล่โคดมากเกินเลยจำผิดๆ ทำให้ งง ไปเป็นวัน ผมไปใช้
1 2 3 4 | if ( get_magic_quotes_gpc() ) $postedValue = htmlspecialchars( stripslashes ( $value ) ) ; else $postedValue = htmlspecialchars( $value ) ; |
เห็นมันออก <p>
This is some <strong>sample text</strong>. You are using <a href="http://ckeditor.com/">CKEditor</a>.</p>
พอมาไล่ทำใหม่เริ่มใหม่หมด ถึงได้
1 2 | < p > This is some < strong >sample text</ strong >. You are using < a href = "http://ckeditor.com/" >CKEditor</ a >.</ p > |
แต่มีข้อส่งสัยนิสหนึ่งเรื่องลง text file ค่าที่ได้มันไม่เหมือนกับที่ได้ตอนเอาลงฐานข้อมูล
ค่าใน txt มันจะมี ก่อน " ทุกที่เลยเท่าที่สังเกตุ ผมจึงเขียนฟังชั่น replace ค้นหา " แทนค่า "
1 2 | < p > < img alt = "indecision" src = "http://localhost/ajaxckeditor/ckeditor/plugins/smiley/images/whatchutalkingabout_smile.gif" title = "indecision" />Th< span style = "color: rgb(255, 0, 0);" >is is som</ span >e < strong >sample text</ strong >.</ p > |
แต่ค่าในฐานข้อมมูลมันปกติดี
1 2 | < p > < img alt = "indecision" src = "http://localhost/ajaxckeditor/ckeditor/plugins/smiley/images/whatchutalkingabout_smile.gif" title = "indecision" />Th< span style = "color: rgb(255, 0, 0);" >is is som</ span >e < strong >sample text</ strong >.</ p > |
เลยไม่เข้าใจทำมัยมันถึงมี ก่อน " ใน text file
วิธีเขียนก็เขียนตามที่พี่บอก
ขอบคุณครับ เว็บนี้มีสิ่งที่ดีๆมีประโยชน์ต่อผมมาก

สังเกตรูปนะครับ แบล็คสแลช มีใน text
