开发者

HTML/PHP - default input value

开发者 https://www.devze.com 2023-03-06 00:36 出处:网络
I have a post php form and a set of inputs: Your Name Your Last Name My Name Every input looks the same, only the names change:

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 ec开发者_StackOverflowho 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.


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?>">
0

精彩评论

暂无评论...
验证码 换一张
取 消