Reputation: 174
In my application users can make their own web forms for sign ups. Each newly added field has three elements: field_name
, field_type
and field_rules
. I need at least the field_name
to be specified.
My problem is when the validation fails, I lose the newly added form fields when the page reloads. How can I send back form errors to each respected form field and still keep all their work when the form validation fails?
EDIT: Sorry if I wasn't clear. Okay, so jQuery is used to add form fields to a page. Each cloned div contains three text boxes: field_name, field_type and field_rules. What I want to do is use CodeIgniter to iterate over those arrays to check if the required fields I need have content. If not, I want to send the user back to the form page with all of the user-added jQuery cloned divs there with the user-defined data present.
I've been working on a conditional construct that will check the $data array to determine if it should iterate the divs out. What I'm wondering is if there is an easier way: ie: send the data to jQuery on next page load or something.
Upvotes: 0
Views: 181
Reputation: 10060
Check this out: http://www.jqbyte.com/StickyForms/
It's a nice trick which you could use -- storing newly added fields and values in cookies.
Upvotes: 1
Reputation: 9148
In this case the traditional model for form submission is not very practical as to re-create the form fields they would need to be outputted by PHP.
You should look at using Ajax calls to do the form submission. With Ajax you can get all the form fields and bundle them into a request. The request is then posted to your server and a response returned from the server call.
With Ajax the overall page will not reload. You are responsible for parsing the result from the server and presenting it to the user.
To achieve this you should probably re-factor the controller that handles the form output/submission. The submission component should go into a separate controller that outputs the result in JSON
form so it can be easily parsed by jQuery.
Ref: Here's a tutorial that looks like it might help you.
Any questions, then just leave a comment.
Upvotes: 0
Reputation: 19882
Well there are some suggestion i would like to give you.
when you are using jquery create a hidden input field and set its value to 0.
when ever user creates a clone div update its value to +1.
When you submit the field and the validation fails the post data is available.
use jquery document.ready and call a simple function which will run on page load and gets the hidden inputs' value.
if the value is 0 do nothing and if the value is let suppose 3 then create 3 clone divs.
This way user will have the same number of fields as he had before submitting the form.
Also there is some other way of submitting a form and getting post data.
How to repopulate form with failed validation with jquery ajax
Upvotes: 0