Log In · Register

 
 
Closed TopicStart new topic
HTML Forms, How do I get the info sent somewhere?
LaRachel
post Jul 12 2010, 01:58 PM
Post #1


Senior Member
***

Group: Member
Posts: 75
Joined: Feb 2009
Member No: 717,000



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?
 
Maccabee
post Jul 12 2010, 03:47 PM
Post #2


Senior Member
*******

Group: Official Designer
Posts: 5,880
Joined: Nov 2007
Member No: 593,382



Email. You can use html and php.
 
LaRachel
post Jul 12 2010, 04:39 PM
Post #3


Senior Member
***

Group: Member
Posts: 75
Joined: Feb 2009
Member No: 717,000



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?
 
schizo
post Jul 12 2010, 05:40 PM
Post #4


Senior Member
******

Group: Staff Alumni
Posts: 2,435
Joined: Feb 2007
Member No: 506,205



Well, what are you codes? Do you have any PHP at all yet?
 
Mikeplyts
post Jul 13 2010, 04:02 AM
Post #5


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



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
 
LaRachel
post Jul 13 2010, 02:13 PM
Post #6


Senior Member
***

Group: Member
Posts: 75
Joined: Feb 2009
Member No: 717,000



Thank you SO much! Makes perfect sense, and works perfectly.
 
manny-the-dino
post Jul 13 2010, 03:38 PM
Post #7


Senior Member
*******

Group: Administrator
Posts: 8,629
Joined: Jan 2007
Member No: 498,468



Topic Closed and Moved
 

Closed TopicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members: