I am trying to get myContact Us form on my web project to work with php so that when the full name and email address is entered with comments it sends me an email and then displays a success message and if they fail to fill in the form them an error message displays saying for them to try again. My code is not working and I dont really understand why, it just displays with error message no matter what even when all info is entered. this is my php code below: -
`<?php`
$name = $email = "";
if (isset ($_POST ['fullname']))
$name = fix_string ($_POST ['fullname']);
if (isset ($_POST ['email'])) $email = fix_string ($_POST ['email']);
$email_to = "my@eamiladdress.com";
$email_subject = "Question asked";
echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head>
<link href='myelearningtool.css' rel='stylesheet' type='text/css' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>Registration Confirmation E-Learning Tool Prototype</title>
<div class='page'>
<div class='header'>
<div id='logo'>
<img src='images/BS_kids_ELearningToollogo.png' alt='E-Learning Tool Prototype v1' />
</div>
<ul id='navigation'>
<li><a href='****************.html'>Logout</a></li>
<li><a href='**************.html'>Activities</a></li>
<li><a href='****************.html'>Videos</a></li>
<li><a href='****************.html'>Glossary</a></li>
<li><a href='********'>Contact Us</a></li>
</ul>
</div><!--end of header div -->
<div class='content'><!--start of content -->
<div class='contact_details'>
<p>Thanks for that, someone will email you back as soon as possible.</P>
</div><!--end of contact details-->
</div>
</div><!--end of content div-->
</div>
</div>
</html>";
$fail = validate_fullname($name);
$fail += validate_email($email);
if ($fail == ""){
echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head>
<link href='myelearningtool.css' rel='stylesheet' type='text/css' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>Registration Confirmation E-Learning Tool Prototype</title>
<div class='page'>
<div class='header'>
<div id='logo'>
<img src='images/BS_kids_ELearningToollogo.png' alt='E-Learning Tool Prototype v1' />
</div>
<ul id='navigation'>
<li><a href='****************.html'>Return To Home Page</a></li>
</ul>
</div><!--end of header div -->
<div class='content'><!--start of content -->
<p>Ooops yous missed something off. Please try again</p>
<fieldset><!--details section-->
<legend><b>Contact Us</b></legend>
<label for='name'> Your Full Name*:</label> <input type='text' name='fullname' id='name' maxlength='50' size='30' tabindex='1' accesskey='q' />
<label for='email'>Email Address*:</label><input type='text' name='email' id='email' maxlength='50' size='30' tabindex='2' accesskey='w' />
</fieldset><!--end of fieldset-->
<div class='message'>
<fieldset>
<legend>Message*</legend>
<textarea name='message' id='msge' cols='130' rows='10'>Please type your question or query here</textarea>
</fieldset>
<p><input type='submit' value='Enter' /></p>
</div><!--end of content div-->
</div>
</div>
</html>";
}
function validate_fullname($name){
return ($name == "")? "No name was entered<br>": "";
}
function validate_email($email){
if ($email == "")
return "No email was entered<br>";
else if (!((strpos($email, ".") > 0) && (strpos($email, "@") > 0)) || preg_match ("/[^a-z A-Z 0-9.@_-]/", $email))
return "The Email address is invalid<br>";
return "";
}
function fix_string($string){
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return htmlentities ($string);
}
?>
Can anyone please help me and advise on where I am going wrong.
I have made some changes to my code as suggested. I am at a loss I not really understanding why it isnt working, it is also not using the css style sheet. below is the new edited code: -
<?php
if (isset($_POST ['fullname']) || isset($_POST ['email'])):
$name = fix_string($_POST ['fullname']);
$email = fix_string($_POST ['email']);
$email_to = "my@email.com";
$email_subject = "My E-Learning Tool Query";
$fail = validate_fullname($name);
$fail += validate_email($email);
if($fail!=""):
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head>
<link href='myelearningtool.css' rel='stylesheet' type='text/css' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>Contact Us E-Learning Tool Prototype</title>
<div class='page'>
<div class='header'>
<div id='logo'>
<img src='images/BS_kids_ELearningToollogo.png' alt='E-Learning Tool Prototype v1' />
</div>
<ul id='navigation'>
<li><a href='http://******/myelearningtool.html'>Return To Home Page</a></li>
</ul>
</div><!--end of header div -->
<div class='ContactUs'><!--start of content -->
<p>Ooops yous missed something off. Please try again</p>
<form method='post' action='php/contactUs.php' onsubmit='return validation(this)' ><!-- Start of the form to be submitted -->
<fieldset><!--details section-->
<legend><b>Contact Us</b></legend>
<label for='name'> Your Full Name*:</label> <input type='text' name='fullname' id='name' maxlength='50' size='30' tabindex='1' accesskey='q' />
<label for='email'>Email Address*:</label><input type='text' name='email' id='email' maxlength='50' size='30' tabindex='2' accesskey='w' />
</fieldset><!--end of fieldset-->
<div class='message'>
<fieldset>
<legend>Message*</legend>
<textarea name='message' id='msge' cols='130' rows='10'>Please type your question or query here</textarea>
</fieldset>
<p><input type='submit' value='Enter' /></p>
</div>
</div><!--end of contact us div-->
</div>
</div>
</html>
<?php
else:
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head>
<link href='myelearningtool.css' rel='stylesheet' type='text/css' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>Registration Confirmation E-Learning Tool Prototype</title>
<div class='page'>
<div class='header'>
<div id='logo'>
<img src='images/BS_kids_ELearningToollogo.png' alt='E-Learning Tool Prototype v1' />
</div>
<ul id='navigation'>
<li><a href='http://******myelearningtool.html'>Logout</a></li>
<li><a href='http://******/myelearningtoolactivities.html'>Activities</a></li>
<li><a href='http://******t/myelearningtoolVideos.html'>Videos</a></li>
<li><a href='http://******t/myelearningtoolglossary.html'>Glossary</a></li>
<li><a href=' '>Contact Us</a></li>
</ul>
</div><!--end of header div -->
<div class='content'><!--start of content -->
<div class='ContactUs'>
<p>Thanks for that, someone will email you back as soon as possible.</P>
</div><!--end of contact us-->
</div><!--end of content div-->
</div>
</div>
</html>
<?php
endif;
?>
<?php
else:
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<link href="myelearningtool.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Us Page for e-Learning Tool Prototype</title><!--this is the title of the page, I have chosen this title as it states exactly what the web page is -->
</head>
<body>
<div class="page">
<div class="header"><!--start of the header which will contain the banner and navigation-->
<ul id="navigation" style="text-align: center">
<li><a href="http://******/myelearningtool.html">Home Page</a></li>
<li><a href="">Activites</a></li>
<li><a href="">Videos</a></li>
</ul>
<div id="logo" style="text-align: center">
<img src="images/BS_kids_ELearningToollogo.png" alt="E-Learning Tool Prototype v1" /><!--logo created in photoshop-->
</div><!--end of logo div-->
</div><!--end of header div -->
<div class="ContactUs">
<form method="post" action="php/contactUs.php" onsubmit="return validation(this)" ><!-- Start of the form to be submitted -->
<fieldset><!--details section-->
<legend><b>Contact Us</b></legend>
<label for="name"> Your Full Name*:</label> <input type="text" name="fullname" id="name" maxlength="100" size="30" tabindex="1" accesskey="q" />
<label for="email">Email Address*:</label><input type="text" name="email" id="email" maxlength="50" size="30" tabindex="2" accesskey="w" />
</fieldset><!--end of fieldset-->
<div class="message">
<fieldset>
<legend>Message*</legend>
<textarea name="message" id="msge" cols="130" rows="10">Please type your question or query here</textarea>
</fieldset>
<p><input type="submit" value="Enter" /></p>
</div>
</form>
</div><!--end of div-->
</div><!--end of page div-->
</body><!--end of body div-->
</html><!--end of html div-->
<?php
endif;
$fail = validate_fullname($name);
$fail += validate_email($email);
function validate_fullname($name){
return ($name == "")? "No name was entered<br>": "";
}
function validate_email($email){
if ($email == "")
return "No email was entered<br>";
else if (!((strpos($email, ".") > 0) && (strpos($email, "@") > 0)) || preg_match ("/[^a-z A-Z 0-9.@_-]/", $email))
return "The Email address is invalid<br>";
return "";
}
function fix_string($string){
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return htmlentities ($string);
}
?>