简体   繁体   English

如果该字段为空,如何设置默认值。-php

[英]How to set default value if the field is empty.-php

I want to set default value if the field is empty. 如果该字段为空,我想设置默认值。

I have a text field with reset button with that, and also i want to set default value in that field. 我有一个带有复位按钮的文本字段,我也想在该字段中设置默认值。

If user wants to change the field value he can click on reset and enter new value and click on save. 如果用户要更改字段值,则可以单击重置并输入新值,然后单击保存。

If field is empty by default it should be default value. 如果默认情况下该字段为空,则应为默认值。

on click of save its getting saved in database. 在点击保存后,将其保存在数据库中。

Here is my code 这是我的代码

<td>
<input id="default_fromMail" name="default_fromMail" type="email" value="<?php echo isset($item['default_fromMail']) ? $item['default_fromMail'] : ''; ?>"
size="50" class="code" placeholder="<?php _e('', 'custom_table_example')?>"   /> 
<input  type="button" value="Reset" id="default_from_button" name="openLab5" />
</td>

As of now its getting saved into database. 到目前为止,它已保存到数据库中。 But empty field is saved as it is(empty value) 但是,空字段将按原样保存(空值)

I tried setting default value in table it self but still its not working. 我尝试在表自身中设置默认值,但仍然无法正常工作。

Below is my db structure 下面是我的数据库结构 在此处输入图片说明

i need xyz@gmail as default if the value is empty. 如果该值为空,则需要xyz @ gmail作为默认值。

In handler form code use this: 在处理程序形式的代码中使用以下代码:

$email = (empty($_POST['default_fromMail']) ? 'xyz@gmail' : $_POST['default_fromMail'];

And then write $email to DB. 然后将$email写入数据库。

This variant better than set default value in DB column, 'cause if you want change default value, you need change only variable value against change table structure. 此变体比在DB列中设置默认值更好,因为如果要更改默认值,则只需要更改变量值即可更改表结构。

you should check the value in the code that saves the field (form) value in the database. 您应该检查将字段(表单)值保存在数据库中的代码中的值。 If this field is empty, you should pass it the default value. 如果此字段为空,则应将其传递为默认值。

$defaultMail = empty($_POST['default_fromMail']) ? 'xyz@gmail' : $_POST['default_fromMail'];

And then save the $defaultMail value to the database. 然后将$ defaultMail值保存到数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM