开发者

Multipart form with data output to browser in jQuery/PHP. How to show input data?

开发者 https://www.devze.com 2023-03-22 11:50 出处:网络
I\'m trying to create a form with 3 steps: fill the form check if data is correct (show input) thank you

I'm trying to create a form with 3 steps:

  1. fill the form
  2. check if data is correct (show input)
  3. thank you

With an advice of some开发者_运维技巧 people here (regarding my previous question) I've changed my way of doing it from mainly PHP + js-validation to mainly js + PHP process data.

I need an advice with how to deal with this now.

Previously I've had a PHP if/else that determined which step to show and kept data in $_SESSION for 2nd step and possible corrections back in 1st step.

Now I'm wondering if I really need two ajax calls (first to process data in order to show it - 2nd form step uses $_SESSION to display data input in 1st step; second to generate e-mail and pdf with given data - same $_SESSION as step2).

Maybe a good solution would be to put data with javascript into 2nd step aswell and use $_SESSION only in the final processing and generating.

What's the common/your approach to this problem?


Here's the normal flow:

User loads page with form on it. Fills it out. Submits it.

You can validate every field as they fill it out (instant feedback which is nice from a user's perspective) or validate using the onSubmit event (in jquery $('#formID').submit). You don't allow them to submit if it doesn't pass, return false from the submit function.

In case they don't have JS enabled (you can try to prevent them from using it w/o JS but in reality they can just use curl -d "value1=foo&value2=foo2&value3=foo3" http://example.org/page/ to get around you) you have to validate the data on the server, too. JS isn't enough.

If it doesn't pass server validation, you can redirect them back to the form using the Location: http://example.org header or echo the form again in the server-side code. If it does pass, you can use it (insert it into db, echo it, email it, whatever).

You save the data in your database and add it to $_SESSION. You echo the data they just entered along with a button "Download PDF" or some such.

They click "Download PDF".

You have all the information you need to create and PDF. You don't have anything to validate but you need to use the $_SESSION information to create the PDF. You should test the $_SESSION to make sure they have valid input from the previous pages or else someone can mimic a post to the page and generate a PDF (perhaps blank though). I generally avoid using data from a $_SESSION as anything but state information -- I'll write to a db on post data (after scrubbing) but if it's in $_SESSION it usually is just stuff that tells me who they are and stores other information about configuration, etc. In your case, I'd have written to a DB in step one and now would use some ID from $_SESSION to pull that record and create the PDF to send it.

I think all of your validation can be easily done in step one and then you separate step 2 into delivery based on the validity of step one.

0

精彩评论

暂无评论...
验证码 换一张
取 消