Having never done PHP before, I have managed to put together this basic script to handle the submittion of a form on my page:
Part of the script is to check whenther the email address is valid or not, if it is not valid, it alerts the user via a javascript alert box, however after doing so, the pages directs to the php script displaying a blank page - e.g. www.*domain*.co.uk/submit.php
I dont want this to happen, I want it to stay on the original form page: www.*domain*.co.uk/enquiry.html.
I would also like, when the script displays the email is invalid, to clear the email input box on the page. Does anyone have any idea how I do this?
PHP
HTML
Part of the script is to check whenther the email address is valid or not, if it is not valid, it alerts the user via a javascript alert box, however after doing so, the pages directs to the php script displaying a blank page - e.g. www.*domain*.co.uk/submit.php

I would also like, when the script displays the email is invalid, to clear the email input box on the page. Does anyone have any idea how I do this?
PHP
Code:
<?php
/* Set e-mail recipient */
$myemail = "enquiries@*domain*.co.uk";
/* Check all form inputs using check_input function */
$regarding = check_input($_POST['regarding']);
$firstname = check_input($_POST['firstname'], "Enter your first name");
$lastname = check_input($_POST['lastname'], "Enter your last name");
$companyname = check_input($_POST['companyname']);
$address1 = check_input($_POST['address1']);
$address2 = check_input($_POST['address2']);
$city = check_input($_POST['city']);
$region = check_input($_POST['region']);
$postcode = check_input($_POST['postcode']);
$country = check_input($_POST['country']);
$email = check_input($_POST['email']);
$number = check_input($_POST['number']);
$enquiry = check_input($_POST['enquiry']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("The E-mail address entered is not valid.");
}
$subject = "New $regarding Enquiry";
/* Let's prepare the message for the e-mail */
$message = "A enquiry form has been submitted:
Enquiry regarding $regarding
Name: $firstname $lastname
Company: $companyname
Address:
$address1
$address2
$city
$region
$postcode
$country
E-mail: $email
Contact Number: $number
Enquiry:
$enquiry
";
$headers = "From: enquiryform@*domain*.co.uk";
/* Send the message using mail() function */
mail($myemail, $subject, $message, $headers);
/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
echo "<script language=\"javascript\">alert('$myError');</script>";
exit();
}
?>
HTML
Code:
<!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" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/JavaScript" src="script.js"></script>
<title>A-spec Solutions - Enquiry</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="placeholder"><a href="index.html"><img src="gfx/name2.png" alt="A-spec Solutions" /></a></div>
<div id ="menuback">
<div id="menucontainer">
<div class="split"></div>
<div class="navlinks"><a href="index.html">Home</a></div>
<div class="split"></div>
<div class="navlinks"><a href="about.html">About</a></div>
<div class="split"></div>
<div class="navlinks"><a href="itsupport.html">IT Support</a></div>
<div class="split"></div>
<div class="navlinks"><a href="itsecurity.html">IT Security</a></div>
<div class="split"></div>
<div class="navlinks"><a href="ithome.html">Home IT</a></div>
<div class="split"></div>
<div class="navlinks"><a href="contact.html">Contact</a></div>
<div class="split"></div>
</div>
</div>
<div class="wrapper">
<h2 class="headunderline">Enquery Form</h2>
<br />
<form action="submit.php" method="post">
<table id="enq">
<tr>
<td>
<div class="formtypelbl">
<label for="regarding">Please select the service you are enquiring:</label>
</div>
<div>
<select id="regarding" name="regarding">
<option value="#" selected="selected"></option>
<option value="support">IT Support</option>
<option value="security">IT Security</option>
<option value="home" >Home Support</option>
</select>
</div>
</td>
</tr>
<tr>
<td class="colour">
<br /><br />
<div class="formheader">Name</div><br />
<div class="formlbl">
<label for="firstname">First name:</label>
</div>
<div class="formstandardinput">
<input type="text" id="firstname" name="firstname" class="standard" />
</div>
<div class="formlbl">
<label for="lastname">Last name:</label>
</div>
<div>
<input type="text" id="lastname" name="lastname" class="standard" />
</div>
</td>
</tr>
<tr>
<td>
<br /><br />
<div class="formheader">Comapny Name (if applicable)</div><br />
<div class="formlbl">
<label for="companyname">Company name:</label>
</div>
<div>
<input type="text" id="companyname" name="companyname" class="standard" />
</div>
</td>
</tr>
<tr>
<td class="colour">
<br /><br />
<div class="formheader">Address</div><br />
<div class="formlbl">
<label for="address1">Address line 1:</label>
</div>
<div>
<input type="text" id="address1" name="address1" class="large" />
</div>
</td>
</tr>
<tr>
<td class="colour">
<div class="formlbl">
<label for="address2">Address line 2:</label>
</div>
<div>
<input type="text" id="address2" name="address2" class="large" />
</div>
</td>
</tr>
<tr>
<td class="colour">
<div class="formlbl">
<label for="city">City:</label>
</div>
<div class="formstandardinput">
<input type="text" id="city" name="city" class="standard" />
</div>
<div class="formlbl">
<label for="region">Region:</label>
</div>
<div>
<input type="text" id="region" name="region" class="standard" />
</div>
</td>
</tr>
<tr>
<td class="colour">
<div class="formlbl">
<label for="postcode">Postcode:</label>
</div>
<div class="formstandardinput">
<input type="text" id="postcode" name="postcode" class="standard" />
</div>
<div class="formlbl">
<label for="country">Country:</label>
</div>
<div>
<input type="text" id="country" name="country" class="standard" />
</div>
</td>
</tr>
<tr>
<td>
<br /><br />
<div class="formheader">Contact Details</div><br />
<div class="formlbl">
<label for="email">Email:</label>
</div>
<div>
<input type="text" id="email" name="email" class="standard" />
</div>
</td>
</tr>
<tr>
<td>
<div class="formlbl">
<label for="number">Contact number:</label>
</div>
<div>
<input type="text" id="number" name="number" class="standard" />
</div>
</td>
</tr>
<tr>
<td class="colour">
<br /><br />
<div>
<label for="enquiry">Enquery:</label>
<br />
<textarea rows="10" cols="30" id="enquiry" name="enquiry" class="large"></textarea>
</div>
</td>
</tr>
<tr>
<td>
<div>
<input type="submit" value="Submit" id="submit" />
</div>
</td>
</tr>
</table>
</form>
</div>
<div id="footer">
<p class="footercenter">© 2010 Aspec Solutions, all rights reserved.<a href="http://www.linkedin.com"><img src="gfx/linkedin.png" alt="Linkedin" id="linkedin" class="fbimg"/></a><a href="http://www.facebook.com/#!/pages/A-spec-Solutions/167870849919537"><img src="gfx/facebook.png" alt="Facebook" id="facebook" class="fbimg"/></a></p>
</div>
</body>
</html>
Last edited: