...

View Full Version : Should be a simple contact form, but...



CatRyd
07-14-2012, 10:26 PM
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:


<!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:


<!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

Len Whistler
07-15-2012, 12:10 AM
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.

_Aerospace_Eng_
07-15-2012, 05:19 PM
Your problem however is the textarea in your form for "comments" has name="message". Change that to name="comments" and that should work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum