HI guys, as mentioned not too long ago here https://www.daniweb.com/programming/web-development/threads/501668/contact-form-building-my-own-or-already-made I'm building my own php script that is supposed to validate and submit form fields to an email address provided. The problem is that I have no real experience of php so I' just doing some readings, getting bits and bobs from different places and add my own code - probably wrong anyway.
In terms of functionality, this is what I would like to build:
-Each field has its own error message displayed if empty or in case of bad input;
-When the form is submitted I need a message somewhere around the form to say something like "Thank you, form submitted" but I need the form to remain visible
-When I press the submit button, since this is a 1 page site, it will scroll back to the top, as you would expect. Normally I'd address this with one line of jquery
$(".contactForm .button input[type='submit']").click(function(e){
e.preventDefault();
});
but that wouldn't work here as I need the form to submit. Any other idea?
So, one way or another I've produced something that partially works but there are quite a lot of problems still. What would be great is if somebody could give me a hand to build this and get it to work: if you glance at the code and tell me that needs a completely rebuild, it's fine, at the end of the day, like I said, I'm not really that good with php.
So, let's get to the code. Here is what I've produced so far (if you want to see this code in action, you can find it here http://antonioborrillo.co.uk/agency_test/test/en/index.php and then click on "get in touch"):
<!-- form validation -->
<?php
//create variables for validation
$titleError = "";
$firstNameError = "";
$lastNameError = "";
$emailAddressError = "";
$messageError = "";
$websiteError = "";
$titleError = "";
$title = "";
$firstName = "";
$lastName = "";
$emailAddress = "";
$message = "";
$website = "";
//mail variables
$to = "bassabasso@gmail.com";
$subject = "New request";
$currentTitle = $_POST["title"];
$currentFirstName = $_POST["firstName"];
$currentdLastname = $_POST["lastName"];
$currentEmail = $_POST["emailAddress"];
$currentWebsite = $_POST["website"];
$currentMessage = $_POST["message"];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$currentMessage,$headers);
if($_SERVER["REQUEST_METHOD"] == "POST"){
//validate title
if($_POST["title"] == "0"){
$titleError = "Select a title";
}
//validate firstName
if (empty($_POST["firstName"])){//if empty
$firstNameError = "First name is required";
}
else{
$firstName = test_input($_POST["firstName"]);
// check if firstName only contains letters and whitespace
if(!preg_match("/^[a-zA-Z ]*$/",$firstName)){
$firstNameError = "Only letters and white space allowed";
}
}
if (empty($_POST["lastName"])){//if empty
$lastNameError = "Last name is required";
}
else{
$lastName = test_input($_POST["lastName"]);
// check if lastName only contains letters and whitespace
if(!preg_match("/^[a-zA-Z ]*$/",$lastNameError)){
$lastNameError = "Only letters and white space allowed";
}
}
if(empty($_POST["emailAddress"])){
$emailAddressError = "Email is required";
}
else{
$emailAddress = test_input($_POST["emailAddress"]);
// check if e-mail address is well-formed
if(!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)){
$emailAddressError = "Invalid email format";
}
}
if(empty($_POST["website"])){
$website = "";
}
else{
$website = test_input($_POST["website"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteError = "Invalid URL";
}
}
if(empty($_POST["comment"])){
$messageError = "Message required";
}
else{
$message = test_input($_POST["comment"]);
}
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div class="pseudoPage contact" data-item="contact">
<h2>Contact us</h2>
<p>We're always excited to work on a new project, so give us a shout!</p>
<p>Fill in the form or drop us a line at <a href="mailto:info@webdevsolutions.xxx">info@webdevsolutions.xxx</a></p>
<div class="contactForm">
<div class="contactFormPanel">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="control-group">
<label class="control-label" for="title">Title*</label>
<div class="controls">
<select id="title" name="title">
<option value="0">Please select</option>
<option value="1">Mr </option>
<option value="2">Mrs</option>
<option value="3">Ms</option>
<option value="4">Sir</option>
</select>
<span class="error"><?php echo $titleError;?></span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="firstName">First name*</label>
<div class="controls">
<input id="firstName" type="text" name="firstName" value="<?php echo $firstName;?>">
<span class="error"><?php echo $firstNameError;?></span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="lastName">Last name*</label>
<div class="controls">
<input id="lastName" type="text" name="lastName" value="<?php echo $lastName;?>">
<span class="error"><?php echo $lastNameError;?></span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="emailAddress">Email address*</label>
<div class="controls">
<input id="emailAddress" type="text" name="emailAddress" value="<?php echo $emailAddress;?>">
<span class="error"><?php echo $emailAddressError;?></span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="website">Website</label>
<div class="controls">
<input id="website" type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteError;?></span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="message">Enter your message*</label>
<div class="controls">
<textarea id="message" name="message"><?php echo $message;?></textarea>
<span class="error"><?php echo $messageError;?></span>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="button">
<!-- <a href="#">Submit</a> -->
<input type="submit" name="submit" value="Submit">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
Here is what it works:
First Name, Last Name, email address and website seem to validate correctly. Title is a bit strange: say I select Mr and then hit the submit button: if there is an error (perhaps because I didn't fill in the email address), the title field resets itself to "Please select": how do I make sure that whatever selection I've made stays there?
Message: that's another one, and same as title: if I type something, then submit, and again, say there is an error somewhere else in the form, the message in the textarea disappears and the error "message required" appears (but I did type the message in!) I thought I sorted it out by doing this <textarea id="message" name="message"><?php echo $message;?></textarea>
but obviously I was wrong. How do I retain the message?
As far as sending the form to the email address specified, well, what to say, I'm a bit lost there. Looking on the net, I noticed that mail()
needs a few parameters, so I have come up with this:
//mail variables
$to = "bassabasso@gmail.com";
$subject = "New request";
$currentTitle = $_POST["title"];
$currentFirstName = $_POST["firstName"];
$currentdLastname = $_POST["lastName"];
$currentEmail = $_POST["emailAddress"];
$currentWebsite = $_POST["website"];
$currentMessage = $_POST["message"];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$currentMessage,$headers);
Trouble is that I need to send also other things like, title, Name, email etc, and the manual says taht you can have additional parameters but I don't quite understand how:
is it something like this: mail($to,$subject,$currentMessage,$headers,$currentTitle,$currentFirstName,$currentdLastname,$currentEmail,$currentWebsite,);
?
Finally, how do I get the email to be sent only if the validation has been successful? Currently everytime I press submit I get about 3 empty emails coming through
Any help will be much appreciated
thanks