Page 1 of 1

Bootstrap php contact form: php wont sent email

Posted: Sat Apr 15, 2017 9:53 pm
by cjkeane
Hi everyone.

I've been struggling for quite awhile on getting a bootstrap contact form to send email.

1. I use bootstrap form validation
2. I use fontawesome
3. I use phpmailer for sending email.

The form looks good, its the way I want it and the bootstrap validation works. The captcha validation works too.. It seems that my php code doesn't run at all, no success or failure messages display and no email is sent. I'd appreciate it if someone could point me in the right direction. I haven't had this issue before.

Code: Select all

<?php
 ob_start();
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Starter Template for Bootstrap</title>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
    <link href="css/starter-template.css" rel="stylesheet">
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>  
 	<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>  
    <!-- bootstrap handles validation so php validation error checks not needed-->
  	<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/css/bootstrapValidator.min.css"/>
  	<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/js/bootstrapValidator.min.js"> </script>
    <script src='https://www.google.com/recaptcha/api.js'></script>
    <script src="js/validate-me.js"></script>
</head>
<body>
	<div class="container">
	    <form class="well form-horizontal" action="" method="post"  id="contact_form">
	        <fieldset>
		    <legend>Contact Us Today!
		        <?php 
   				if(isset($_POST['submit'])) {
					echo 'submit button clicked';
					$FullName = isset($_POST['fullname']) ? trim($_POST['fullname']) : '';
					$EmailAddress = isset($_POST['email_address']) ? trim($_POST['email_address']) : '';
					$CompanyPosition = isset($_POST['company_position']) ? trim($_POST['company_position']) : '';
					$CompanyName = isset($_POST['company_name']) ? trim($_POST['company_name']) : '';
					$PhoneNumber = isset($_POST['phone_number']) ? trim($_POST['phone_number']) : '';
					$Country = isset($_POST['country']) ? trim($_POST['country']) : '';
					$Location = isset($_POST['location']) ? trim($_POST['location']) : '';
					$ServicesRequired = isset($_POST['services_required']) ? trim($_POST['services_required']) : '';
					$CasesPerYear = isset($_POST['cases_per_year']) ? trim($_POST['cases_per_year']) : '';
					$Comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
					if(isset($_POST['g-recaptcha-response']))	{$captcha=$_POST['g-recaptcha-response']; }	
						$errors = array(); 
						//if (empty($_POST['full_name']) ) {$errors[]='Full Name Required';}
						//if (empty($_POST['company_position']) ) {	$errors[]='Company Position Required';	}
						//if (empty($_POST['company_name']) ) {$errors[]='Company Name Required';}
						//if (empty($_POST['phone_number']) ) {$errors[]='Phone Number Required';	}
						//if (empty($_POST['email_address'])) {$errors[]='Email Address Required';}
						//if (empty($_POST['country']) ) {$errors[]='Country Required';	}
						//if (empty($_POST['location']) ) {$errors[]='Location Required';	}
						//if (empty($_POST['services_required']) ) {$errors[]='Services Requires is Required';	}
						//if (empty($_POST['cases_per_year']) ) {$errors[]='Cases Per Year is Required';	}
						//if (empty($_POST['comment']) ) {$errors[]='Comment is Required';	}
						if(!$captcha){ $errors[]='<div class="alert alert-danger" role="alert" id="captcha_message">Failure <i class="glyphicon glyphicon-thumbs-down"></i> The Captcha code was invalid.</div>'; }
							$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LennxwUAAAAADsh9_YK-7A2EAILA35i5mN_tEpj&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
							if($response.success==false) {
								echo '<div class="alert alert-danger" role="alert" id="captcha_message">Failure <i class="glyphicon glyphicon-thumbs-down"></i> The Captcha code was invalid.</div>';
							} else {
								if (empty($errors)) { 
									$to = 'cjkeane@shaw.ca';
									$ContactEmail = 'cjkeane@shaw.ca';
									$subject = 'RE: Request A Quote submitted from megassistance.com';
									$message = "
									<html>
										<body>
											<table rules='all' style='border-color: #666;' cellpadding='10' width='697' >
												<tr><td colspan='2'><h1><strong>Megassistance.com: Request A Quote</strong></h1></td></tr>
												<tr><td colspan='2'><h2 style='background: #eaf1dd; color:#660066'><strong>Contact Information</strong> for:</h2></td></tr>
												<tr><td width='323'> Name</td><td width='374'>$FullName</td></tr>
												<tr><td>Email Address</td><td>$EmailAddress</td></tr>
												<tr><td>Job Position</td><td>$JobPosition</td></tr>
												<tr><td>Company Name</td><td>$CompanyName</td></tr>
												<tr><td> Phone Number</td><td>$PhoneNumber</td></tr>
												<tr><td>E-Mail Address</td><td>$ContactEmail</td></tr>
												<tr><td>Country</td><td>$Country</td></tr>
												<tr><td>Location</td><td>$Location</td></tr>
												<tr><td>Services Required</td><td>$ServicesRequired</td></tr>
												<tr><td>Number of Cases Per Year</td><td>$CasesPerYear</td></tr>
												<tr><td>Comments:</td><td>$Comments</td></tr>
											</table>
										</body>
									</html>";  
									require_once 'class.phpmailer.php';
									$mail = new PHPMailer();
                                	$mail->CharSet = 'UTF-8';
                                	$mail->IsHTML(true);
					$mail->From     = '$EmailAddress';
                                	//$mail->FromName = 'cjkeane@shaw.ca';
					$toaddresses = 'cjkeane@shaw.ca';
					$mail->Subject = "RE: Request A Quote";
                                	$mail->Body =  $message; 
                                	if(!$mail->Send()) {
                                    	echo '<div class="alert alert-danger" role="alert" id="failure_message">Message Failed <i class="glyphicon glyphicon-thumbs-down"></i>>Sorry there was an error sending your message. </div><br/>';
                                	} else {
						echo '<div class="alert alert-success" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> Thanks for contacting us, we will get back to you shortly.</div><br />';
                                   }	
			   	} else { 
                            		echo '<div class="alert alert-danger" role="alert" id="failure_message">Failure <i class="glyphicon glyphicon-thumbs-down"></i>>Sorry there were validation errors. Please try again.</div>';
                                }        
                            }
                        }
					?>
                </legend>
		<div class="form-group">
        	        <label class="col-md-4 control-label">Full Name</label>
        		<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
            				<input name="full_name" placeholder="Name" class="form-control" id="ContactName" type="text">
          				</div>
        			</div>
      			</div>
			    <div class="form-group">
        			<label class="col-md-4 control-label">E-Mail Address</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
            				<input name="email_address" placeholder="E-Mail Address" class="form-control" type="text">
          				</div>
        			</div>
      			</div>
		      	<div class="form-group">
        			<label class="col-md-4 control-label">Job Position</label>
        				<div class="col-xs-6 inputGroupContainer">
          					<div class="input-group">
            					<span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
            					<input name="company_position" placeholder="Position" class="form-control" type="text">
          					</div>
        				</div>
      			</div>
        	    <div class="form-group">
        			<label class="col-md-4 control-label">Company Name</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
            				<input name="company_name" placeholder="Company Name" class="form-control" type="text">
          				</div>
        			</div>
      			</div>
     			<div class="form-group">
        			<label class="col-md-4 control-label">Phone Number</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
            				<input name="phone" placeholder="(845)555-1212" class="form-control" type="text">
          				</div>
        			</div>
      			</div>
      			<div class="form-group">
        			<label class="col-md-4 control-label">Your Location</label>
        			<div class="col-xs-6 selectContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
            				<select name="location" class="form-control selectpicker">
      							<option value=" " >Please select your location </option>
      							<option>Canada</option>
      							<option>USA</option>
      							<option >Latin America</option>
      							<option >Europe</option>
      							<option >Africa</option>
      							<option >Asia</option>
      							<option >Middle East</option>
      							<option >Others</option>
    					    </select>
          				</div>
        			</div>
      			</div>
        		<div class="form-group">
        			<label class="col-md-4 control-label">Services Required</label>
        			<div class="col-xs-6 selectContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
            				<select name="services_required" class="form-control selectpicker"  multiple="multiple">
       							<option value="1">Outpatient visits (housecall, urgent care, walk in clinic, emergency room)</option>
        						<option value="2">Inpatient visits (hospitals)</option>
        						<option value="3">Repatriation</option>
        						<option value="4">Costs containment</option>
        						<option value="5">Others</option>
							</select>
          				</div>
        			</div>
      			</div>
        		<div class="form-group">
        			<label class="col-md-4 control-label">Number of cases per year</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
            				<input name="cases_per_year" placeholder="Number of cases per year" class="form-control" type="text">
          				</div>
        			</div>
      			</div>   
     		    <div class="form-group">
        			<label class="col-md-4 control-label">Your Comment</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
            				<textarea class="form-control" name="comment" placeholder="Comment"></textarea>
          				</div>
        			</div>
      			</div>
      			<!-- Success/Fail message -->
       			<?php echo $result; ?> 
			</fieldset>
     		<label class="col-md-4 control-label"></label>
        	<div class="col-md-4"><div class="g-recaptcha" data-sitekey="6LennxwUAAAAAG7YqFDJQ2GvbzZulMNuTdKHcQOW"></div></div>
     		<div class=clearfix></div>
		    <div class="form-group">
        		<label class="col-md-4 control-label"></label>
        		<div class="col-md-4">
         			<br /> <input type="submit" name="submit" id="submit" class="btn btn-warning">
        		</div>
      		</div>
		</form>
	</div>
    
<?php
ob_end_flush();
?>
  </body>
</html>



Re: Bootstrap php contact form: php wont sent email

Posted: Sun Apr 16, 2017 7:22 am
by Celauran
That's a whole lot of mixed concerns, making it somewhat difficult to parse. First, have you checked your error log? Second, I notice

Code: Select all

$mail->From     = '$EmailAddress';
This sets the from email to the literal string $EmailAddress. There's no variable interpolation inside single quotes, only inside double quotes. Here, however, there isn't any need for quotes. Finally, are you certain emails are not being sent, or are they simply not being received? Those are two very different scenarios.

Re: Bootstrap php contact form: php wont sent email

Posted: Sun Apr 16, 2017 6:14 pm
by cjkeane
i figured out the issue with the $mail->From = $EmailAddress last night and I rewrote simpler code excluding the bootstrap validation.
1. I now have the emails sending
2. I use php for error catching.
3. My only issue now is that even though there is a captcha error, the email still sends. Any ideas would be appreciated. Thanks.

Code: Select all

<?php 
    $error = false;
    if(isset($_POST['submit']) && !empty($_POST['submit'])) {
		$full_name = isset($_POST['full_name']) ? trim($_POST['full_name']) : '';
		$email_address = isset($_POST['email_address']) ? trim($_POST['email_address']) : '';
		$job_position = isset($_POST['job_position']) ? trim($_POST['job_position']) : '';
		$company_name = isset($_POST['company_name']) ? trim($_POST['company_name']) : '';
		$phone_number = isset($_POST['phone_number']) ? trim($_POST['phone_number']) : '';
		$location = isset($_POST['location']) ? trim($_POST['location']) : '';
		$services_required = isset($_POST['services_required']) ? trim($_POST['services_required']) : '';
		$cases_per_year = isset($_POST['cases_per_year']) ? trim($_POST['cases_per_year']) : '';
		$comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
					 
		if (!preg_match("/^[a-zA-Z ]+$/",$full_name))  { $error = true; $full_name_error = "Please Enter Valid Full Name"; }
		if(!filter_var($email_address,FILTER_VALIDATE_EMAIL)) {  $error = true; $email_address_error = "Please Enter Valid Email Address"; }
		if(empty($job_position)) { $error = true; $job_position_error = "Please Enter Your Job Position"; }
		if(empty($company_name)) { $error = true; $company_name_error = "Please Enter Your Company Name"; }
		if(empty($phone_number)) { $error = true; $phone_number_error = "Please Enter Your Phone Number"; }
		if(empty($location)) { $error = true; $location_error = "Please Select a Location"; }
		if(empty($services_required)) { $error = true; $services_required_error = "Please Select at least one Service"; }
		if(empty($cases_per_year)) { $error = true; $cases_per_year_error = "Please specify the number of cases you deal with per year."; }
		if(empty($comment)) { $error = true; $comment_error = "Please Enter Your Comments"; }
		if(isset($_POST['g-recaptcha-response']))	{$captcha=$_POST['g-recaptcha-response']; }	
		if(!$captcha){ echo '<div class="alert alert-danger" role="alert" id="captcha_message">Failure <i class="glyphicon glyphicon-thumbs-down"></i> Please check the captcha form.</div>'; }
		
		$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LennxwUAAAAADsh9_YK-7A2EAILA35i5mN_tEpj&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);	
		
    	if($response.success==false) {
	       	echo '<div class="alert alert-danger" role="alert" id="captcha_message">Failure <i class="glyphicon glyphicon-thumbs-down"></i> Please check the captcha form.</div>';
       	} else {
			if (!$error) {
				$to_email = "cjkeane@shaw.ca";
				//$subject = "Request A Quote from " . $full_name;
				$body = "
					<html>
						<body>
							<table rules='all' style='border-color: #666;' cellpadding='10' width='697' >
								<tr><td colspan='2'><h1><strong>Megassistance.com: Request A Quote</strong></h1></td></tr>
								<tr><td colspan='2'><h2 style='background: #eaf1dd; color:#660066'><strong>Contact Information</strong> for:</h2></td></tr>
								<tr><td width='323'> Name</td><td width='374'>$full_name</td></tr>
								<tr><td>Email Address</td><td>$email_address</td></tr>
								<tr><td>Job Position</td><td>$job_position</td></tr>
								<tr><td>Company Name</td><td>$company_name</td></tr>
								<tr><td> Phone Number</td><td>$phone_number</td></tr>
								<tr><td>Location</td><td>$location</td></tr>
								<tr><td>Services Required</td><td>$services_required</td></tr>
								<tr><td>Number of Cases Per Year</td><td>$cases_per_year</td></tr>
								<tr><td>Comments:</td><td>$comment</td></tr>
							</table>
						</body>
					</html>";  
					require_once 'class.phpmailer.php';
					$mail = new PHPMailer;
					$mail->CharSet = 'UTF-8';
					$mail->IsHTML(true);
					$mail->From = $email_address;
					$mail->FromName = $email_address;
					$mail->addAddress('cjkeane@shaw.ca');
					$mail->Subject = "RE: Request A Quote";
					$mail->Body     = $body;
					if(!$mail->send()) {
						echo '<div class="alert alert-danger" role="alert" >Message Failed <i class="glyphicon glyphicon-thumbs-down"></i>>Sorry there was an error sending your message. </div><br/>';
		  			} else {
		  				echo '<div class="alert alert-success" role="alert">Success <i class="glyphicon glyphicon-thumbs-up"></i> Thanks for contacting us, we will get back to you shortly.</div><br />';
					}
				}
			}
		}
	?>
<!DOCTYPE html>
<html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Starter Template for Bootstrap</title>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
    <link href="css/starter-template.css" rel="stylesheet">
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>  
 	<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>  
    <!-- bootstrap handles validation so php validation error checks not needed-->
  	<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/css/bootstrapValidator.min.css"/>
  	<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/js/bootstrapValidator.min.js"> </script>
    <script src='https://www.google.com/recaptcha/api.js'></script>
    
</head>
<body>
<div class="container">
         	<form role="form" class="well form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"  id="contact_form">
        
            
            <fieldset>
				<legend>Contact Us Today!</legend>
               
               <div class="form-group">
        			<label class="col-md-4 control-label">Full Name</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
                        	<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
            				<input class="form-control" name="full_name" placeholder="Your Full Name" type="text" value="<?php if($error) echo $full_name; ?>" />
                        </div>
        			</div>
                    <div class="col-md-4"></div>
                    <div class="col-sm-6"><span class="text-danger"><?php if (isset($full_name_error)) echo $full_name_error; ?></span></div>
      			</div>
               <div class="form-group">
        			<label class="col-md-4 control-label">E-Mail Address</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
                        	<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
            				<input class="form-control" name="email_address" placeholder="Your Email Address" type="text" value="<?php if($error) echo $email_address; ?>" />
                        	
          				</div>
                    </div>
                     <div class="col-md-4"></div>
                    <div class="col-sm-6"><span class="text-danger"><?php if (isset($email_address_error)) echo $email_address_error; ?></span> </span></div>
      			</div>
                <div class="form-group">
        			<label class="col-md-4 control-label">Job Position</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
            				<input name="job_position" placeholder="Position" class="form-control" type="text" value="<?php if($error) echo $job_position; ?>">
                           
          				</div>
        			</div>
                    <div class="col-md-4"></div>
                    <div class="col-sm-6"><span class="text-danger"> <span class="text-danger"><?php if (isset($job_position_error)) echo $job_position_error; ?></span></span> </span></div>
      			</div>
                 <div class="form-group">
        			<label class="col-md-4 control-label">Company Name</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
            				<input name="company_name" placeholder="Company Name" class="form-control" type="text" value="<?php if($error) echo $company_name; ?>">
          				</div>
        			</div>
                    <div class="col-md-4"></div>
                    <div class="col-sm-6"><span class="text-danger"> <span class="text-danger"><?php if (isset($company_name_error)) echo $company_name_error; ?></span></span> </span></div>
      			</div>
                <div class="form-group">
        			<label class="col-md-4 control-label">Phone Number</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
            				<input name="phone_number" placeholder="(845)555-1212" class="form-control" type="text" value="<?php if($error) echo $phone_number; ?>">
          				</div>
        			</div>
                     <div class="col-md-4"></div>
                    <div class="col-sm-6"><span class="text-danger"> <span class="text-danger"><?php if (isset($phone_number_error)) echo $phone_number_error; ?></span></span> </span></div>
      			</div>

				<div class="form-group">
        			<label class="col-md-4 control-label">Your Location</label>
        			<div class="col-xs-6 selectContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
            				<select name="location" class="form-control selectpicker">
      							<option value=" " >Please select your location </option>
      							
                                 <option value="Canada" <?php if ($_POST['location'] == 'Canada') echo 'selected="selected"'; ?> >Canada</option>
								 <option value="USA" <?php if ($_POST['location'] == 'USA') echo 'selected="selected"'; ?> >USA</option>
                                  <option value="Latin America" <?php if ($_POST['location'] == 'Latin America') echo 'selected="selected"'; ?> >Latin America</option>
								  <option value="Europe" <?php if ($_POST['location'] == 'Europe') echo 'selected="selected"'; ?> >Europe</option>
  <option value="Africa" <?php if ($_POST['location'] == 'Africa') echo 'selected="selected"'; ?> >Africa</option>
                                  <option value="Asia" <?php if ($_POST['location'] == 'Asia') echo 'selected="selected"'; ?> >Asia</option>
                                  <option value="Middle East" <?php if ($_POST['location'] == 'Middle East') echo 'selected="selected"'; ?> >Middle East</option>
      							<option value="Others" <?php if ($_POST['location'] == 'Others') echo 'selected="selected"'; ?> >Others</option>
      							
      									
      							
      							
      							
    					    </select>
          				</div>
        			</div>
                     <div class="col-md-4"></div>
                    <div class="col-sm-6"><span class="text-danger"> <span class="text-danger"><?php if (isset($location_error)) echo $location_error; ?></span></span> </span></div>
      			</div>
				<div class="form-group">
        			<label class="col-md-4 control-label">Services Required</label>
        			<div class="col-xs-6 selectContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
            				<select name="services_required" class="form-control selectpicker"  multiple="multiple">
       						
                             <option value="Outpatient visits (housecall, urgent care, walk in clinic, emergency room)" <?php if ($_POST['services_required'] == 'Outpatient visits (housecall, urgent care, walk in clinic, emergency room)') echo 'selected="selected"'; ?> >Outpatient visits (housecall, urgent care, walk in clinic, emergency room)</option>
							 <option value="Inpatient visits (hospitals)" <?php if ($_POST['services_required'] == 'Inpatient visits (hospitals)') echo 'selected="selected"'; ?> >Inpatient visits (hospitals)</option>
                             <option value="Repatriation" <?php if ($_POST['services_required'] == 'Repatriation') echo 'selected="selected"'; ?> >Repatriation</option>
							 <option value="Costs containment" <?php if ($_POST['services_required'] == 'Costs containment') echo 'selected="selected"'; ?> >Costs containment</option>
							 <option value="Others" <?php if ($_POST['services_required'] == 'Others') echo 'selected="selected"'; ?> >Others</option>
                                
                         
							</select>
          				</div>
        			</div>
                     <div class="col-md-4"></div>
                    <div class="col-sm-6"><span class="text-danger"> <span class="text-danger"><?php if (isset($services_required_error)) echo $services_required_error; ?></span></span> </span></div>
      			</div>
				<div class="form-group">
        			<label class="col-md-4 control-label"># of cases per year</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
            				<input name="cases_per_year" placeholder="Number of cases per year" class="form-control" type="text" value="<?php if($error) echo $cases_per_year; ?>">
          				</div>
        			</div>
                      <div class="col-md-4"></div>
                    <div class="col-sm-6"><span class="text-danger"> <span class="text-danger"><?php if (isset($cases_per_year_error)) echo $cases_per_year_error; ?></span></span> </span></div>
      			</div>  
				<div class="form-group">
        			<label class="col-md-4 control-label">Your Comment</label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
            				<textarea class="form-control" name="comment" placeholder="Comment"><?php if($error) echo $comment; ?></textarea>
          				</div>
        			</div>
                       <div class="col-md-4"></div>
                    <div class="col-sm-6"><span class="text-danger"> <span class="text-danger"><?php if (isset($comment_error)) echo $comment_error; ?></span></span> </span></div>
      			</div>
				
                <label class="col-md-4 control-label"></label>
        	<div class="col-md-4"><div class="g-recaptcha" data-sitekey="6LennxwUAAAAAG7YqFDJQ2GvbzZulMNuTdKHcQOW"></div></div>
     		<div class="clearfix"></div>
		                    
                <div class="form-group">
        			<label class="col-md-4 control-label"></label>
        			<div class="col-xs-6 inputGroupContainer">
          				<div class="input-group">
            				<br /> <input name="submit" type="submit" class="btn btn-primary" value="Send" />
          				</div>
        			</div>
      			</div>
            </fieldset>
            </form>
        </div>
</body>
</html>


Re: Bootstrap php contact form: php wont sent email

Posted: Sun Apr 16, 2017 8:50 pm
by Celauran

Code: Select all

if($response.success==false) {
Check what you're getting back. Might be an object, might be an array, and probably needs to be JSON decoded. In any case, PHP does not use dot notation, so $response.success isn't a thing.

Re: Bootstrap php contact form: php wont sent email

Posted: Tue Apr 18, 2017 1:50 pm
by cjkeane
I got it to work thanks! I changed it a bit and then decoded the json

Code: Select all

$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$my_secret."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);	
            	$googleobj = json_decode($response);
            	$verified = $googleobj->success;
            	if ($verified === true){
                	if (!$error) {
my only issue now is how to retain 'all' the selected values in a multiple select list after the form is submitted in case there are validation errors. I can retain 1, but not all. Any suggestions would be helpful. Thanks.

Code: Select all

<select name="services_required[]" class="form-control selectpicker"  multiple="multiple">
	<option value="Outpatient visits (housecall, urgent care, walk in clinic, emergency room)" <?php if ($_POST['services_required'] == 'Outpatient visits (housecall, urgent care, walk in clinic, emergency room)') echo 'selected="selected"'; ?> >Outpatient visits (housecall, urgent care, walk in clinic, emergency room)</option>
	<option value="Inpatient visits (hospitals)" <?php if ($_POST['services_required'] == 'Inpatient visits (hospitals)') echo 'selected="selected"'; ?> >Inpatient visits (hospitals)</option>
	<option value="Repatriation" <?php if ($_POST['services_required'] == 'Repatriation') echo 'selected="selected"'; ?> >Repatriation</option>
	<option value="Costs containment" <?php if ($_POST['services_required'] == 'Costs containment') echo 'selected="selected"'; ?> >Costs containment</option>
	<option value="Others" <?php if ($_POST['services_required'] == 'Others') echo 'selected="selected"'; ?> >Others</option>
</select>

Re: Bootstrap php contact form: php wont sent email

Posted: Tue Apr 18, 2017 2:49 pm
by Celauran
With the current setup, you're only allowing one value. You need $_POST['services_required'] to be an array rather than a string.

Code: Select all

<select name="services_required[]" class="form-control selectpicker"  multiple="multiple">
Then, rather than checking if $_POST['services_required'] equals a certain value, you can check if the array contains said value using something like in_array

Re: Bootstrap php contact form: php wont sent email

Posted: Tue Apr 18, 2017 3:16 pm
by cjkeane
I think I figured it out.

Code: Select all

<select name="services_required[]" class="form-control selectpicker"  multiple="multiple">
<?php 
    $services = array("Outpatient visits (housecall, urgent care, walk in clinic, emergency room)","Inpatient visits (hospitals)","Repatriation","Costs containment","Others");
    $choosen_services = $_POST['services_required'];
    for ($i=0;$i<count($services);$i++)  {
        $selected = (in_array($services[$i],$choosen_services) ? 'selected="selected"' : ''); 
        echo "<option value='$services[$i]' $selected>$services[$i]</option>";
    } 
?>
</select>