PDA

View Full Version : CGI scripting? (Or PHP?)



Muz
2007-10-12, 01:18 PM
Hey folks-

I'm putting together a website, designing it myself with HTML and CSS, and it's mostly complete. My problem is that there are two very simple forms that need to take text input from a single box and send that input to a single email address (though each of the two forms has its own email address it needs to send to).

I've got the HTML done, but of course the problem is getting the actual functionality to work, which I understand requires a bit of server-side scripting. Unfortunately I only know HTML and CSS, and don't know much (or hardly a thing, really) about Perl/CGI/PHP/etc.

Is this something that's easy to do/write? I don't figure it'd be TOO hard to put together something so basic, but I suppose I could be wrong. Does anyone know where I could find out how to do this (or better yet, a place where I can find a free script that would do this sort of thing for me?)

Any help would be appreciated. :smallsmile:

valadil
2007-10-12, 03:07 PM
It's pretty easy with PHP. Basically on your form you'll want the action attribute to point to your .php script. Method can be post or get.

The php script is also pretty simple. Documentation is available online (http://us3.php.net/manual/en/function.mail.php), but the basics function you'll be using is mail(). It takes 3 arguments, to, subject, and message. So calling "mail('[email protected]', 'php mail', 'I just sent email through php! BTW, hello world!')" would send a message to [email protected], the subject would be php mail, and the message itself would be the last bit at the end. Sending mail in PHP is pretty easy.

The part that's trickier (but really not all that tricky) is getting the form values. They get stored in an array, which is kind of like a list of data if you're not familiar with data structures. Depending which method you'll use, the array your data goes in is either $_POST or $_GET. PHP uses associative arrays, which means you can access data by name instead of by value. What this means is that if your HTML form had a textfield input with a name of "emailmessage" and you were using method=get, you could get the value of that input by looking up $_GET["emailmessage"].

So in essence your script will look something like this:


$emailmessage = $_GET["emailmessage"];
mail("[email protected]", "subject line", $emailmessage);


The problem with that is that your visitors will be left with a blank page. All that script does is send you mail. You'll have to send them some text.



echo "Thank you for sending me feedback! Please <a href=http://mysite.com>click here</a> to return to my site.";



Or something to that effect.

The Prince of Cats
2007-10-12, 05:49 PM
Well, I learned through looking up tutorials. I started with this tutorial (http://www.webmonkey.com/webmonkey/programming/php/tutorials/tutorial4.html) (somewhat randomly) and then continued learning from PHP.net until I was comfortable.

Writing emailers like the one you want can lead to spam issues. Some spammers look for those forms and add headers to the body. You need to escape the text properly...

Muz
2007-10-12, 05:55 PM
Thanks for the help!
(Ahh, I love this forum. ...Well, not "love" as in the love of a man for a woman, but "love" as in the love of a good steak! ...Except I'm not really that big of a fan of steak, but... OH! Lobster. There we go.) :smallbiggrin:

Fax Celestis
2007-10-12, 05:56 PM
Thanks for the help!
(Ahh, I love this forum. ...Well, not "love" as in the love of a man for a woman, but "love" as in the love of a good steak! ...Except I'm not really that big of a fan of steak, but... OH! Lobster. There we go.) :smallbiggrin:

Stay away from my avatar.

Raiser Blade
2007-10-12, 06:10 PM
*Lovingly huggles Fax's avatar*

Mhhmm tasty:smallwink:

Reinboom
2007-10-12, 07:44 PM
Make sure you do a bit of research to see what your host supports. Also, I don't recommend CGI for such a simple task.

Also, for handling forms - you want to work into JavaScript first.

Shhalahr Windrider
2007-10-12, 08:59 PM
Despite valadil's example, you're definitely gonna want to use the POST method rather than the GET method.

The GET method sends the form data through the script's url in your browser's address field. This allows people to call up the script directly without using the form. This kind of thing is useful when the script leads to things you want to link to like search engine results (http://www.google.com/search?client=opera&rls=en&q=PHP+e-mail+script&sourceid=opera&ie=utf-8&oe=utf-8) or threads on a forum (http://www.giantitp.com/forums/showthread.php?t=59292). It is not such a good idea for scripts that send e-mail, where you don't want the same results repeated so easily.

The POST method sends data behind the scenes in special headers. Most people cannot produce any results from a POST-based script without filling in the form and sending it directly. This makes it a better choice for e-mail scripts.

valadil
2007-10-13, 10:13 AM
Despite valadil's example, you're definitely gonna want to use the POST method rather than the GET method.


No matter how many years I keep programming for the web I'll never keep track of which is post and which is get. I know that they're different and I remember what the difference is, but I just fail at remembering which is which and have to look it up each and every time. It's freakin' annoying!

But yes, you're right in that POST is better than GET here. A crafty foe could use POST in an http request as part of a script (rather than using GET in their browser's address bar), but someone who knows how to do that can find plenty of other ways to annoy you before resorting to http requests.

Penguinsushi
2007-10-16, 05:50 PM
I spend most of my day writing php, so I *might* be biased, but yeah - go with that over cgi's. It's much simpler IMO - unless your host has a built in cgi formmailer (which you might check on, I know the host we typically use does), in which case they'll have a utility to help you set it up.

As was pointed out, there is plenty of php documentation on the net as well. I highly suggest www.php.net (http://www.php.net) as a great reference for functions and syntax.

Windrider has a good point about using POST. Though it doesn't necessarily guarantee you against spam content, it will make it at least a little more difficult.

You should definitely learn some PHP anyway - you can do *much* more in the way of dynamic content with it. Especially if you couple that with a sql database...but I digress.

Good luck.

~PS