Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/PHP - default input value

I have a post php form and a set of inputs:

  1. Your Name
  2. Your Last Name
  3. My Name

Every input looks the same, only the names change:

<input type="text" name="your_name" value="<?php echo get_option('your_name'); ?>" />

How to set default values when value= is not available?

[edit]

Ok, so, normally I'd do something like:

<input type="text" name="your_name" value="Mike" />

But in this case I have a PHP script that grabs inputs data and displays it using value="<?php echo get_option('your_name'); ?>" . So I have no idea how to force my form to display "Mike" in my input.

like image 753
Wordpressor Avatar asked May 16 '11 12:05

Wordpressor


People also ask

What is the default input type in HTML?

If the type attribute is not specified, the default type is "text".

What is the default value in characters type text of an input element?

The size attribute is a numeric value indicating how many characters wide the input field should be. The value must be a number greater than zero, and the default value is 20.

What is value in HTML input?

The value attribute specifies the value of an <input> element. The value attribute is used differently for different input types: For "button", "reset", and "submit" - it defines the text on the button.


1 Answers

You need to check the return of get_option first, and substitute something if a default is not available

<?php
    $default = get_option('your_name');
    if( $default == "")
    {
        $default = <whatever your default value is>;
    }
?>
<input type="text" name="your_name" value="<?php echo $default; ?>" />

Change get_option to return an empty string (or something else) if the default is not available.

like image 151
Steve Mallory Avatar answered Oct 10 '22 06:10

Steve Mallory