Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-14-2012, 09:26 PM   PM User | #1
CatRyd
New to the CF scene

 
Join Date: May 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
CatRyd is an unknown quantity at this point
Question Should be a simple contact form, but...

This is my first time using php. I followed an online tutorial. My problem seems to be in the validation, it is giving me errors even though I fill everything in correctly...

THIS IS MY HTML:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Needmathshelp.co.uk|My rates</title>


<link href="mathshelp.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.body #textbox table tr td {
	text-align: left;
}
.body #textbox form table tr td textarea {
	text-align: left;
}
</style>
</head>

<body>

<div id="content">

<div id="centerDoc"></div>

<div id="headerbox">

<div id="headertextbox">
<img src="Images/phone.gif" width="23" height="16" /><a href="callto://+447838793633"> 07838 793633</a> (9 am to 9 pm)<br/>
<br/>
<img src="Images/envelope.gif" width="23" height="16" /> <a href="mailto:mike@needmathshelp.co.uk">mike@needmathshelp.co.uk</a> <br/><br/>
<img src="Images/skype.gif" width="23" height="19" /> <a href="callto://needmathshelp/">needmathshelp</a> (Skype)
</div>

<div id="logo">
</div>

</div>

<div id="headline">
<h1>Contact me</h1>
</div>

<td class="body">
<div id="textbox">
If you are interested in <b>tuition</b>, it is helpful if your message tells me about the topic and qualification you need help with, where you are based, and whether the tuition is for you or for another member of your family.<br/><br/>


If you are interested in <b>data analysis or proof-reading</b>, it's most important to explain how big your project is, and what deadline you are working to.<p>

<form name="htmlform" method="post" action="html_form_send.php">
<table width="520" cellspacing="5">
</tr>
<tr>
 <td valign="top">
  <label for="name">First Name</label>
 </td>
 <td valign="top">
  <input  type="text" name="name" maxlength="50" size="30">
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="email">Email Address </label></td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>
 
</tr>
<tr>
 <td valign="top">
  <label for="telephone">Telephone Number</label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="message">Message</label>
 </td>
 <td valign="top"><textarea  name="message" maxlength="1000" cols="32" rows="5"></textarea></td>
 
</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit">
 </td>
</tr>
</table>
</form>
</div>


<div id="navbox">
</div>

<a href=" index.html"><div class="homegreen">
<h2>Home</h2>
</div></a>

<a href="where.html"><div class="wheregreen">
<h2>Where I teach</h2>
</div></a>



<a href="rates.html"><div class="ratesgreen">
<h2>My rates</h2>
</div></a>

<a href="testimonials.html"><div class="testemonialsgreen">
<h2>Testemonials</h2>
</div><a>

<a href="contact.html"><div class="contactred">
<h2>Contact me</h2>
</div><a>


<div id="footer">
</div>
<div id="footertext">
© 2012 needmathshelp.co.uk
</div>
</div>

</body>
</html>
MY PHP:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if(isset($_POST['email'])) {
     
    // CHANGE THE TWO LINES BELOW
    $email_to = "cathrine.rydning@gmail.com";
     
    $email_subject = "Someone contacted you via your webpage";
     
     
    function died($error) {
        // your error code can go here
        echo "Error: Form not completed correctly.";
        echo "The following errors were found:.<br /><br />";
        echo $error."<br /><br />";
        echo "Please try again.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('Error: Form not completed correctly.');      
    }
     
    $name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
     
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$name)) {
    $error_message .= 'The Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The message you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "Name: ".clean_string($first_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
     
     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>
 
<!-- place your own success html below -->
 
Thank you for contacting me. I will be in touch with you very soon.
 
<?php
}
die();
?>
</body>
</html>
The page can be found here: http://needmathshelp.co.uk/contact.html
CatRyd is offline   Reply With Quote
Old 07-14-2012, 11:10 PM   PM User | #2
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
I would go with a basic contact form and no error checking. Then when - or if - problems arise with errors tackle them at that time. That's the best way to learn PHP.

I doubt you will run into many problems by eliminating error checking.
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Old 07-15-2012, 04:19 PM   PM User | #3
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Your problem however is the textarea in your form for "comments" has name="message". Change that to name="comments" and that should work.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:55 PM.


Advertisement
Log in to turn off these ads.