I made a contact page with a form and the issue is that if I send the email and the error is caused by PHPmailer than the rest of the page does not render But if it is a validation error than the entire page renders as it should.
Unfortunately I am running on localhost so I understand that there will be connection errors when sending but how would I be able to make the page continue to render after the error has been found?
HTML that includes the PHP files :
include_once('validate.php');
than the PHP file that is included I am aware there will be an error connection:
<?php
$name = $srn = $email = $txt = "";
$border1 = $border2 = $border3 = $border4 = "";
$errornum1 = $errornum2 = $errornum3 = $errornum4 = 0;
$errorname = $errorsrn = $errormail = $errortxt = $secondcap = "";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = preg_replace('/\s+/',' ', $data);
return $data;
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if($_POST['hidden'] !== ""){
$secondcap ='<span style="text-align:center;background-color:RGBA(255, 255, 255, 0.7);position:absolute;left:0px;width:100%;color:#f00;font-size:1.2em;">There Appears to be an issue, Please try again</span>';
}else{
$secondcap = "";
if($_POST['name'] == ""){
$errorname = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Name Field is Empty</span>';
$name = "";
$border1 = 'style="border:#f00 solid 1px;"';
$errornum1 = 1;
}else{
if(!preg_match("/^[a-zA-Z ]*$/",$_POST['name'])){
$errorname = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Name Field contains invalid characters</span>';
$name = "";
$border1 = 'style="border:#f00 solid 1px;"';
$errornum1 = 1;
}else{
$errorname = "";
$name = test_input($_POST['name']);
$border1 = 'style="border:#2f2 solid 1px;"';
$errornum1 = 0;
}
}
if($_POST['srn'] == ""){
$errorsrn = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Surname Field is Empty</span>';
$srn = "";
$border2 = 'style="border:#f00 solid 1px;"';
$errornum2 = 1;
}else{
if(!preg_match("/^[a-zA-Z ]*$/",$_POST['srn'])){
$errorsrn = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Surname Field contains invalid characters</span>';
$srn = "";
$border2 = 'style="border:#f00 solid 1px;"';
$errornum2 = 1;
}else{
$errorsrn = "";
$srn = test_input($_POST['srn']);
$border2 = 'style="border:#2f2 solid 1px;"';
$errornum2 = 0;
}
}
if($_POST['email'] == ""){
$errormail = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Email Field is Empty</span>';
$email = "";
$border3 = 'style="border:#f00 solid 1px;"';
$errornum3 = 1;
}else{
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$errormail = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Email Field Requires an "@Service" and ".Web" E.G thisemail@gmail.com</span>';
$email = "";
$border3 = 'style="border:#f00 solid 1px;"';
$errornum3 = 1;
}else{
$errormail = "";
$email= test_input($_POST['email']);
$border3 = 'style="border:#2f2 solid 1px;"';
$errornum3 = 0;
}
}
if($_POST['text'] == ""){
$errortxt = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Message is Empty</span>';
$txt= test_input($_POST['text']);
$border4 = 'style="border:#f00 solid 1px;"';
$errornum4 = 1;
}else{
if(!preg_match("/^[a-zA-Z ]*$/",$_POST['text'])){
$errortxt = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Message field Contains Invalid characters</span>';
$txt= "";
$border4 = 'style="border:#f00 solid 1px;"';
$errornum4 = 1;
}else{
$errortxt = "";
$txt= test_input($_POST['text']);
$border4 = 'style="border:#2f2 solid 1px;"';
$errornum4 = 0;
}
}
if($errornum1 + $errornum2 + $errornum3 + $errornum4 == 0){
require("../PHPMailer-5.2-stable/PHPMailerAutoload.php");
require '../PHPMailer-5.2-stable/class.phpmailer.php';
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
$mail->SMTPDebug = 0;
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Port = 465;
$mail->Username = "****"; // SMTP username
$mail->Password = "***"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("****", "****");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->Body = $txt;
$mail->AltBody = $txt;
if(!$mail->Send()){
$secondcap = '<h3 style="color:#f00;text-align:center;">There Was an error sending your email please try again</h3>';
return;
}else{
$secondcap = '<h3 style="color:#0f0">Your Email has been sent</h3>';
return;
}
}
}
}
?>
<htmL>
<form method="post" action="contact.php#head" autocomplete="off">
<h2>Form</h2>
<?php echo $secondcap; ?>
<div class="form">
<label>Name<?php echo $errorname; ?></label><input type="text" <?php echo $border1; ?> name="name" value="<?php echo $name ?>"><br>
<label>Surname <?php echo $errorsrn; ?></label><input type="text" <?php echo $border2; ?> name="srn" value="<?php echo $srn ?>"> <br>
<label>Email <?php echo $errormail; ?></label><input type="email" <?php echo $border3; ?> name="email" value="<?php echo $email ?>"><br>
<label style="display:none;">Hidden</label><input type="text" name="hidden" style="display:none">
</div>
<div class="form line">
<label>Message <?php echo $errortxt; ?></label><textarea name="text" <?php echo $border4; ?> ><?php echo $txt ?></textarea>
</div>
<div class="form">
<!-- <h3>Please, complete puzzle to submit</h3> -->
<input type="submit" name="submit" value="Send">
</div>
</form>
</html>
The rest of the page that isn't loading after the PHP is found in the HTML page:
<div id="map">
<script>
function initMap() {
var uluru = {lat: -26.209248, lng: 28.254368};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
</div>
<footer>
<p>Join us on social media:</p>
<a href="https://www.facebook.com/****" target="_blank" class="fa fa-facebook"> Facebook</a>
<a href="https://twitter.com/****" target="_blank" class="fa fa-twitter"> Twitter</a>
</footer>
Bonus question: how can I add the email and password Securely without it being visible in the document? Regarding this form will it generally work when I make it live or is there still things I need to input?