Web Design Forum: PHP Contact Form/Page - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

PHP Contact Form/Page Not working quite right! Rate Topic: -----

#1 User is offline   welshhuw 

  • Web Guru
  • PipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 1,755
  • Joined: 21-October 08
  • Reputation: 20
  • Gender:Male
  • Location:South Wales, UK
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 27 July 2009 - 05:26 PM

Hi,

I have a contact.php page on my website which isn't giving me the correct output I wanted. Please see pics:
What I wanted to happen after I hit submit is the thanks.html page to open. Instead it displays the contact.php page with a thanks message.

Can anybody point out how to alter the php script to make the error and thanks pages to display when req'd.

Thanks.

<div id="main">
		<div id="content">
			<h2 class="touch">Get In Touch</h2>
			<p>All fields required.</p>
<?php
//really thorough email address validation function
function isValidEmail($email) {
  // First, we check that there's one @ symbol, 
  // and that the lengths are right.
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters 
    // in one section or wrong number of @ symbols.
    return false;
  }
  // Split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
    if
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i])) {
      return false;
    }
  }
  // Check if domain is IP. If not, 
  // it should be valid domain name
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; // Not enough parts to domain
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if
(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$",
$domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}

//variables to hold the form status
$showform    =    true;
$formdata    =    array(
    
);

//process the form if submitted
if(isset($_POST['Submit'])) {
    //grab the values from the form
    $name        =    trim(stripslashes($_POST['Name']));
    $email        =    trim(stripslashes($_POST['Email']));
    $comments    =    trim(stripslashes($_POST['Comments']));
    
    //email variables
    $to            =    "huw@huwrowlands.co.uk";
    $subject    =    "Huw Rowlands Website Contact Form";    
    $headers    = "From: $email\n";

    // Gather all data for email body.
    $body = "";
    $body .= "Name: ";
    $body .= $name;
    $body .= "\n\n";
    $body .= "Email: ";
    $body .= $email;
    $body .= "\n\n";
    $body .= "Comments: ";
    $body .= $comments;
    $body .= "\n\n";
        
    //perform some validation
    $errors        =    array();    
    
    if(empty($name)) {
        $errors['name']            =    "Enter your name";
    }
    if(empty($email)) {
        $errors['email']        =    "Enter your email";
    }
    elseif(!isValidEmail($email)) {
        $errors['email']        =    "Email address invalid";
    }
    if(empty($comments)) {
        $errors['comments']        =    "Enter a message";
    }
    
    //if the form is valid then send the message
    if(count($errors)==0) {
        if(mail($to,$subject,$body,$headers)) {
            //hide the form, show success message instead
            $showform    =    false;
        }
        else {
            $errors['form']    =    "Unable to send message, try again";
        }
    }        
}

//show the form if needed
if($showform) {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="webform" onsubmit=" return checkForm()">
    <fieldset>
        <label for="Name">Name:</label>
        <input type="text" name="Name" id="Name" class="inputbox"<?php echo isset($name)?' value="'.$name.'" ':''; ?>/>
        <div class="error<?php echo isset($errors['name'])?' error2':''; ?>" id="nameError">Name Required!</div>
        
        <label for="Email">Email:</label>
        <input type="text" name="Email" id="Email" class="inputbox" <?php echo isset($email)?' value="'.$email.'" ':''; ?>/>
        <div class="error<?php echo isset($errors['email'])?' error2':''; ?>" id="emailError">Valid Email Required!</div>
        
        <label for="Comments">Comments:</label>
        <textarea name="Comments" rows="" cols="" id="Comments"><?php echo isset($comments)?$comments:''; ?></textarea>
        <div class="error<?php echo isset($errors['comments'])?' error2':''; ?>" id="commentsError"><br />Comments Required!</div>
        
        <input type="submit" value="Submit" name="Submit" class="submit"/>
    </fieldset>
</form>
<?php
} else { //otherwise show the successpage 
?>
<h2 class="thanks">Thanks!</h2>
<p>Your message was sent.</p>
<?php } ?>

0

#2 User is offline   Faevilangel 

  • Wordpress Ninja.....
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,166
  • Joined: 11-May 09
  • Reputation: 57
  • Gender:Male
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 27 July 2009 - 05:35 PM

the script on my website does what you want....

submit form
if an error then displays error with link back to the form
if successful then display thanks.html

use a php if else command
0

#3 User is offline   welshhuw 

  • Web Guru
  • PipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 1,755
  • Joined: 21-October 08
  • Reputation: 20
  • Gender:Male
  • Location:South Wales, UK
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 27 July 2009 - 05:37 PM

Sorry, I forgot to show you my code!

:blush1:
0

#4 User is offline   scaz182 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 365
  • Joined: 01-April 08
  • Reputation: 2
  • Gender:Male
  • Location:Reading UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 27 July 2009 - 05:51 PM

I'd just add a redirect (to thanks.html) in place of your thank you message.

e.g. Where you have this:
else { //otherwise show the successpage 
?>
<h2 class="thanks">Thanks!</h2>
<p>Your message was sent.</p>
<?php } ?>


Put this instead:
else { //otherwise show the successpage 
       // Define the URL.
       $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
       // Check for a trailing slash.
       if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
       $url = substr ($url, 0, -1); // Chop off the slash.
       }
       $url .= '/thanks.html';
       // redirect
       header("Location: $url");
       exit();
}

0

#5 User is offline   welshhuw 

  • Web Guru
  • PipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 1,755
  • Joined: 21-October 08
  • Reputation: 20
  • Gender:Male
  • Location:South Wales, UK
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 27 July 2009 - 06:47 PM

Could I simplify that into this?:
(I did try this and it gave me an error?)
header( "Location: http://www.huwrowlands.co.uk/ok.html" );

0

#6 User is offline   scaz182 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 365
  • Joined: 01-April 08
  • Reputation: 2
  • Gender:Male
  • Location:Reading UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 28 July 2009 - 12:13 PM

View Postwelshhuw, on 27 July 2009 - 06:47 PM, said:

Could I simplify that into this?:
(I did try this and it gave me an error?)
header( "Location: http://www.huwrowlands.co.uk/ok.html" );



Yeah you could do that instead although it will require more work if you change server or the directory.

I havn't fully looked through your code but if you are going to use the header(); function you must ensure you have absolutely no content (not even whitespace) being printed out before you redirect so its worth checking if that is causing your error.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users