HTML/PHP - default input value

六眼飞鱼酱① 提交于 2019-11-28 13:30:02

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.

you can change the get_option() function to be something like

function get_option($name) {
   $defaults = array(
      'fist_name' => 'Mike',
      'fist_name' => 'Wordpressor',
      'my_name' => 'Dunno'
   );
   // get the value from the $defaults array
   $val = $defaults[$name];

   // but if the same value has already been posted - replace the default one
   if (isset($_POST[$name])) {
      $val = $_POST[$name];
   }
   return $val;
}

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

Since value is the default, just add a condition around your get_option-function, maybe something like this:

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

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

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;
?>
<input type="text" name="your_name" value="<?php echo empty(get_option('your_name')) ? "TheDefaultValue" : get_option('your_name'); ?>" />

You could use my tiny library ValueResolver in this case, for example:

$default = ValueResolver::resolve(get_option('your_name'), '<whatever your default value is>');

and don't forget to use namespace use LapaLabs\ValueResolver\Resolver\ValueResolver;

There are also ability to typecasting, for example if your variable's value should be integer, so use this:

$id = ValueResolver::toInteger('6 apples', 1); // returns 6
$id = ValueResolver::toInteger('There are no apples', 1); // returns 1 (used default value)

Check the docs for more examples

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; ?>" />

Set some variable at start with the post variables.

like

$name_val = "";
if(isset($_POST["your_name"])
{
    $name_val = $_POST["your_name"];
}

<input type="text" name="your_name" value="<?= $name_val?>">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!