HTML/PHP - 默认输入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6017598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:09:46  来源:igfitidea点击:

HTML/PHP - default input value

phpformsinputdefault-value

提问by Wordpressor

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

我有一个 post php 表单和一组输入:

  1. Your Name
  2. Your Last Name
  3. My Name
  1. 你的名字
  2. 你的姓氏
  3. 我的名字

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?

当 value= 不可用时如何设置默认值?

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

但在这种情况下,我有一个 PHP 脚本,它可以获取输入数据并使用value="<?php echo get_option('your_name'); ?>". 所以我不知道如何强制我的表单在我的输入中显示“Mike”。

采纳答案by Steve Mallory

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

您需要先检查 get_option 的返回值,如果默认值不可用,则替换一些内容

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

如果默认值不可用,请更改 get_option 以返回空字符串(或其他内容)。

回答by Teneff

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

您可以将get_option()功能更改为类似

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;
}

回答by Jose soriano

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

这就是我在我的问题中解决这个问题的方法,我认为这是类似的,当读取 $_POST 时,值是从 $_POST 值填充的,否则设置默认值 Mike

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

Hope this helps someone oneday

希望有一天这可以帮助某人

回答by Mohammad Faisal

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

回答by Bjoern

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

由于value是默认设置,只需在get_option-function周围添加一个条件,可能是这样的:

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

回答by terra823

I agree with Teneff but I would break it out.

我同意 Teneff,但我会打破它。

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

在 index.php 中,我将在文档顶部显示以下内容

<?
$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."

那么我将在根级别上有一个名为“default_text.php”的单独文件。

<?
$Name = <<<EOD Name EOD;
return $Name;
?>

回答by Dennis

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

回答by Victor Bocharsky

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

在这种情况下,您可以使用我的小库ValueResolver,例如:

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

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

并且不要忘记使用命名空间 use LapaLabs\ValueResolver\Resolver\ValueResolver;

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

还可以进行类型转换,例如,如果您的变量的值应该是integer,请使用以下命令:

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

Check the docsfor more examples

查看文档以获取更多示例

回答by Kamil Sindi

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

您可以执行 switch 语句并默认为您想要的任何内容。

回答by Muhammad Ummar

Set some variable at start with the post variables.

在开始时使用 post 变量设置一些变量。

like

喜欢

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

<input type="text" name="your_name" value="<?= $name_val?>">