Welcome Guest, Not a member yet? Register   Sign In
conditional set_value()

CI 3x

I have a form that submits, then sends the user back to the same form and repopulates the fields if they click a checkbox to "send another after submit"...

All is working fine, the form is presented another time with the populated fields.

My issue is that I would like to empty out *some* of the fields on that second offering.

For example, what is the most elegant way of nulling out a text field, where I am currently using?:
PHP Code:
value="<?php echo set_value('f_eventname'); ?>" 

I know I could do a condition based off that checkbox value, something like:
PHP Code:
value="<?php (isset($submitAnother) ? echo "" : echo set_value('f_eventname')); ?>" 

but wondering if there is a better built-in way in CI?



I don't think there really an elegant way of handling this. This is the way I would do this: after the form is submitted and successfully validated, you could set a variable eg. anotherForm to true which you pass to your view. There you can check this variable and choose to not use set_value like so:

PHP Code:
// if anotherForm exists and is true, don't set the value entered earlier on
value="<?php echo (isset($anotherForm) and ($anotherForm == true)) ? '' : set_value('f_eventname'); ?>" 

Then there is you second question about the checkbox. The set_value() function is only used for inputs and textareas. For checkboxes you could do 2 things: 
1. use the form_checkbox function from the Form Helper to create your checkbox, which also handels the submitted value. See the user manual for the explanation: http://www.codeigniter.com/user_guide/he...m_checkbox 
2. you could use the set_checkbox function from the Form Helper which is like set_value. See the example taken from the user manual:

PHP Code:
<input type="checkbox" name="mycheck" value="1" <?php echo set_checkbox('mycheck''1'); ?> />
<input type="checkbox" name="mycheck" value="2" <?php echo set_checkbox('mycheck''2'); ?> /> 

The Form Helper has al sorts of functions 'helping' you with creating and processing forms. 

Hope this helps!


Depending on how many fields you want to keep compared to how many you want to resubmit, you could use a redirect to the same page. A redirect will reset the post fields. You would just send the additional details in the function arguments, and set those as the defaults for the form view.

Alternatively, perhaps the functionality should be redesigned. So if they are giving their name, postcode, password, username for example, plus some address details, say, and it is the address details that are being resubmitted, then I would have another screen entirely for adding more addresses. ie not re-displaying the same page. This stops the user from accidentally changing some of the info originally sent.

Alternatively you can manually set an array (all blank initially) that you use for the field values, rather than using set_value. Then overwrite those field values with submitted values for refilling, and then overwriting the 'to be blanked out' submitted values again if successfully validated and re-sending for the 'submit another' version of the screen. I would not do this myself but you could do it this way.

I suppose that all depends on your exact functionality. If you provide some details of the fields perhaps a better suggestion can be made.

Personally I would use two forms. One for the initial send and one for the submit another. You can then easily have different instructions, layouts and buttons for different options on the resubmit, and use that form again should the user later want to add another of whatever it is you are adding in your example.

Hi, thanks for the ideas / thoughts. Yeah, basically, I will have to write some conditionals. No problem.. just more code :-)

Maybe a suggestion for a future CI, $conditional:

 set_value($field[, $default = ''[, $html_escape = TRUE[, $reset_conditions = array()]]])

        $field (string) – Field name
        $default (string) – Default value
        $html_escape (bool) – Whether to turn off HTML escaping of the value
        $reset_conditions (array) - Condition to reset field value

So something like:

$reset_conditions = array(
       'submitAnoter'          => true,
       'anotherCondition'    => 10

<input type="text" name="quantity" value="<?php echo set_value('quantity', '0', , $reset_conditions); ?>" size="50" />


Theme © iAndrew 2016 - Forum software by © MyBB