HTML/PHP - default input value

前端 未结 10 1311
梦如初夏
梦如初夏 2020-12-11 16:48

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

相关标签:
10条回答
  • 2020-12-11 17:12

    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.

    0 讨论(0)
  • 2020-12-11 17:15

    Simply use a ternary operator. its pretty easy try this

    $default = 'Mike';
    
    $your_name = get_option('your_name');
    
    $condition = !empty($your_name) ? $your_name : $default;
    
    <input type="text" name="your_name" value="<?php echo $condition; ?>" />
    
    0 讨论(0)
  • 2020-12-11 17:26

    This is How I solved this issue in my problem which I believe is similar, when $_POST is read the value is populated from the $_POST value else sets a default of Mike

    value="<?php if (isset($_POST['name'])) echo $_POST['name']; else echo "Mike"?>" >
    

    Hope this helps someone oneday

    0 讨论(0)
  • 2020-12-11 17:27
    <input type="text" name="your_name" value="<?php echo empty(get_option('your_name')) ? "TheDefaultValue" : get_option('your_name'); ?>" />
    
    0 讨论(0)
  • 2020-12-11 17:29

    I agree with Teneff but I would break it out.

    In the index.php I would have the following at the top of the doc

    <?
    $defaultText = include 'default_text.php';  
    ?>
    

    where your forms are it would be:

    <form method="post" action="">
    <input id="names"><? echo $defaultText; ?> </input> 
    </form> 
    

    then I would have a seperate file on the root level called "default_text.php."

    <?
    $Name = <<<EOD Name EOD;
    return $Name;
    ?>
    
    0 讨论(0)
  • 2020-12-11 17:32

    You can do a switch statement and have default to be whatever you want it to be.

    0 讨论(0)
提交回复
热议问题