View Full Version : Printing form variables from a new window

01-27-2006, 08:23 PM

I am currently working on a project that formats variables from a form and writes them to a new page to be printed, The form also has an online submit routine so the easy route is already being used. I am using a button with javascript to open a new window and write the values of each form field to the new window in a printer friendly format (Darn Text Areas).

I thought i had it but my script is not working, If anyone could review my code to see where I screwed up it would be greatly appreciated.

<title>Damn Form that won't work</title>
<script type="text/javascript">
function printField() {
var name = document.contactForm.q[1].value;
var email = document.contactForm.email.value;
var phone = document.contactForm.q[2].value;
var st1 = document.contactForm.q[3].value;
var st2 = document.contactForm.q[4].value;
var city = document.contactForm.q[5].value;
var state = document.contactForm.q[6].value;
var zip = document.contactForm.q[7].value;
var year = document.contactForm.q[8].value;
var make = document.contactForm.q[9].value;
var model = document.contactForm.q[10].value;
var opts = document.contactForm.q[11].value;
var story = document.contactForm.q[12].value;
var regExp=/\n/gi;
s = s.replace(regExp,'<br>');
pWin = window.open('','pWin','location=no, menubar=yes, toolbar=no');
pWin.document.write('Name: ' + name + '<br>');
pWin.document.write('E-Mail: ' + email + '<br>');
pWin.document.write('Phone: ' + phone + '<br>');
pWin.document.write('Street Address: <table><tr><td>' + st1 + '</td></tr><tr><td>' + st2 + '</td></tr><tr><td>' + city + ', ' + state + '&nbsp' + zip + '<br><br>');
pWin.document.write('<h3><div align="center">My Vehicle</div></h3><br>');
pWin.document.write('Year: ' + year + '<br>');
pWin.document.write('Make: ' + make + '<br>');
pWin.document.write('Model: ' + model + '<br>');
pWin.document.write('Options: ' + opts + '<br><br>');
pWin.document.write('<h3><div align="center">Why my vehicle should be the next CoverCar</div></h3><br>');
pWin.document.write('<br>' + story + '<br>');

<form name="contactForm" method="post" action="http://www.mycontactform.com/sendform/sendform.php" onreset="return confirm('Really reset all form fields?')">
<input name="user" type="hidden" id="user" value="quanta">
<input name="formid" type="hidden" id="formid" value="38618">
<input name="subject" type="hidden" id="subject" value="Entry Photo">
Name: <input type="text" name="q[1]"><br>
Email: <input type="text" name="email"><br>
Address: <textarea name="q[2]"></textarea>

<!-- Form shortened but you get the Idea -->

<button type="submit">Submit your entry online</button><br><br>
<button onclick="printField()">Submit your Entry via SnailMail</button>

Any help would be appreciated, I've been looking at this code too long and can't figure it out.

Actual page online at: http://www.quantaproducts.com/contest/contestentries.asp

01-28-2006, 08:33 AM
biggest thing is your names - you can't say name="q[2]"

just say name="q2" instead (i just did a global replace on [ and ] in notepad and it worked fine).

also the line s=s.replace(someregex) refers to a non defined variable (s)

and there's no q12.

01-30-2006, 02:02 PM
when's the last time you did something then looked back on it after the weekend and realized " What the heck was i thinking?"

Thanks subhailc

I wasn't thinking, oh and s was defined but then i started changing variables around, and i restructured the way i was formating. The names are required by the cgi script i'm using (outsourced), but i can always call form item 1,2,3 etc.

Once again thanks subhailc for the quick kick in the brain...:thumbsup:

01-31-2006, 03:37 AM

01-31-2006, 04:09 AM
biggest thing is your names - you can't say name="q[2]"
You can. I dont know PHP but I know it requires element names to be suffixed by [] (but without the index though) so that it knows it expects an array of elements.

You would access these types of field names using square bracket notation.


See my sig for more info of the bracket notation.

I know this problem is already solved and naming fields with brackets is unnecessary but I thought it's worth mentioning this.

01-31-2006, 05:19 AM
i was talking about this
var name = document.contactForm.q[1].value; hadnt thougth about the quotes with brackets tho - nice.

and yeah you can use it in php - works just like you'd expect

<html><body><?php echo $bob[2]; ?><form><input name="bob[]" />
<input name="bob[]" />
<input name="bob[]" />
<input name="bob[]" />
<input name="bob[]" />
<input type="submit" /></form></body></html>on submit that'd print out the value of the third input down - and you can force it with
<input name="bob[11]" />
<input name="bob[10]" />
<input name="bob[2]" />anyways i dindt mean to give bad info - just easier to say 'cant' when it didnt seem relevant or important.