Jump to content

Re-fill form input after an error

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Hello,

I'm having some difficulty to find how to refill my forms if the user didn't meet my criteras.
For example, I have a form where I ask the name of the client (basic example), the name need minimum 3 char, max 200
Of course, I have javascript validation, but as you know, javascript validation are not safe

So, back on the server side, I validate everything, and if he fail, I redirect the user to the form and I give him a unique Id.
So when I reload the form, I can (with the Id) get back what he posted, and the $_POST he had at this time, so the page will reload the exact same way.
And I send a json object fill with the post he sended, so I can refill the form.

Everything work great for small example like the name of the user.
But when I come with multi-part example, like when some forms are loaded via AJAX later depending on the option the user will check, it dosen't work.
I do have the data in my json object, but the form isn't on the page, so I can't fill them.

So, I'm asking you, how would you do this?

My idea for the moment, is to fill everything that is present on the page at the time on load. And each time I fill an form element, I delete the value inside the json object. So I only keep the data I didn't place yet.
Once everything is re-fill, I fire all the even I can think of for each element I re-fill, so if there is a ajax call linked to the event, it will occurs.
If I see an ajax call, I wait for the answer, and once the answer if processed (new element on the page), I recall my refill function with the json object with the data of the element that I didn't place yet, so it will fill the new data, but don't touch the old data, in case while doing so the user change a element.
But seriously... this is starting to be heavy, so I'm asking if anyone have a other idea.

Like always, my problem are twisted and complicated and mostly theorical mostly because I code this in my framework and not in a website, so it will have to work with every page possible, but I hope someone understood me, and will be able to help me.

#2
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
If you have Javascript validation won't that mean he's up to no good if he manages to POST incorrect data? I don't use JSON so I would probably just use a colon separated string. Eg:
First: bbqroast
Last:
Email: bbq@bbqro.ast
Would be sent as:
bbqroast::bbq@bbqro.ast
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#3
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Sometime the javascript can be disabled, and it dosen't mean the guy try to hack me.

I manage to repopulate the form that are on the screen at the moment of the window is loaded, by resending all the data with php session but the forms that is later called depending on the data the user picked I can't repopulate them.
I'm trying with the jquery $.when function, but it's executed too early. I'm trying to read about the jquery defered see if this can do anything good




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users