Page 1 of 1

Style sheet and code problem

Posted: Thu Sep 18, 2014 2:22 pm
by kydude1957
The code below has the following problems I cannot seem to figure out:

The error message shows on load.

I made a separate css file for the form and put it in the same folder as the css file for the rest of the site but the css doesnt get applied to the contact form. Also when you put a valid email address in the code it doesnt send an email.

Any help would be appreciated.

CSS:

Code: Select all

<style type="text/css">
<!--
*{
margin: 0px;
padding: 0px;
}

 body{
    
    background: #fff;
    text-align: center;
    font-family: verdana;
    font-size: 18px;
    color: #000;
 }
 #wrapper{
    margin:0 auto;
    width: 500px;
    
 }
 #contactform{
border: 5px solid #ccc;
width: 440px;
margin: 25px;
padding: 5px;
 } 

 label {
float: left;
width: 100px;
text-align: right;
 }
 
 input, textarea{
  width: 300px;
  padding: 5px;
  margin: 0px 0px 10px 0px;  
  }
  
  input.submit{
    width: 100px;
    margin-left: 75px;
  }
  p.error{
    color: red;
    text-transform: uppercase;
  }
  p.sent{
    color: green;
  }	
-->
</style>

Contact form code:

Code: Select all

<div class='mainbody'>

<?php

if($_POST=['submit']){
       if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
        $error = 'true';
        
       }  else {
            
            $to = 'test@gmail.com'; 
                  
            $name =trim($_POST['name']);
            $email =trim($_POST['email']);
            $message =trim($_POST['comments']);
            
            $subject = "Contact Form";
            
            $messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
            $headers = "From:" . $name;
            $mailsent = mail($to, $subject, $messages, $headers);
            
            if($mailsent){
                $sent = true;
            }
            
        }
            
        }
    

?>

<!DOCTYPE html PUBLIC "-//WC3/DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtmll-transitional.dtd">
<html xmlns="http://www.w3c.org/1999/xhtml">
<head>
<title>Some Title</title>
<meta http-equiv="content-type" content="text/html" charset="UTF-8" />

<link rel="stylesheet" href="contact/css" />

</head>

<body>
     <div id="wrapper">
          <h1>Send a Message</h1>
          <?php if($error == true) { ?>
          <p class="error">There was a missing field on the form.  Please make sure you enter all details and a comment</p>
          <?php } if($sent == true) { ?>
          <p class="sent">Thank you for contacting me.  I will do my best to get back to you within 24 hours!!</p>
          <?php } ?>
          <div id="contactform">
          <form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <label for="name">Name:</label>
                <input type="text" name="name" />
                <label for="email">Email:</label>
                <input type="text" name="email" />
                <label for="comments">Comments:</label>
                <textarea name="comments" cols="15" rows="10"></textarea>
                <input type="submit" name="submit" class="submit" value="Submit" />
          </form>
          <div style="clear: both;"></div>
          </div>
     </div>

</body>
</html>
</div>

Re: Style sheet and code problem

Posted: Thu Sep 18, 2014 2:35 pm
by Celauran
Do the style tags exist in the CSS document? They shouldn't.

Code: Select all

<link rel="stylesheet" href="contact/css" />
Should that read contact.css, maybe?

You have an opening div before your DOCTYPE declaration. That's just wrong.
Also when you put a valid email address in the code it doesnt send an email.
Does it not send, or is it not received? Those are two different problems. What does mail() return? Anything in your mail server logs?

Re: Style sheet and code problem

Posted: Thu Sep 18, 2014 3:01 pm
by kydude1957
Ok got the stylesheet problem settled and it displays as needed. Now that leaves the error message displaying at load time. Also when you fill in the message, subject and name and send an email the form disappears and the success message never shows. Heres the current form code:

Code: Select all

<?php

if($_POST=['submit']){
       if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
        $error = 'true';
        
       }  else {
            
            $to = 'test@gmail.com'; 
                  
            $name =trim($_POST['name']);
            $email =trim($_POST['email']);
            $message =trim($_POST['comments']);
            
            $subject = "Contact Form";
            
            $messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
            $headers = "From:" . $name;
            $mailsent = mail($to, $subject, $messages, $headers);
            
            if($mailsent){
                $sent = true;
            }
            
        }
            
        }
    

?>

<!DOCTYPE html PUBLIC "-//WC3/DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtmll-transitional.dtd">
<html xmlns="http://www.w3c.org/1999/xhtml">
<head>
<title>Some Title</title>
<meta http-equiv="content-type" content="text/html" charset="UTF-8" />

<link rel="stylesheet" href="Styles/contact.css" />

</head>

<body>
     <div id="wrapper">
          <h1>Send Austen a Message</h1>
          <?php if($error == true) { ?>
          <p class="error">There was a missing field on the form.  Please make sure you enter all details and a comment</p>
          <?php } if($sent == true) { ?>
          <p class="sent">Thank you for contacting me.  I will do my best to get back to you within 24 hours!!</p>
          <?php } ?>
          <div id="contactform">
          <form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <label for="name">Name:</label>
                <input type="text" name="name" />
                <label for="email">Email:</label>
                <input type="text" name="email" />
                <label for="comments">Comments:</label>
                <textarea name="comments" cols="15" rows="10"></textarea>
                <input type="submit" name="submit" class="submit" value="Submit" />
          </form>
          <div style="clear: both;"></div>
          </div>
     </div>

</body>
</html>

Re: Style sheet and code problem

Posted: Thu Sep 18, 2014 3:24 pm
by Celauran

Code: Select all

if($_POST=['submit']){
That = shouldn't be there.

Code: Select all

if ($_POST['submit']) {

Re: Style sheet and code problem

Posted: Thu Sep 18, 2014 3:40 pm
by kydude1957
ok thanks. that took care of the error message but as soon as you hit send the form page goes way and you are back to the home page. it should stay on the form page and display the conformation message. the mail is never received but i see no errors in the log.