HTML / PHP - default input value

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

  • your name
  • your name
  • My name

Each 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 = not available?

[edit]

Ok, so normally I would do something like:

 <input type="text" name="your_name" value="Mike" /> 

But in this case, I have a PHP script that captures the input and displays it using value="<?php echo get_option('your_name'); ?>" . Therefore, I do not know how to make my form display "Mike" on my input.

+6
source share
10 answers

You need to check the return of get_option first and replace something if the default is unavailable

 <?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 value is not available.

+5
source

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; } 
+2
source

You can make a switch statement and by default be who you want.

0
source

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

I agree with Teneff, but I will break it.

In index.php, I will have the following at the top of the document

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

where your forms will be like this:

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

then I would have a separate file at the root level named default_text.php.

 <? $Name = <<<EOD Name EOD; return $Name; ?> 
0
source
 <input type="text" name="your_name" value="<?php echo empty(get_option('your_name')) ? "TheDefaultValue" : get_option('your_name'); ?>" /> 
0
source

In this case, you can use my tiny ValueResolver library, for example:

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

and do not forget to use the namespace use LapaLabs\ValueResolver\Resolver\ValueResolver;

There is also the possibility of type casting, for example, if the value of the variable must 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) 

Learn more about docs for more examples.

0
source

This is how I solved this problem in my problem, which, in my opinion, is similar when $ _POST is read, the value is populated from the value of $ _POST else, sets the default value for Mike

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

Hope this helps someone on hold

0
source

Just use the ternary operator. its pretty easy to 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
source

Set some variable at the beginning with post variables.

as

 $name_val = ""; if(isset($_POST["your_name"]) { $name_val = $_POST["your_name"]; } <input type="text" name="your_name" value="<?= $name_val?>"> 
-2
source

Source: https://habr.com/ru/post/888220/


All Articles