Handling 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
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.
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.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
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?
Awesome!
Completion rate improved to 5
Handling Form Data
Swipe um das Menü anzuzeigen
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
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.
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.
Danke für Ihr Feedback!