Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    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.

  • #3
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #4
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •