HI guys, I’m new to the forum and to php as well, in the sense that I don’t have any real and solid php coding experience, I’ve only “played” with it a few times.
Now, I have to build this php form and, considering the lack of knowledge I think I’ve done OK so far, but there are a few obstacles that I don’t seem to be able to overcome alone.
So, before posting the code, here is what I’m supposed to build:
-An HTML contact form that has php validation and that upon submission emails the content to an email address
I’ve done a bit of reading, looked at quite a few tutorials and although not everything is clear I do have some other programming experience plus HTML and CSS so hopefully it won’t be hard.
I’ve built the form, even done the validation (it seems to work) and the submission with the email, but here are the things that I couldn’t get to work:
1)I have a title field with a few options (first option is “Please select”) and when I select one of them and press the submit button,if the form contains an error and reloads, the option selected in the title field reverts back to the default “Please select”: how do I make sure that the selected option remains selected throughout the process?
2)When I click submit the page jumps up to the top. That’s expected behaviour of course but I can’t use a e.preventDefault()
in jquery otherwise the form won’t submit, is there any way in php to stop the page jumping up when the form is submitted? Ideally, what I’d like is to display a thank you message when the form has been submitted
3)Once the submission has occurred, the data typed in the form is still there: shouldn’t that disappear automatically?
4)the email I receive after the form has been submitted isn’t properly formatted in the sense that everything runs on the same line even if I’ve used “/r/n”.
So, here is the form with the php script and if you want to see it in action here is the link to the site (just click get in touch and you’ll see the form http://antonioborrillo.co.uk/agency_test/test/en/index.php)
[php]
<?php //create variables for validation $titleError = ""; $firstNameError = ""; $lastNameError = ""; $emailAddressError = ""; $messageError = ""; $websiteError = ""; $titleError = ""; $title = ""; $firstName = ""; $lastName = ""; $emailAddress = ""; $message = ""; $website = ""; //mail variables $to = "[email protected]"; $subject = "New request"; $currentTitle = $_POST["title"]; $currentMessage .= "Title: " . $_POST["title"] . "\r\n"; $currentMessage = "First Name: " . $_POST["firstName"] . "\r\n"; $currentMessage .= "Last Name: " . $_POST["lastName"] . "\r\n"; $currentMessage .= "Email address: " . $_POST["emailAddress"] . "\r\n"; $currentMessage .= "Website: " . $_POST["website"] . "\r\n"; $currentMessage .= "Message: " . $_POST["message"] . "\r\n"; //keep track of how many errors $errors = 0; $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"){//has the form been submitted? //validate title if($_POST["title"] == "0"){ $titleError = "Select a title"; $errors = 1; } else{ $title = $_POST["title"]; $errors = 0; } /* if( 0 != $_POST['title'] ) { $title = $_POST['title']; } else { $titleError = "required"; } */ //validate firstName if (empty($_POST["firstName"])){//if empty $firstNameError = "First name is required"; $errors = 1; } 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"; $errors = 1; } else{ $errors = 0; } } if (empty($_POST["lastName"])){//if empty $lastNameError = "Last name is required"; $errors = 1; } 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"; $errors = 1; } else{ $errors = 0; } } if(empty($_POST["emailAddress"])){ $emailAddressError = "Email is required"; $errors = 1; } 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"; $errors = 1; } else{ $errors = 0; } } if(empty($_POST["website"])){ $website = ""; $errors = 0; } 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"; $errors = 1; } else{ $errors = 0; } } if(empty($_POST["message"])){ $messageError = "Message required"; $errors = 1; } else{ $message = test_input($_POST["message"]); $errors = 0; } if($errors == 0){ mail($to,$subject,$currentMessage,$headers); } /* else{ return false; } */ } function test_input($data){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>[/php]
<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:[email protected]">[email protected]</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>
What I would be extremely grateful is that if somebody can have a quick glance at the code and help me out with the above issues. If it turns out that what I’ve done is rubbish, then I’m more than happy to re-write the whole thing but I’ll need some help getting it right