Collecting info from forms

jadiebrown

New Member
I am pretty new to web design so this may be quite a simple question - I have a form on the contact us page of a website. Getting the form up with the submit button etc is simple enough. But then what? How do I actually get the messages that people input into the form and submit? The website supports the process but I'm not sure how to link it up.
 

adx

New Member
If you have PHP on your server all you really need is the code.
Just so happens I had a contact form open on my desktop! What are the odds.

Code:
<?php
   session_start(); // This should be placed before any Html code, even Doctype!
  ?>

Some CSS for the visuals..

Code:
 #form-field p {
    color: #5D5D5D;
    font-size: 14px;
    margin: 10px 0px 0px 15px;
    text-align: left;}
	
   #form-field {
    width: 450px;
    background-color: #F3F3F3;
    border: 1px solid #5D5D5D;
    margin: 10px;}
	
   #form-field form {
    margin: 10px 15px 10px 15px;}
	
   #form-field label {
    font-size: 11px;
    margin: 4px;
    color: #5D5D5D;
    line-height: 18px;}
 
   .input-fields {
    width: 200px;
    border: 1px solid #5D5D5D;}
	
   #text-field {
    width: 420px;
    border: 1px solid #5D5D5D;}

Some JS to limit the text input..

Code:
<script type="text/javascript">
 function crop(area, limit) {
 if (area.value.length > limit) {
 area.value = area.value.substring(0, limit);}}
</script>

The actual PHP bit..

Code:
<?php
  
   // Copyright © HtmlSwag.com
	 
   if ($_SESSION['token'] == $_POST['token']) {
	 
    if (!empty($_POST['email']) && !empty($_POST['message'])) {
	   
    $to = "[email protected]"; // Your E-Mail address!
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $email = $_POST['email'];
     
     // Basic regular expression to ascertain that an e-mail is valid
     
     if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.
     [a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
	 
     mail($to, $subject, $message, $email);
     $success = "Your message has been sent successfully!";}
		 
     else {
     $validate = "E-Mail address failed to validate!";}
    }
	 
    else {
    $failure = "Please fill out the required fields!";}
   }
	 
   else {
   $ready = "Everything seems fine, go for it!";}
   
   // Creates the session
   
   $token = md5(uniqid(rand(), true)); 
   $_SESSION['token'] = $token;
  ?>

The form..

Code:
 <div id="form-field">
  <p>Contact form | Status: <span style="color: #FF3366">
  <?php echo $ready.$success.$failure.$validate ;?></span></p>
      
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      
  <label>Your E-Mail: *</label><br />
  <input name="email" type="text" class="input-fields" 
  onkeydown="crop(this.form.email, 50);" 
  onkeyup="crop(this.form.email, 50);" /><br />
       
  <label>Subject:</label><br />
  <input name="subject" type="text" class="input-fields" 
  onkeydown="crop(this.form.subject, 50);" 
  onkeyup="crop(this.form.subject, 50);" /><br />
       
  <label>Message: * (Max <b>500</b> chars, * Required fields)</label><br />
  <textarea name="message" rows="5" cols="" id="text-field" 
  onkeydown="crop(this.form.message, 500);" 
  onkeyup="crop(this.form.message, 500);"></textarea>
  <input type="hidden" name="token" value="<?php echo $token ;?>" />
  <input type="submit" value="Submit" />
  <input type="reset" value="Reset" />
 </form>
</div>

Might be more than you asked for, but I was looking at it anyway so. Let me know if that doesn't do the trick! :)
Edit: Make sure the page extension is .php instead of .html, and be sure to add your E-Mail address to the PHP code
 
Last edited:

jadiebrown

New Member
Nevermind, I realise how silly that last question may have sounded. After your answer, many forums and tutorials and a whole lot of frustration, I have got it working!. Thanks again for your reply it set the foundation for me learning so much more about PHP!!:)
 

adx

New Member
Wasn't actually too silly of a question :) I made it specifically so you can put it anywhere you want it. Glad you liked it!
 
Top