Printable Version of Topic

Click here to view this topic in its original format

Forums _ Webmasters' Corner Resolved Topics _ HTML Forms

Posted by: LaRachel Jul 12 2010, 01:58 PM

Hey all, I'm trying to put an HTML form on my website, but I don't know how to see what people are putting in it? If I understand correctly it needs to be sent to some kind of... page?

Basically, what I'm trying to do is have people fill out the form, hit submit, and then have the results go somewhere so that I can see them later. I have it made and done, just don't know where to send the results?

Posted by: Maccabee Jul 12 2010, 03:47 PM

Email. You can use html and php.

Posted by: LaRachel Jul 12 2010, 04:39 PM

I don't know how to do that _unsure.gif

Also, I have tried using mailto, as that was the only thing I find on google about that, and that is definitely not what I'm going for. It just opens Outlook, and honestly who uses Outlook?

Posted by: schizo Jul 12 2010, 05:40 PM

Well, what are you codes? Do you have any PHP at all yet?

Posted by: Mikeplyts Jul 13 2010, 04:02 AM

Create a new PHP file. Let's say it's named message.php. Now, move on to your other file that contains the HTML form, which we'll call message.html.

For the HTML file, message.html, you're going to want your form fields acquire a name attribute and your form itself will need the action attribute and method attribute. So:

CODE
<form action="message.php" method="post">
    <table>
        <tr>
            <td><label for="name">Name:</label></td>
            <td><input id="name" name="name" /></td>
        </tr>
        <tr>
            <td><label for="email">E-mail:</label></td>
            <td><input id="email" name="email" /></td>
        </tr>
        <tr>
            <td><label for="message">Message:</label></td>
            <td><textarea id="message" name="message"></textarea></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Submit Message" /></td>
        </tr>
    </table>
</form>

You see how the action attribute of the form links to the PHP file? Basically, it runs whatever is in that PHP file for the form. And the method attribute uses the post method, which means it is being submitted. And for all the form fields, the name attribute contains the value of the field so that when it is posted, the PHP can recognize it.

Now, let's move on to the PHP, message.php:
CODE
<?php
// Retrieve the posted content from the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

// Now, submit the form if the $_POST array isn't empty. However, since PHP5, an empty array still counts as '1'. So, we have adjust this issue accordingly.
if (count($_POST) >= 2) {
// Check to see if there are any empty fields, though. If there are, generate an error.
if (!$name || !$email || !$message) echo 'Please fill in all fields.';

// Since this demonstration form submits an e-mail address, we need to check to see if the e-mail address is valid.
else if (!preg_match('~^.+?@[\w\.-]+\.[a-z]{2,4}$~', $email)) echo 'Use a valid e-mail address.';

// If everything checks out alright, continue.
else {
$to = 'youremail@address.com';
$subject = 'New message!';
$message = 'You have received a new message from ' . $name . '.

"' . $message . '"';
$from = 'From: ' . $email;
mail($to, $subject, $message, $from);
}
}
?>

I attached a few comments to help you out. But, basically, we take what was submitted from the form and assign those values to some variables. From there, we just count how many fields were posted (in this case, greater than or equal to two). If that's true, we go on to check if any one of those fields are empty (or null, using the ! operator). We also check for certain characters and symbols in the e-mail field to see if it's a valid email address (name@domain.com). After that, if there are no errors, we assign the e-mail address you want use to receive the messages (or details, whatever) to a variable, as well as a subject, a message, and a From header. We use the variables we got from the submitted data to fill in some of the details and then we use the mail() function (which is a built-in PHP function, really lovely) to send an e-mail to the supplied e-mail address. From there, you just check your e-mail and read the message generated by the PHP file.

A bit confusing at first, yes, but it's pretty easy to learn. Especially with an issue like this. You can also use this kind of PHP to update MySQL databases, if that's what you want. But, let me know if you need any help with this. thumbsup.gif

Posted by: LaRachel Jul 13 2010, 02:13 PM

Thank you SO much! Makes perfect sense, and works perfectly.

Posted by: manny-the-dino Jul 13 2010, 03:38 PM

Topic Closed and Moved