Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

forms - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...