How can a default value be set in a form field in PHP to prepopulate the value when the form is loaded?
To set a default value in a form field in PHP, you can use a ternary operator to check if the field has been submitted. If it has not been submitted, you can set the default value. This way, when the form is loaded initially, the field will be prepopulated with the default value.
<form method="post">
<input type="text" name="field_name" value="<?php echo isset($_POST['field_name']) ? $_POST['field_name'] : 'Default Value'; ?>">
<input type="submit" value="Submit">
</form>
Keywords
Related Questions
- Are there any best practices for efficiently converting links into clickable links in PHP?
- What are the potential pitfalls of using <ul> elements for structuring content in PHP, and how can these be avoided for a more visually appealing display?
- What is the purpose of using flush() in PHP and how does it affect the output to the browser?