0Pricing
Flask Academy · บทเรียน

สร้างแบบฟอร์ม HTML ที่ส่งข้อมูล

สร้างแบบฟอร์มที่ action ชี้ไปยังเส้นทาง

สร้างแบบฟอร์ม HTML ที่ส่งข้อมูล เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Forms Matter

An HTML form is how a browser collects typed input and hands it to your server. It is the front door for almost every user action. 📝

The form Element

Everything starts with a form tag. The browser bundles whatever fields live inside it and sends them together when the user submits.

<form>
  <!-- fields go here -->
</form>

Point It With action

The action attribute names the URL that receives the data. Leave it empty to post back to the same page the form lives on.

<form action="/signup">

Choose the method

Set method to post so the values travel in the request body, not the URL. That keeps form data tidy and out of browser history.

<form action="/signup" method="post">

Name Every Field

Each input needs a name attribute. That name becomes the key Flask uses to find the value on the server side.

<input type="text" name="username">

Add a Submit Button

A submit button triggers the send. Clicking it tells the browser to gather the named fields and fire the request to your action URL.

<button type="submit">Sign up</button>

Match the Route Methods

The receiving route must allow POST. List it in methods, or Flask answers with a 405 and your form data never lands.

@app.route("/signup", methods=["GET", "POST"])
def signup():
    ...

Read Values From request.form

On the server, posted fields live in request.form, a dict-like object keyed by each input name you defined in the HTML.

username = request.form["username"]

Serve the Form on GET

When the request is a GET, just render the page that shows the empty form so users have something to fill out.

if request.method == "GET":
    return render_template("signup.html")

Handle the POST Branch

When the method is POST, read the fields and act on them. One view can both show the form and process the submission.

if request.method == "POST":
    name = request.form["username"]

A Complete Mini Form

Put it together: a form that posts to /signup and a view that greets the user. That is the full round trip in a few lines.

@app.route("/signup", methods=["GET", "POST"])
def signup():
    if request.method == "POST":
        return "Hi " + request.form["username"]
    return render_template("signup.html")

Quick Check

Let us confirm what makes a form actually post.

Recap: Posting Forms

You built a form with an action and post method, named your fields, allowed POST on the route, and read values from request.form. 🎉

คำถามที่พบบ่อย

บทเรียน “สร้างแบบฟอร์ม HTML ที่ส่งข้อมูล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “สร้างแบบฟอร์ม HTML ที่ส่งข้อมูล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “สร้างแบบฟอร์ม HTML ที่ส่งข้อมูล”

สร้างแบบฟอร์มที่ action ชี้ไปยังเส้นทาง คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “สร้างแบบฟอร์ม HTML ที่ส่งข้อมูล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม

ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. สร้างแบบฟอร์ม HTML ที่ส่งข้อมูล
  2. อ่านและกำหนดค่าเริ่มต้นพารามิเตอร์คำค้น
  3. ตรวจสอบฟิลด์ที่จำเป็นด้วยตนเอง
  4. เปลี่ยนเส้นทางหลัง POST (รูปแบบ PRG)
← กลับไปที่ Flask Academy