0

We have created a questionnaire using a HTML form. The form is submitted via a typical ajax call as follows:

$.ajax({
    url: sendResults.attr('action') + "?ajax=true",
    type: sendResults.attr('method'),
    data: sendResults.serialize(),
    success: confirm
});

However, we want to save a copy of the submitted form HTML to the server (not in a database) so that it may be rendered to a PDF and attached to a confirmation e-mail sent from another PHP script. This HTML should contain the 'checked' status of check-boxes and radio buttons as they have been changed by the user. There are no textareas in the form.

One method of doing this is to select the form HTML via javaScript as follows:

var html = "<html>" + document.getElementById('myForm').innerHTML + "</html>";

And then serialize and submit this HTML with the form data by changing this line:

data: sendResults.serialize() + "&answers=" + html

However, this requires the ISP to override some firewall rules for the action PHP script and this would probably not be considered good practice, and is likely a security risk!

So, given that the "normal" serialized form data is just an array of integers, is there any known way to "apply" these values to the default HTML form, which is stored on the server, to arrive at the form as submitted, so that it can be rendered to PDF and attached to an e-mail?

Thanks in advance for any help.

deanodley
  • 1
  • 1
  • Of course there is and it's done all the time with forms to send them prefilled to browser – charlietfl Jan 12 '16 at 18:06
  • The questionnaire is being used to ascertain attitude to financial risk and so in addition to the calculated result, the user (who filled in the form) must be provided with an exact copy of their answers as they submitted them. – deanodley Jan 12 '16 at 18:09
  • 1
    And the problem with using same form template in php is? This is done all the time using server code only – charlietfl Jan 12 '16 at 18:24
  • 1
    If you already have the form, then you don't need the HTML. Save the submitted values, unchecked boxes won't show up, so you know their unchecked. Build a view partial based on the form and at run-time of sending the email, grab the view partial, fill in from databased info, and attach. It's just that simple. You're thinking to deep on this. – SpYk3HH Jan 12 '16 at 18:40

2 Answers2

0

try to encode your html content "&answers="+btoa(html)

decode it back in the server side.

I also recommend using post request instead of using the url paramter.

Check this link How can you encode a string to Base64 in JavaScript?

Community
  • 1
  • 1
Oddadmix
  • 180
  • 2
  • 11
  • I tried this but when I assign a variable, $answers = $_POST['answers'], it is empty. Previously this variable would contain the HTML as a string, now with the Base64 approach it is empty. I have encoded the HTML as Base64 using btoa and other methods I found on-line. – deanodley Jan 13 '16 at 17:53
  • if you are still passing the answers in the paramter use $answers = $_GET['answers']; – Oddadmix Jan 13 '16 at 17:58
-1

What if you get the attributes and values of the inputs and insert it into your database? It will save you some bytes of space and make your database table easy to read and understand, so when you have to generate the PDF you can select then and "recreate" the same HTML form.

P.S: I know it's a little late to answer but i hope it helps somebody.

Renoir Reis
  • 359
  • 4
  • 17
  • 1
    The `.serialize()` method already creates a map of the name attribute and associated value of each input. The point of grabbing the HTML from the client would make sense if you don't have control over the script that generated that form in the first instance, otherwise the serialized map it is already sending would be sufficient to regenerate the HTML on the server side. – dubrox Nov 19 '16 at 02:39
  • That is a good solution, but i didn't found in the jquery documentation how serialize can get other atributes like checked or some custom attributes. – Renoir Reis Nov 19 '16 at 11:29