Question

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.

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

Was it helpful?

Solution

You can set them like this, if set_value('columnanme') returns false then a form $_POST did not take place so what it will do is get the data from the data base example $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 $quantityx holds the data from your databse

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.

OTHER TIPS

To populate the value below code worked for me.

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

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

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

Text Inputs

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

Use false as third parameter of set_value to avoid double html escaping, which is done by form_input as well.

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_radio with form_checkbox and set_radio with 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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top