LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to write to a file from a form with perl, and not leave the form page. (http://www.linuxquestions.org/questions/programming-9/how-to-write-to-a-file-from-a-form-with-perl-and-not-leave-the-form-page-682978/)

OldGaf 11-12-2008 05:45 PM

How to write to a file from a form with perl, and not leave the form page.
 
Hi all,

I would like to have a simple form (one text box) that will post to a perl script which in turn will append the post to a file on the server.

I am new to perl, but know how to write a script to update a file. I also can write a basic form. (impressive eh?)

The catch is that I do not want to leave the form page..... just submit the data and have the text box clear and wait for more input.

Is this possible?

If not, can I have the "confirmation page" redirect me back to the form?

Thank,

-OG-

teknik 11-12-2008 06:05 PM

To do it without leaving the page will require the use of AJAX (Asynchronous Javascript and XML) - don't worry, its easier than it sounds. Do some tutorials on that and you'll find your answer. Basically the solution would require using javascript to send off a request "behind-the-scenes" to your perl script which writes to the file, and then the javascript call would return when the perl script has executed, and at that point you'd clear your text field (again, with javascript). It's easier than it sounds, for sure.

The other way you asked about, where the confirmation redirects back to the form, is also really easy. All you'd have to do in that case is put a header on the confirmation page to redirect back to the form page. You can do this with the following script:

Code:

#!/usr/bin/perl

# this will have to be your first line of output...
print "Location: http://www.your-website.com/yourpage.html\n\n";


OldGaf 11-12-2008 08:18 PM

Quote:

Originally Posted by teknik (Post 3339930)
To do it without leaving the page will require the use of AJAX (Asynchronous Javascript and XML) - don't worry, its easier than it sounds. Do some tutorials on that and you'll find your answer. Basically the solution would require using javascript to send off a request "behind-the-scenes" to your perl script which writes to the file, and then the javascript call would return when the perl script has executed, and at that point you'd clear your text field (again, with javascript). It's easier than it sounds, for sure.

The other way you asked about, where the confirmation redirects back to the form, is also really easy. All you'd have to do in that case is put a header on the confirmation page to redirect back to the form page. You can do this with the following script:

Code:

#!/usr/bin/perl

# this will have to be your first line of output...
print "Location: http://www.your-website.com/yourpage.html\n\n";


Thanks a bunch boss!

I will look at this tomorrow.

-OG-

theNbomr 11-12-2008 08:53 PM

Not quite as spiffy, but a lot easier for newbies to implement, would be to have your per CGI simply re-issue the original form. If it is as simple as you say it is, it should re-draw in no time on the browser, and not be too annoying to the user. When using perl for CGI work, I recommend the CGI module available from CPAN. Makes parsing the form data quite painless.

--- rod.


All times are GMT -5. The time now is 07:23 AM.