Site icon บริษัท โค๊ดบี จำกัด

สอน Laravel 5 : สร้าง html form ส่งข้อมูลแบบ POST

สอน-laravel-framework

สอน-laravel-framework

<h3>สอน Laravel 5 &colon; สร้าง html form ส่งข้อมูลแบบ POST<&sol;h3>&NewLine;<p><&sol;br><br &sol;>&NewLine;การสร้างแบบฟอร์มเพื่อรับข้อมูลแบบ POST จากผู้ใช้งานเว็บไซต์ และส่งข้อมูลนั้นไปยังส่วนของการบันทึกข้อมูล<br &sol;>&NewLine;หรือส่วนของ controller สำหรับใน Laravel framework มีขั้นตอนที่ไม่ได้ยุ่งยากอะไรมาก เราสามารถ<br &sol;>&NewLine;ทำได้ด้วยการสร้าง html form แบบทั่วไปและชี้ action ไปที่ controller method นั้น ๆ ผ่านการ<br &sol;>&NewLine;กำหนดที่ router ได้เลย<br &sol;>&NewLine;<&excl;--more--><&sol;p>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>สร้าง html form ส่งข้อมูลแบบ POST<&sol;h4>&NewLine;<p>สร้างไฟล์ <strong>register&period;blade&period;php<&sol;strong> วางไฟล์ไว้ที่ <strong>resources&sol;views&sol;register&period;blade&period;php<&sol;strong> เป็นการสร้างฟอร์มให้ผู้ใช้งานเว็บ<br &sol;>&NewLine;ทำการกรอกข้อมูล จากนั้นเมื่อมีการกดปุ่ม submit ข้อมูลจะถูกส่งไปที่ Router register แบบ POST<br &sol;>&NewLine;โดยในฟอร์มจะสังเกตุว่ามีตัวแปรอยู่ 2 ตัวคือตัวแปร name เป็นการรับค่า input text จากผู้ใช้งาน<br &sol;>&NewLine;ส่วนตัวแปร &lowbar;token เป็นการส่งตัวแปรตรวจสอบค่าเพื่อป้องกันการโจมตีแบบ <a href&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;web-security-การโจมตีเว็บไซต์รูปแ&sol;" target&equals;"&lowbar;blank">CSRF<&sol;a> ซึ่งเป็นคุณสมบัติที่ติด<br &sol;>&NewLine;มากับ Laravel framework<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"resources&sol;views&sol;register&period;blade&period;php">&lt&semi;body&gt&semi; &NewLine;&lt&semi;div class&equals;"container"&gt&semi; &NewLine; &lt&semi;div class&equals;"title"&gt&semi;Hello &lbrace;&lbrace;&lbrace; &dollar;name or '' &rcub;&rcub;&rcub;&lt&semi;&sol;div&gt&semi; &NewLine; &lt&semi;div class&equals;"content"&gt&semi; &NewLine; &lt&semi;form action&equals;"register" method&equals;"post"&gt&semi; &NewLine; &lt&semi;input type&equals;"hidden" name&equals;"&lowbar;token" value&equals;"&lt&semi;&quest;php echo csrf&lowbar;token&lpar;&rpar; &quest;&gt&semi;"&gt&semi; &NewLine; &lt&semi;input type&equals;"text" name&equals;"name" value&equals;""&gt&semi; &NewLine; &lt&semi;input type&equals;"submit" value&equals;"สมัครสมาชิก"&gt&semi; &NewLine; &lt&semi;&sol;form&gt&semi; &NewLine; &lt&semi;&sol;div&gt&semi; &NewLine;&lt&semi;&sol;div&gt&semi; &NewLine;&lt&semi;&sol;body&gt&semi;<&sol;pre>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>สร้าง Router Method ไว้รอรับข้อมูลแบบ POST<&sol;h4>&NewLine;<p>สร้าง Register Method ที่ Router ที่ไฟล์ <strong>app&sol;Http&sol;routes&period;php<&sol;strong> ไว้รอรับตัวแปรแบบ POST<br &sol;>&NewLine;จากนั้นใช้ Request คลาสเพื่อรับข้อมูลแบบ input post อย่าลืมทำการเรียกใช้งานตัว Request คลาส<br &sol;>&NewLine;ด้วยคำสั่ง <strong>use Illuminate&bsol;Http&bsol;Request<&sol;strong> <&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"app&sol;Http&sol;routes&period;php"> &NewLine;use Illuminate&bsol;Http&bsol;Request&semi; &sol;&sol; เรียกใช้งานคลาส Request &NewLine;Route&colon;&colon;get&lpar;'&sol;'&comma; function &lpar;&rpar; &lbrace; &NewLine; return view&lpar;'register'&rpar;&semi; &sol;&sol; โหลด register view แสดง html form ในครั้งแรก &NewLine;&rcub;&rpar;&semi; &NewLine;Route&colon;&colon;post&lpar;'register'&comma;function &lpar;Request &dollar;request&rpar; &lbrace; &NewLine;&Tab;&dollar;name &equals; &dollar;request-&gt&semi;input&lpar;'name'&rpar;&semi; &sol;&sol; รับตัวแปร name จาก html form &NewLine;&Tab;&dollar;data &equals; array&lpar; &NewLine;&Tab;&Tab;'name' &equals;&gt&semi; &dollar;name &NewLine;&Tab;&rpar;&semi; &NewLine; return view&lpar;"register"&comma;&dollar;data&rpar;&semi; &sol;&sol; return ค่าออกไปแสดงที่ register view &NewLine;&rcub;&rpar;&semi;<&sol;pre>&NewLine;<p>เราสามารถ return ค่าออกไปแสดงที่ view ได้ในรูปแบบของ array ดังนี้<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >&dollar;data &equals; array&lpar; &NewLine;&Tab;'name' &equals;&gt&semi; &dollar;name &NewLine;&rpar;&semi; &NewLine;return view&lpar;"register"&comma;&dollar;data&rpar;&semi;<&sol;pre>&NewLine;<p>และเมื่ออยู่ที่ view เราสามารถแสดงค่าตัวแปร name โดยใช้ syntax ของ <strong>Blade template<&sol;strong> ได้ดังนี้<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >&lt&semi;div class&equals;"title"&gt&semi;Hello &lbrace;&lbrace;&lbrace; &dollar;name or '' &rcub;&rcub;&rcub;&lt&semi;&sol;div&gt&semi;<&sol;pre>&NewLine;<p>หรือจะเขียนโดยใช้ php echo ธรรมดาแบบนี้ก็ได้ <&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >&lt&semi;div class&equals;"title"&gt&semi;Hello &lt&semi;&quest;php if&lpar;isset&lpar;&dollar;name&rpar;&rpar; echo &dollar;name&semi; &quest;&gt&semi;&lt&semi;&sol;div&gt&semi;<&sol;pre>&NewLine;<p>จากตัวอย่างด้านบนเป็นการส่งค่า POST Data จาก html form view มาที่ Router Method และ<br &sol;>&NewLine;ทำการ return ค่าออกไปแสดงที่ register view ทันที แต่ในการทำงานจริงเราจะไม่ใช้วิธีรับค่าโดยตรง<br &sol;>&NewLine;ผ่าน Router แต่จะเป็นการชี้ collback function ไปที่ controller และรับค่าตัวแปรทั้งหมดใน<br &sol;>&NewLine;controller นั้น ๆ แทน หลังจากได้ตัวแปรทั้งหมดเราจะทำงานให้เสร็จภายใน controller นั้น ๆ เสียก่อน<br &sol;>&NewLine;เช่นอาจจะบันทึกข้อมูลลงฐานข้อมูลหรือประมวลผลในรูปแบบอื่น ๆ ก่อน เสร็จแล้วค่อย คืนค่ากลับไปที่ view<br &sol;>&NewLine;ในบทความต่อไป เราจะมาทดสอบการทำงานรวมกันระหว่าง view -> router -> controller<br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;

Exit mobile version