Click to See Complete Forum and Search --> : submit forms

01-15-2007, 09:58 PM
can someone tell me how sumbit buttons work? I dont know how that works because when I look at html codes with forms it tells me nothing of sending it to an email address. For instance if I want someone to sign-up for a newsletter where is that information sent...please someone help me out, I always write on this thing and get no replies 98% of the time.

01-17-2007, 04:55 AM
For dealing with things like newsletter sign-ups you really need a server side script to handle things.

The form in your HTML collects the information, then clicking submit gets the browser to send the information entered into the form to your script. The script can then do what it wants - save the information, send you and email or whatever.

Here's a very quick example (without any real error checking or anything) that would create a form where a user entered their name and email address and when this is submitted the information is sent to the site admin - first some HTML:

<form method="post" action="send.php">
<label for="name">Name:</label> <input type="text" name="name" id="name" value="">
<label for="email">Email: </label> <input type="text" name="email" id="email" value="">
<input type="submit" value="Send">

The <form method="post" action="send.php"> line is the most important one, it tells the browser that when the form is submitted it should send the content of the form (using the HTTP POST method) to a file called send.php - the form also contains a couple of input fields where the user can type in their name and email address. When the form is submitted what is entered here will be what gets sent to send.php

So now onto send.php (assuming you're running on a server that supports PHP)


$name = !empty($_POST['name']) ? $_POST['name'] : 'Name not entered';
$email = !empty($_POST['email']) ? $_POST['email'] : 'Email not entered';

$message = 'Hello! ' . $name . ' (email address: ' . $email . ') says hi!';

mail('admin@myserver.com', 'Message from the website', $message);

echo '<p>Thanks for submitting the form!</p>';


The <?php and ?> are tags that tell the server that the content contained between them is PHP code and needs to be processed - rather than plain old HTML that is sent directly to the browser.

Remember the form was setup to send the content of the form using POST? well PHP takes all this POST content and stores it in $_POST. If you know the names of the variables that were sent (name and email - the name attributes from the form input fields) you can get the information that was sent to the script. The lines,

$name = !empty($_POST['name']) ? $_POST['name'] : 'Name not entered';
$email = !empty($_POST['email']) ? $_POST['email'] : 'Email not entered';

do this. First they check that $_POST['name'] and $_POST['email'] have a value - that is they are !empty. If there is a value supplied, then that value is used. Otherwise a message saying the user didn't supply that information is set.

Right, now we have the information we need to do something with it. The line,

$message = 'Hello! ' . $name . ' (email address: ' . $email . ') says hi!';

joins it all together to create a cheery greeting. But a greeting isn't much use if no one sees it, so

mail('admin@myserver.com', 'Message from the website', $message);

sends the greeting (stored in $message) as an email to admin@myserver.com with the subject line message from the website.

Finally a message is written back to the browser thanking the user.