Welcome Guest, Not a member yet? Register   Sign In
adding input fields without js/query
#1

[eluser]PaulDunn[/eluser]
Hi

I have a multi page form posting to one controller method and use a switch statement to determine the page. Each case saves the previous page form data to session.

Without the requirement of js/jquery the user needs the ability to add inputs on demand.

Im not sure how to go about this and would very much appreciate some advise and suggestions.

Cheers
-Paul
#2

[eluser]CroNiX[/eluser]
Code:
//...form....
<input name="firstname" id="firstname" />

<a href="#" id="add_element">Click to Add</a>
Code:
<xscript type="text/javascript">
$(document).ready(function() {
    //add a click event to the anchor to trigger the making of the new form element
    $('#add_element').click(function(e){
        e.preventDefault();
        //create new html element
        var new_el = jQuery('&lt;input /&gt;')
            .attr('id', 'lastname')
            .attr('type', 'text')
            .attr('name', 'lastname');
        //insert it after the #firstname element
        $('#firstname').after(new_el);
    });
});
</xscript>
#3

[eluser]PaulDunn[/eluser]
Sorry I mean without js/jquery.

I'll edit my original post.
#4

[eluser]PaulDunn[/eluser]
Hasn't anyone got any tips Sad
#5

[eluser]LuckyFella73[/eluser]
Somebody correct me if I'm wrong but as far as I know:
if you don't want to reload the page page you can't create additional formfields
without using javascript. To add a form field without reloading a page
is a client-side task.
#6

[eluser]PaulDunn[/eluser]
I'm fine with a page refresh.
#7

[eluser]LuckyFella73[/eluser]
Ok, then you would have to place some form element where the
user can select what kind of additionally fields he needs
or maybe just a number of additionally input fields for example.

In your controller (in the related switch-part) check if these
elements contain data. Depending on what post values you get add the
wanted elements.
#8

[eluser]CroNiX[/eluser]
I was thinking of something similar.
1) have a checkbox in the form...if checked means need more fields...submit
2) in the form validation, check to see if this box is checked, if it is, fail the validation via custom rule (this will cause the form to be repopulated)
3) in the view, add a check to see if the checkbox value is present, if it is, add the extra needed fields
Code:
if($this->input->post('checkbox_name'))
{
    //echo out your extra form elements
}

This can get messy though...as you might need different sets of validation rules depending on if that checkbox is checked or not (to add additional rules for the new elements).




Theme © iAndrew 2016 - Forum software by © MyBB