Can I write to a div using Perl, or otherwise solve my form submission problem?
July 24, 2008 12:11 PM   Subscribe

Can I write to a div using Perl, or otherwise solve my form submission problem?

I have a Perl/CGI form mailer script that I didn't write and shouldn't edit too much, but can somewhat.

It's currently set up so that I can define the thank you page and error page within the Perl file. I don't want it to redirect to those pages however. I want to display the messages on the page the form is on instead, without reloading if possible. I played around with redirecting to the same page but appending a parameter in the URL. Then using JS to show a div if that var was set. But I'm realizing I need to display some of the items submitted from the form and I don't know how to keep those values since the page is being refreshed.

I don't know Perl so I don't know if there is a better way to do this, but can I do something like write to a div with Perl? Other ideas?
posted by unsigned to Computers & Internet (5 answers total) 1 user marked this as a favorite
 
What about just adding the form to the thank you page? You should have the form data available to you at that point, so you could pre-populate the values.

If you're handy with AJAX (and you'll have to be if you have you have your heart set on "without reloading if possible"), you could strip out all the html from the thanks/error pages except the actual message part. Then have the form make the ajax call and update a (previously empty) div with the returned message. Should be fairly simple if you use something like prototype.js.

(My perl skills ain't much either, but I expect people that do know it will need to see the actual code before making perl-specific recommendations. The main question being is the html output nicely separated from the perl logic.)
posted by danOstuporStar at 12:38 PM on July 24, 2008


This is exactly what a little AJAX is good for.

W3shools tutorial.

I don't actually know Perl within the CGI world, but I assume that anything I can do with PHP I could do with perl. This is a snap with PHP.
posted by cgg at 1:52 PM on July 24, 2008


If your perl script sends the user to the thank you or error page by sending a redirect header -- I'm guessing you're using formmail.pl, which does this -- then to use it in an AJAX context you'll need to modify it to either directly respond with the HTML you want stuffed into the div, or, better, keep your html separate from your server code by having it respond with some simpler signal, say "1" for success or "0" for fail, and let the AJAX code read that and use it to show or hide an appropriate div containing whatever html you're looking to display. Or if you want to get fancy, have it send back some JSON snippet containing any variables you want to get back from the form.

If you are using formmail.pl, the easiest way to do this is going to be to hack the line that prints out the actual redirect header (print "Location: [your url]") into something your receiving javascript can easily read and do something with. (print "1")

So, whole process, A-Z: instead of your HTML form submitting directly to your perl script, the submit button will call some javascript which will send the form via AJAX. (I second the recommendation of prototype.js, which makes this very simple to do.) You'd also have two extra divs in your HTML, both styled with display:none -- one will contain your 'thank you' html, the other your 'error' html. Your perl script will send back 1 or 0. The callback function from the ajax call will receive this, and make one or the other of those hidden divs visible -- if you're using prototype.js, this is as easy as $('ID_of_thankyou_div').show();
posted by ook at 2:51 PM on July 24, 2008


Er, one correction -- since you'd be removing the Location: header, you'd have to send a content-type header instead... so instead of "print '1'" it'd be "print 'Content-type: text/html\n\n1';" or what have you.
posted by ook at 2:53 PM on July 24, 2008


Response by poster: I tried going the ajax route, which certainly makes sense. I'm not sure if I'd been staring at the screen too long or just didn't feel like fighting with it any more, but it didn't quite work.

I did however, realize that even though I didn't think it would work this way at all... if I put the form in an iframe and kept the redirect in the perl to thanks.html it redirect the iframe to thanks.html. I don't know why I assumed it would redirect the parent window instead, but glad it didn't.

Thanks all -
posted by unsigned at 7:21 PM on July 24, 2008


« Older Where to get a stethoscope in Montreal?   |   Help a mid 20's guy find some new punk music. Newer »
This thread is closed to new comments.