php Codeigniter set_value() 并填充表单值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14667959/
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 07:45:20  来源:igfitidea点击:

Codeigniter set_value() and populate the form value

phpformscodeigniterretain

提问by user1854438

My form field looks like this

我的表单域看起来像这样

    echo form_label('Quantity', 'quantity');
    echo form_input('quantity', $quantityx);

I modified it to retain form values

我修改它以保留表单值

    echo form_label('Quantity', 'quantity');
    echo form_input('quantity', $this->input->post('quantity'));

I set the $quantityx above in the code behind in order to populate a value from the database so a user can edit a row.

我在后面的代码中设置了上面的 $quantityx 以便从数据库中填充一个值,以便用户可以编辑一行。

How can I retain the value for validations, and have a variable to populate from the database at the same time?

如何保留验证值,并同时从数据库中填充变量?

I have tried this a well

我已经试过了

    echo form_label('Quantity', 'quantity');
    echo form_input('quantity', set_value($quantityx));

Thanks for any suggestions

感谢您的任何建议

回答by tomexsans

You can set them like this, if set_value('columnanme')returns falsethen a form $_POSTdid not take place so what it will do is get the data from the data base example $profile->firstname

您可以像这样设置它们,如果set_value('columnanme')返回,false$_POST不会出现表单,因此它将执行的是从数据库示例中获取数据 $profile->firstname

$firstname = set_value('firstname') == false ? $profile->firstname : set_value('firstname');
$lastname = set_value('lastname') == false ? $profile->lastname : set_value('lastname');

Use it like:

像这样使用它:

echo form_label('Firstname', 'fname');
echo form_input('firstname', $firstname);
echo form_label('Firstname', 'lnamey');
echo form_input('firstname', $lastname);

in your case you can do:

在你的情况下,你可以这样做:

$quantity = set_value('Quantity') == false ? $quantityx : set_value('Quantity');

ANd on your form:

并在您的表格上:

echo form_label('Quantity', 'quantity');
    echo form_input('quantity', $quantity);

Assuming that $quantityxholds the data from your databse

假设$quantityx保存来自您的数据库的数据

EDIT
=============================================

编辑
==============================================

Just use set_values()second parameter as the default value. i.e set_value('name_of_input_field',$data_from_databse_as_default);If this does not work, then use my first answer.

只需使用set_values()第二个参数作为默认值。即set_value('name_of_input_field',$data_from_databse_as_default);如果这不起作用,那么使用我的第一个答案。

回答by Captain Sparrow

To populate the value below code worked for me.

填充以下代码的值对我有用。

form_input('cat_name','{YOUR VALUE}',array('id' => 'cat_name', ''placeHolder'' => 'Enter Cat Name')); 

回答by Tushar Sharma

There are 3 basic types of inputs. Here's how to repopulate each of them.

有 3 种基本类型的输入。以下是如何重新填充它们中的每一个。

Text Inputs

文本输入

$quantity = getValueFromDb();
echo form_input(
    ['name' => 'textInput',],
    set_value('textInput', $quantity, false)
);

Use falseas third parameter of set_valueto avoid double html escaping, which is done by form_inputas well.

使用false作为第三个参数set_value,以避免双重HTML转义,这是做form_input的很好。

Dropdowns

下拉菜单

$quantity = getValueFromDb();
$quantities = getQuantitiesList();
echo form_dropdown(
    [ 'name' => 'formDropdown'],
    [''=>'-- Select Quantity --'] + $quantities,
    set_value('formDropdown', $quantity, false)
);  

Radio Buttons

单选按钮

$quantity = getValueFromDb();
echo form_radio(
    ['name'=>'formRadio'],
    5,
    set_radio('formRadio', 5, $quantity === 5)
), 5;
echo form_radio(
    ['name'=>'formRadio'],
    10,
    set_radio('formRadio', 10, $quantity === 10)
), 10;

For radio inputs, use loops for substituting literals.

对于无线电输入,使用循环替换文字。

Checkboxes

复选框

Checkboxes can be made in a similar fashion by replacing form_radiowith form_checkboxand set_radiowith set_checkbox.

可以通过替换form_radioform_checkboxset_radio以类似的方式制作复选框set_checkbox

$values = [ 5 => 'Five', 10 => 'Ten', 15 => 'Fifteen',];
$selectedValues = [5, 15];
foreach ($values as $value => $label) {
    echo form_checkbox(
        ['name' => 'formCheckbox[]'],
        $value,
        set_checkbox(
            'formCheckbox[]', 
            $value,
            in_array($value, $selectedValues, true)
        )
    ), $label;
}

回答by Devendra Kumar

You can try this code to set the value in the text field while validating and fetch the value from the database.

您可以尝试使用此代码在文本字段中设置值,同时验证并从数据库中获取值。

form_input(array('id' => 'cat_name', 'name' => 'cat_name','value'=>set_value('cat_name',$cat_name)));