Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Handling Form Data | Working with Forms
Quizzes & Challenges
Quizzes
Challenges
/
PHP Core Concepts

bookHandling Form Data

When you build a form in HTML and submit it to a PHP script, the data from each form field is sent to the server. PHP provides special variables called superglobals to access this data. The two most common are $_GET and $_POST.

index.php

index.php

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445
<?php // Check if the form has been submitted using GET or POST $method = $_SERVER["REQUEST_METHOD"]; $name = ""; $email = ""; if ($method === "POST") { // Access form fields sent via POST $name = isset($_POST["name"]) ? $_POST["name"] : ""; $email = isset($_POST["email"]) ? $_POST["email"] : ""; } elseif ($method === "GET" && isset($_GET["name"])) { // Access form fields sent via GET $name = isset($_GET["name"]) ? $_GET["name"] : ""; $email = isset($_GET["email"]) ? $_GET["email"] : ""; } ?> <!DOCTYPE html> <html> <head> <title>Handle Form Data</title> </head> <body> <h2>Submit your information</h2> <form method="post" action=""> <label>Name: <input type="text" name="name" /></label><br /> <label>Email: <input type="email" name="email" /></label><br /> <input type="submit" value="Submit via POST" /> </form> <form method="get" action=""> <label>Name: <input type="text" name="name" /></label><br /> <label>Email: <input type="email" name="email" /></label><br /> <input type="submit" value="Submit via GET" /> </form> <?php if ($name || $email): ?> <h3>Form Data Received:</h3> <p>Name: <?php echo htmlspecialchars($name); ?></p> <p>Email: <?php echo htmlspecialchars($email); ?></p> <p>Submitted using: <?php echo $method; ?></p> <?php endif; ?> </body> </html>

Form fields are mapped to PHP variables in the superglobals using the name attribute of each input field. For example, if your form has <input type="text" name="username">, then after submission, you can access the value with $_POST["username"] or $_GET["username"], depending on the form's method.

Note
Note

To safely access form data, always check if the expected key exists in the superglobal array using isset() before using it. This prevents errors if a field is missing; also, use htmlspecialchars() when displaying form data to avoid unwanted HTML being rendered on your page.

question mark

Which PHP superglobal is used to access data sent via a POST form?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookHandling Form Data

Svep för att visa menyn

When you build a form in HTML and submit it to a PHP script, the data from each form field is sent to the server. PHP provides special variables called superglobals to access this data. The two most common are $_GET and $_POST.

index.php

index.php

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445
<?php // Check if the form has been submitted using GET or POST $method = $_SERVER["REQUEST_METHOD"]; $name = ""; $email = ""; if ($method === "POST") { // Access form fields sent via POST $name = isset($_POST["name"]) ? $_POST["name"] : ""; $email = isset($_POST["email"]) ? $_POST["email"] : ""; } elseif ($method === "GET" && isset($_GET["name"])) { // Access form fields sent via GET $name = isset($_GET["name"]) ? $_GET["name"] : ""; $email = isset($_GET["email"]) ? $_GET["email"] : ""; } ?> <!DOCTYPE html> <html> <head> <title>Handle Form Data</title> </head> <body> <h2>Submit your information</h2> <form method="post" action=""> <label>Name: <input type="text" name="name" /></label><br /> <label>Email: <input type="email" name="email" /></label><br /> <input type="submit" value="Submit via POST" /> </form> <form method="get" action=""> <label>Name: <input type="text" name="name" /></label><br /> <label>Email: <input type="email" name="email" /></label><br /> <input type="submit" value="Submit via GET" /> </form> <?php if ($name || $email): ?> <h3>Form Data Received:</h3> <p>Name: <?php echo htmlspecialchars($name); ?></p> <p>Email: <?php echo htmlspecialchars($email); ?></p> <p>Submitted using: <?php echo $method; ?></p> <?php endif; ?> </body> </html>

Form fields are mapped to PHP variables in the superglobals using the name attribute of each input field. For example, if your form has <input type="text" name="username">, then after submission, you can access the value with $_POST["username"] or $_GET["username"], depending on the form's method.

Note
Note

To safely access form data, always check if the expected key exists in the superglobal array using isset() before using it. This prevents errors if a field is missing; also, use htmlspecialchars() when displaying form data to avoid unwanted HTML being rendered on your page.

question mark

Which PHP superglobal is used to access data sent via a POST form?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3
some-alt