Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2006, 08:23 PM   PM User | #1
spinsane
New to the CF scene

 
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
spinsane is an unknown quantity at this point
Printing form variables from a new window

Hello,

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.

Code:
<html>
<head>
<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.open(); 
   pWin.document.write('<html><head></head><body>'); 
   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>'); 
   pWin.document.write('</body></html>'); 
   pWin.print(); 
   pWin.document.close(); 
   pWin.close(); 
} 
</script>
</head>

<body>
<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/contes...estentries.asp
spinsane is offline   Reply With Quote
Old 01-28-2006, 08:33 AM   PM User | #2
subhailc
Regular Coder

 
Join Date: Jan 2004
Posts: 185
Thanks: 2
Thanked 1 Time in 1 Post
subhailc is an unknown quantity at this point
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.
subhailc is offline   Reply With Quote
Old 01-30-2006, 02:02 PM   PM User | #3
spinsane
New to the CF scene

 
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
spinsane is an unknown quantity at this point
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...
spinsane is offline   Reply With Quote
Old 01-31-2006, 03:37 AM   PM User | #4
subhailc
Regular Coder

 
Join Date: Jan 2004
Posts: 185
Thanks: 2
Thanked 1 Time in 1 Post
subhailc is an unknown quantity at this point
subhailc is offline   Reply With Quote
Old 01-31-2006, 04:09 AM   PM User | #5
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Quote:
Originally Posted by subhailc
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.

Code:
alert(document.contactForm.elements["q[2]"].value);
alert(document.contactForm.elements["q[]"].value);
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.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 01-31-2006, 05:19 AM   PM User | #6
subhailc
Regular Coder

 
Join Date: Jan 2004
Posts: 185
Thanks: 2
Thanked 1 Time in 1 Post
subhailc is an unknown quantity at this point
i was talking about this
Code:
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
Code:
<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
Code:
<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.
subhailc is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:32 PM.


Advertisement
Log in to turn off these ads.