How can default values be set for form inputs in PHP?
To set default values for form inputs in PHP, you can use the ternary operator to check if the input has been submitted. If it has not been submitted, you can set a default value for the input. This ensures that the form inputs will have a predefined value when the form is first loaded.
<?php
// Set default values for form inputs
$name = isset($_POST['name']) ? $_POST['name'] : 'John Doe';
$email = isset($_POST['email']) ? $_POST['email'] : 'example@example.com';
?>
<form method="post" action="">
<input type="text" name="name" value="<?php echo $name; ?>">
<input type="email" name="email" value="<?php echo $email; ?>">
<button type="submit">Submit</button>
</form>
Keywords
Related Questions
- What are the potential pitfalls of using static methods in PHP classes, as seen in the forum thread?
- What potential pitfalls should be considered when using callbacks in PHP methods like UserRepository::attachAfterPersistHandler()?
- What best practices should be followed when handling email headers, content types, and encoding in PHP scripts to ensure successful email delivery?