2

I have a page that generates forms depending on user choice. I want to know if there is a way to convert the form into plain text? Ex. The form has 4 fields, each has a label. I would like to take all the labels/fields and print them to the user as follows:

label1 field1
label2 field2
label3 field3
label4 field4

Is there a way to do so?

sikas
  • 5,435
  • 28
  • 75
  • 120
  • You'll have to use client side scripting for this, preferably jQuery. Is this relevant in your case? – Shadow The GPT Wizard Jan 01 '12 at 10:57
  • Yes, there is. What mark-up are you working with? What have you tried? What research have you done? Have you put together a [JS Fiddle demo](http://jsfiddle.net/) for us to work with? – David Thomas Jan 01 '12 at 10:57
  • @Shadow Wizard: yes, this is acceptable. – sikas Jan 01 '12 at 10:57
  • @David Thomas: I've searched google but couldn't find anything. – sikas Jan 01 '12 at 10:59
  • Can you please explain a bit what's the reasoning behind this? What will you use text result for? Will you display it back? Should it be converted to non-form HTML or actual text only? Text only will of course invalidly display within browser anyway... – Robert Koritnik Jan 01 '12 at 11:06
  • @RobertKoritnik: I'm creating a project that when a form is filled, there will be a button that when clicked will convert the form to text to be copied. – sikas Jan 01 '12 at 11:10

4 Answers4

2

You can use jQuery to generate basic output, for example:

var texts = [];
$("form label").each(function() {
    var oLabel = $(this);
    var oInput = oLabel.next();
    texts.push(oLabel.text() + " - " + oInput.val());
});
var plainText = texts.join("<br />");
$("#Output").html(plainText);

This will iterate over all the form labels, then take the next element of each label which is the input.

Live test case.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • that is exactly what I want. But do I need to link and JS files to have the JQuery usable? – sikas Jan 01 '12 at 11:11
  • @sikas yes you need to reference one single file, the latest version can be found [here](http://code.jquery.com/jquery-latest.min.js) - either copy the file locally to your own server, or link directly to that file e.g. `` – Shadow The GPT Wizard Jan 01 '12 at 11:15
  • A more reliable source for hosted versions (as I'm not too sure about jQuery's stance on using their hosted version) would be [Google's hosted libraries](http://code.google.com/apis/libraries/devguide.html#jquery). – Purag Jan 01 '12 at 11:21
  • @Purmou got to disagree about that.. IMO jQuery is as stable as Google and will stick around even after Google is long gone. Even if someone will eventually but them, hard to believe they will stop hosting their own code. – Shadow The GPT Wizard Jan 01 '12 at 11:23
  • @ShadowWizard: Maybe so, but the only reason I mentioned Google's was because I'm unsure about jQuery's thoughts on using their hosted version. Do you know of any official statement about that? – Purag Jan 01 '12 at 11:23
  • @Purmou just [this](http://stackoverflow.com/a/4059708/447356) so far and my own logic. :) – Shadow The GPT Wizard Jan 01 '12 at 11:24
  • @Purmou, ShadowWizard: jQuery lists their own CDN in their [Downloading jQuery](http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery) faq page, albeit they list themselves *last*, though I don't know that their's significance to that. – David Thomas Jan 01 '12 at 11:34
1

If you don't mind an extra file, with PHP.

You'd need to make a small php file to echo all the form information like this:

<?php
$label1 = "label1";
$label2 = "label2"; //Set all labels here
$label3 = "label3";
$label4 = "label4";
echo $label1 . " " . $_POST['field1'] . "<br />"; //Change to get depending on your method.
echo $label2 . " " . $_POST['field2'] . "<br />";
echo $label3 . " " . $_POST['field3'] . "<br />";
echo $label4 . " " . $_POST['field4'] . "<br />";
echo "Done.";
?>

and put that in it's own file. Set the form's action attribute to that file and it will print out all the data.

Different55
  • 577
  • 2
  • 17
  • That would be great if I have only one form. But I have around 20. So I'm not gonna make php file for each! – sikas Jan 01 '12 at 11:06
  • You don't have to. If they are all like that, you could use the same file. If they aren't, you could still use one file, but it would take a little longer to make. – Different55 Jan 01 '12 at 11:51
1

One method, without a JavaSCript library, is:

var form = document.getElementsByTagName('form')[0];

form.onsubmit = function(){

    var labels = document.getElementsByTagName('label');

    if (!document.getElementById('container')){
        var container = document.createElement('ol');
        container.id = 'container';
    }
    else {
        var container = document.getElementById('container');
        while (container.firstChild){
            container.removeChild(container.firstChild);
        }
    }

    for (var i=0,len=labels.length; i<len; i++){
        if(document.getElementById(labels[i].getAttribute('for'))){
            var newLi = document.createElement('li');
            var iText = document.createElement('span');
            newLi.innerHTML = labels[i].innerHTML;
            iText.innerHTML = document.getElementById(labels[i].getAttribute('for')).value;
            newLi.appendChild(iText);
            container.appendChild(newLi);
        }
    }

    document.getElementsByTagName('body')[0].appendChild(container);

    return false;

};

JS Fiddle demo.

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0

You may get the value for labels and fields in javascript using

var label1 = getElementById("id").name

Create a string formatted as you want to show the user and show all the values with

document.write(string);
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
Shraddha
  • 2,337
  • 1
  • 16
  • 14