Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Handling Form Data | Working with Forms
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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain the difference between $_GET and $_POST?

How do I choose which method to use for my form?

What happens if I don't set a name attribute on a form field?

bookHandling Form Data

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
some-alt