I have an Html site.In that there have form for sending mail in Each page.The form action is done in another page that is process.php page.I want to redirect from php page to html page after sending mail.
This is my html code
<form name="mail" id="mail" method="post" action="process.php" onSubmit="return valid_form();">
<div class="form_bg11">
<div class="free_call_back">Free Callback Request</div>
<input name="txtname" id="txtname" type="text" class="free_cal_field" value="Your Name" onClick="(this.value='')" >
<input name="txtphone" id="txtphone" type="text" class="free_cal_field" value="Your Phone Number" onClick="(this.value='')">
<input type="submit" name="Submit" style="background-image: url('images/submit.png'); border: 0px none; height: 19px; margin-left: 85px; width: 56px; margin-top:5px;" value=" ">
</div>
</form>
Thi is my process.php page
<?php
if(isset($_POST['Submit']))
{
$name = $_POST['txtname'];
$phone = $_POST['txtphone'];
$to = "mariyadavis90#gmail.com";
$subject = "New Request Come !";
$message = '<div style="background-color: #EEEEEE;border-bottom: 1px solid #DCDCDC;padding: 10px 0 20px;width: 400px;">
<div style="width:400px;">
<div style="width:400px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc;">
<div style="width:300px; text-align:center;color:#666666;margin-left:20px;font-size:12px;font-weight:bold;">
Now you got a new request from :
</div>
</div> <!--end of div_form_main-->
<div style="width:400px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc;">
<div style="float:left;width:150px;color:#666666;margin-left:20px;font-size:12px;font-weight:bold;"> Name:</div>
<div style="float:left;width:auto;color:#000000;font-size:12px;font-weight:bold;">'.$name.'</div>
</div> <!--end of div_form_main-->
<!--end of div_form_main-->
<div style="width:400px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc;">
<div style="float:left;width:150px;color:#666666;margin-left:20px;font-size:12px;font-weight:bold;"> Phone Number:</div>
<div style="float:left;width:auto;color:#000000;font-size:12px;font-weight:bold;">'.$phone.'</div></div> <!--end of div_form_main-->
</div> <!--end of div_password_main-->';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .="Content-Transfer-Encoding: 8bit";
if(!mail($to, $subject, $message, $headers))
{
$msg = "Sending mail is failed !";
}
else
{
$msg = "Mail send Successfully !";
}
print "<script type=\"text/javascript\">";
print "alert('".$msg."')";
print "</script>";
}
?>
how can i do this?
I am a bit confused by your wording. Why can't you just use the PHP header() function?
header('Location: http://www.example.com/');
you can modify your procss.php (in your javascript block)
print "<script type=\"text/javascript\">";
print "alert('".$msg."')";
print "window.location.href='http://www.yourdomain/yourhtmlpage.html'";
print "</script>";
A quick write up
if(!mail($to, $subject, $message, $headers))
{
$msg = "Sending mail is failed !";
}
else
{
$msg = "Mail send Successfully !";
print "<script type=\"text/javascript\">";
print "window.location.href= 'www.example.com' ";
print "</script>";
}
print "<script type=\"text/javascript\">";
print "alert('".$msg."')";
print "</script>";
Related
contactfrm.php
<?php
// Server side validation
$name = $_POST['name'];
$surname = $_POST['surname'];
$bsubject = $_POST['subject'];
$email= $_POST['email'];
$message= $_POST['message'];
$to = "sample#email.com";
$subject = "Mail From sample 1";
$cc = "sample#email.com";
$txt ="";
$txt .="<html><body>";
$txt .= "<table style='border-collapse: collapse; width: 30%; background: #ddd7d769;'><tr>
<th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Name</th>";
$txt .= "<td style=' border: 1px solid #0000004f;text-align: left;padding: 8px;'>".$name."</td></tr>";
$txt .= "<tr><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Surname</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$surname."</td></tr>";
$txt .= "<tr><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Subject</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$bsubject."</td></tr>";
$txt .= "<tr style='background: lightgrey;'><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Email</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$email."</td></tr>";
$txt .= "<tr><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Message</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$message."</td></tr>";
$txt .= "</table>";
$txt .="</body></html>";
$headers = "From: Sample 1 . "\r\n" ;
$headers .= 'Cc: '.$cc . "\r\n";
$headers .= "MIME-Version: 1.0'.PHP_EOL'\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if(!empty($email))
{
mail($to,$subject,$txt,$headers);
$success_output = "Your message sent successfully, We will contact you soon. Thank You";
}
else
{
$error_output = "Something went wrong. Please try again later";
}
$output = array(
'error' => $error_output,
'success' => $success_output
);
// Output needs to be in JSON format
echo json_encode($output);
?>
Contact Form.html
<section id="contact" class="contact">
<div class="container" data-aos="fade-up">
<div class="section-title">
<h2>Contact</h2>
<p>Feel Free to contact</p>
</div>
<div class="row">
<div class="col-lg-5 d-flex align-items-stretch">
<div class="info">
<div class="address">
<a class="skype" href="skype:live:.cid.123456789?chat" target = "_blank"><i class="fa fa-skype" aria-hidden="true"></i></a>
<a class="skype" href="skype:live:.cid.123456789?chat" target = "_blank"><h4>Skype</h4></a>
<p>Click here to message us directly on Skype</p>
</div>
<div class="address">
<a class="whatsapp" href="https://wa.me/+123456789" target="_blank"><i class="fa fa-whatsapp" aria-hidden="true"></i></a>
<a class="whatsapp" href="https://wa.me/+123456789" target="_blank"><h4>WhatsApp</h4></a>
<p>Click here to message us directly on WhatsApp </p>
</div>
<div class="email">
<i class="fas fa-envelope"></i>
<h4>Email:</h4>
<p>sample#email.com</p>
</div>
<div class="phone">
<i class="fa fa-phone" aria-hidden="true"></i>
<h4>Call:</h4>
<p>+123456789</p>
</div>
</div>
</div>
<div class="col-lg-7 mt-5 mt-lg-0 d-flex align-items-stretch">
<form id="contactfrm" onsubmit="validateCaptcha()" method = "POST" role="form" class="php-email-form">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">First Name</label>
<input type="text" name="name" placeholder="Enter Your First Name" class="form-control" id="name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" required />
<div class="validate"></div>
</div>
<div class="form-group col-md-6">
<label for="name">Surname</label>
<input type="text" class="form-control" placeholder="Enter Your Surname" name="surname" id="surname" data-rule="minlen:4" data-msg="Please enter a valid email" />
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<label for="name">Your Email</label>
<input type="email" class="form-control" name="email" placeholder="example#example.com" id="email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validate"></div>
</div>
<div class="form-group">
<label for="name">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validate"></div>
</div>
<div class="form-group">
<label for="name">Message</label>
<textarea class="form-control" name="message" rows="10" data-rule="required" data-msg="Please write something for us" required></textarea>
<div class="validate"></div>
</div>
<div id="captcha">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Captcha" data-rule="required" data-msg="Please Enter the Captcha" id="cpatchaTextBox"/>
</div>
<div class="text-center" id="btnSend"><button type="submit">Send Message</button></div>
<div id="alert"></div>
</form>
</div>
</div>
</div>
</section><!-- End Contact Section -->
ajax script
<script async = "true">
var code;
function createCaptcha() {
document.getElementById('captcha').innerHTML = "";
var charsArray =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#";
var lengthOtp = 6;
var captcha = [];
for (var i = 0; i < lengthOtp; i++) {
var index = Math.floor(Math.random() * charsArray.length + 1);
if (captcha.indexOf(charsArray[index]) == -1)
captcha.push(charsArray[index]);
else i--;
}
var canv = document.createElement("canvas");
canv.id = "captcha";
canv.width = 100;
canv.height = 50;
var ctx = canv.getContext("2d");
ctx.font = "25px Georgia";
ctx.strokeText(captcha.join(""), 0, 30);
code = captcha.join("");
document.getElementById("captcha").appendChild(canv);
}
function validateCaptcha() {
event.preventDefault();
debugger
if (document.getElementById("cpatchaTextBox").value == code) {
// alert("Valid Captcha")
readCap();
}else{
alert("Invalid Captcha. try Again");
createCaptcha();
}
}
function readCap(){
$.ajax({
url: 'contactfrm.php',
type: 'post',
data: $('#contactfrm').serialize(),
dataType: 'json',
success: function( _response ){
var error = _response.error;
var success = _response.success;
console.log(success);
if(error != null) {
$('#alert').html(error);
}
else {
$('#alert').html(success);
$('#btnSend').remove();
}
},
error: function(jqXhr, json, errorThrown){
var error = jqXhr.responseText;
$('#alert').html(error);
}
});
}
</script>
I have 30 different websites with this same contact form at the bottom.
As shown in the image, When I receive an email, it says "Mail From (website Name)"
So, if I have to upload same php for 30 different websites, I'll have to make 30 different php forms with different titles and headers.
How can I add if else loop where I can target respective website and only its header will be displayed. Because right now I have created 10 different 'contactfrm.php' in which I have changed headers to respective website.
Replace this variable
$subject = "Mail From sample 1";
Into this code you will get your solution .
$regexp = "/([\w]{0,}).([\w]{2,3}|[\w]{2,3}.[\w]{2,3})$/";
$url= $_SERVER['SERVER_NAME'];
$input = parse_url($url, PHP_URL_HOST);
preg_match($regexp, $input, $matches);
$subject = "Mail From ".$matches[1]; //out put domain name
I am responsible for a job application form that does not seem to be working well. The problem is that emails from this form are not always receiving. The weird fact is that I also configured a CC email address in this script (another server) and that email address is always receiving mail in that inbox. Personally I think this is a server problem but maybe I am missing something.
UPDATE: Another website is running on that same server with a contact form that appears to be working fine.
See my code below.
Any help would be greatly appreciated.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>title</title>
<meta name="description" content="" />
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.9/css/all.css" integrity="sha384-5SOiIsAziJl6AWe0HWRKTXlfcSHKmYV4RBF18PPJ173Kzn7jzMyFuTtk8JA7QQG1" crossorigin="anonymous">
<link rel="stylesheet" href="style.css" crossorigin="anonymous">
<!--[if IE 9]><link rel="stylesheet" href="ie.css" type="text/css" /><![endif]-->
<!--[if IE 8]><link rel="stylesheet" href="ie.css" type="text/css" /><![endif]-->
<!--[if IE 7]><link rel="stylesheet" href="ie.css" type="text/css" /><![endif]-->
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<div class="container">
<div class="content">
<div style="display:flex; align-items:flex-start; justify-content:flex-start!important; padding:20px 0px 20px 0px; height:100%; flex-wrap:wrap;" class="wrapper">
<div class="block padding-left">
<div class="padding-mobile" style="width:100%; height:auto; display:flex; align-items:flex-start; justify-content:flex-start; margin-top:60px; flex-wrap:wrap; box-sizing:border-box;">
<h2 style="font-size:42px; margin:0px;">Apply</h2>
<form enctype="multipart/form-data" id="cv" class="formulier" method="POST" action="">
<p>
<label>First name:</label>
<input data-validation="required" type="text" name="naam" placeholder="Uw naam" />
</p>
<p>
<label>Last name:</label>
<input data-validation="required" type="text" name="achternaam" placeholder="Uw achternaam" />
</p>
<p>
<label>Telephone</label>
<input data-validation="required" type="text" name="telefoon" placeholder="Uw telefoonnummer" />
</p>
<p>
<label>Email address</label>
<input data-validation="required email" type="text" name="email" placeholder="Uw email adres" />
</p>
<p>
<label>Select a job application</label>
<select name="vacature" placeholder="Vacature" data-validation="length" data-validation-length="min1" data-validation-error-msg="Selecteer een vacature">
<option value="" disable selected>Vacature</option>
<option>- Open sollicitatie- </option>
<option>Administratief medewerker bedrijfskantoor (m/v)</option>
<option>Inkoper</option>
<option>Leerling Operators</option>
<option>Snijder (m/v)</option>
<option>Planner</option>
<option>Accountmanager Frankrijk</option>
<option>Offsetdrukker Vellen</option>
<option>Oproepkrachten</option>
<option>Weekend-/ vakantiewerkers</option>
<option>Assistent Machineoperator (m/v)</option>
<option>Offsetrotatiedrukkers Heatset (m/v)</option>
<option>Software developer</option>
<option>Allround onderhoudsmonteur</option>
<option>Magazijnmedewerker (m/v)</option>
</select>
</p>
<p style="background:#fff; padding:20px; margin-top:44px; color:#000; border-radius:6px;">
<label style="color:#555; text-shadow:none!important; font-size:18px;">Upload your CV</label>
<input type="file" name="cv" data-validation="mime size" data-validation-allowing="pdf, docx, doc, docm, rtf" data-validation-max-size="2M" />
</p>
<p align="center">Please vrify that you are human.</p>
<div class="g-recaptcha" data-sitekey="---sitekey----" style="margin-bottom:20px;" align="center"></div>
<input type="submit" value="Verzend mijn sollicitatie!" />
</form>
<script>
$("form#cv")
.attr("enctype", "multipart/form-data")
.attr("encoding", "multipart/form-data");
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.26/jquery.form-validator.min.js"></script>
<script>
$.validate({
modules: 'file',
lang: 'nl'
});
</script>
</div>
</div>
<div class="block">
<div class="circle" style="border-radius:600px; border:solid 10px #fff; margin-top:60px; background:url(wrapper.png); background-size:160% auto!important; background-position:-150px 0!important; box-sizing:border-box;"></div>
</div>
<div class="clear"></div>
<div class="block padding-left inline-block">
<div style="text-align:center; display:flex; align-items:center; justify-content:center; width:90%;">
<?php
if(isset($_POST['email'])) {
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<div class="notific" style="background:rgba(192, 0, 0, 0.8); display:flex; align-items:center; justify-content:center; border-radius:6px; margin-top:20px; padding:0px 15px 0px 15px;"><i style="margin:0px 10px 0px 10px; color:#fff; font-size:32px; text-shadow: 1.2px 1.2px 1.2px #000;" class="fas fa-exclamation-triangle"></i><h2 style="font-size:28px!important;">Bevestig dat u een mens bent.</h2></div>';
?>
<script>
$('html, body').animate({
scrollTop: $("div.notific").offset().top
}, 1000)
</script>
<?php
exit;
}
$secretKey = "--secretkey--";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
echo '<div class="notific" style="background:rgba(192, 0, 0, 0.8); display:flex; align-items:center; justify-content:center; border-radius:6px; margin-top:20px; padding:0px 15px 0px 15px;"><i style="margin:0px 10px 0px 10px; color:#fff; font-size:32px; text-shadow: 1.2px 1.2px 1.2px #000;" class="fas fa-exclamation-triangle"></i><h2 style="font-size:28px!important;">Er ging iets mis, probeer het nog eens.</h2></div>';
?>
<script>
$('html, body').animate({
scrollTop: $("div.notific").offset().top
}, 1000)
</script>
<?php
} else {
echo '<div class="notific" style="background:rgba(0, 226, 37, 0.8); display:flex; align-items:center; justify-content:center; border-radius:6px; margin-top:20px; padding:0px 15px 0px 15px;"><i style="margin:0px 10px 0px 10px; color:#fff; font-size:32px; text-shadow: 1.2px 1.2px 1.2px #000;" class="fas fa-check-square"></i><h2 style="font-size:28px!important;">Bedankt voor uw aanmelding.</h2></div>';
?>
<script>
$('html, body').animate({
scrollTop: $("div.notific").offset().top
}, 1000)
</script>
<?php
}
}
?>
</div>
</div>
</div>
</div>
<?php
if(isset($_POST['email']) && isset($_FILES['cv'])) {
$naam = $_POST['naam'];
$achternaam = $_POST['achternaam'];
$telefoon = $_POST['telefoon'];
$email = $_POST['email'];
$vacature = $_POST['vacature'];
$to = 'email#email.com';
$from = 'email#email.com';
$subject = 'Nieuwe sollicitatie op vacature';
$message = "<p><b>Details sollicitatie:</b></p>
<p>Naam: $naam </p>
<p>Achternaam: $achternaam </p>
<p>Telefoon: $telefoon </p>
<p>Email: $email </p>
<p>Vacature: $vacature</p>
</html>
";
$file_tmp_name = $_FILES['cv']['tmp_name'];
$file_name = $_FILES['cv']['name'];
$file_size = $_FILES['cv']['size'];
$file_type = $_FILES['cv']['type'];
$file_error = $_FILES['cv']['error'];
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("em");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from."\r\n";
$headers .= "Reply-To: ".$to."" . "\r\n";
$headers .= 'Cc: cc#email.com' . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
mail($to, $subject, $body, $headers, '-f email#email.com');
} else {}
if(isset($_POST['email']) && !isset($_FILES['cv'])) {
$naam = $_POST['naam'];
$achternaam = $_POST['achternaam'];
$telefoon = $_POST['telefoon'];
$email = $_POST['email'];
$vacature = $_POST['vacature'];
$to = 'email#email.com';
$from = 'email#email.com';
$subject = 'Nieuwe sollicitatie op vacature';
$message = "<p><b>Details sollicitatie:</b></p>
<p>Naam: $naam </p>
<p>Achternaam: $achternaam </p>
<p>Telefoon: $telefoon </p>
<p>Email: $email </p>
<p>Vacature: $vacature</p>
</html>
";
$file_tmp_name = $_FILES['cv']['tmp_name'];
$file_name = $_FILES['cv']['name'];
$file_size = $_FILES['cv']['size'];
$file_type = $_FILES['cv']['type'];
$file_error = $_FILES['cv']['error'];
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from."\r\n";
$headers .= "Reply-To: ".$to."" . "\r\n";
$headers .= 'Cc: cc#email.com' . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
mail($to, $subject, $body, $headers, '-f email#email.com ');
} else {}
?>
</body>
</html>
It has been my experience that this type of situation is not related to your code. You could check your phpmail.log but I suspect you will see all of your emails listed.
The key here is that the CC is always received. That means your emails are being sent but not delivered. This points to your DNS configuration. It may be your SPF record, or lack of a DKIM entry.
Since you are using PHP's mail() function, you are not sending via authenticated SMTP. I would second #CBroe's recommendation to use a mail library, but for a different reason. It will provide you the option of using authenticated SMTP which will have a much higher possibility of delivery.
I would recommend:
Check your SPF Record and make sure it includes your server's IP address. Preferably it will end with "-all" (avoid "+all").
Use a PHP mail library and use authenticated SMTP. This isn't a requirement, but it makes coding email faster, easier and less error prone. Authenticated SMTP never hurts your chances of delivery.
If possible, set up the DKIM record for your domain.
I am working on a contact form at the minute which will spit out some errors if certain fields are left blank, and if the form submits successfully the user will see a successful message.
At the moment I have an errors class and a success class - these are below:
.errors {
font-family: 'Open Sans', sans-serif;
font-size: 13px;
padding: 7px 5px;
color: #A94442;
font-weight: 400;
width: 100%;
border-radius: 2px;
border: 1px solid #EBCCD1;
background-color: #F2DEDE;
margin: 0 0 10px 0;
}
.success {
font-family: 'Open Sans', sans-serif;
font-size: 13px;
padding: 7px 5px;
color: #3C763D;
font-weight: 400;
width: 100%;
border-radius: 2px;
border: 1px solid #D6E9C6;
background-color: #DFF0D8;
margin: 0 0 10px 0;
}
As far as the php validation goes, the errors/success messages are working as expected, however the problem I have is that the background-color and borders of these classes are showing blank when the users initially goes to the contact page.
You can get to the page here Contact Page by viewing the page you will see the issue straight away.
Please could you give me some advice/suggestions on how to hide these until they are needed? I could remove the background-color and borders from the classes and just have the text, but ideally I would like to keep the styling as it is.
Here is the php to go with this and also the html form
$errors = array();
$success = "";
$name = "";
$email = "";
$website = "";
$budget = "";
$message = "";
if (isset($_POST['contact_submit'])) {
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['website'])) {
$website = $_POST['website'];
}
if (isset($_POST['budget'])) {
$budget = $_POST['budget'];
}
if (isset($_POST['message'])) {
$message = $_POST['message'];
}
if (empty($name)) {
$errors[] = "Please enter your name.";
}
if (empty($email)) {
$errors[] = "Please enter a valid email address.";
}
elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Please enter a valid email address.";
}
if (empty($message)) {
$errors[] = "Don't forget your message!";
}
if (count($errors) == 0) {
$headers = "From: email here...\r\n";
$headers .= "Content-type: text/html\r\n";
$myaddress = "email here...";
$subject = "Website Enquiry";
$emailmessage = "<b><u>Name:</u></b><br>$name<br><br><b><u>Email:<br></u></b>$email<br><br><b><u>Message:<br></u></b> $message";
if (!mail($myaddress, $subject, $emailmessage, $headers)) {
$errors[] = "A problem occurred sending your email.";
}
else {
$success = "Your message was sent successfully!";
$name = "";
$email = "";
$message = "";
}
}
}
<form method="POST" action="">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"errors\">" . implode("<br>", $errors) . "</div>"; ?>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"success\">" . $success . "</div>"; ?>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="name" placeholder="Your Name *" value="<?php echo htmlentities($name); ?>">
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="email" placeholder="Your Email *" value="<?php echo htmlentities($email); ?>">
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="website" placeholder="Website (optional)" value="<?php echo htmlentities($website); ?>">
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="budget" placeholder="Budget (optional)" value="<?php echo htmlentities($budget); ?>">
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<textarea type="text" name="message" placeholder="Your Message *"><?php echo htmlentities($message); ?></textarea>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<button type="submit" name="contact_submit" class="btn-one">Send</button>
</div>
</div>
</form>
Thanks in advance!!
You have to do some thing like this
<?php
if(!empty($errors))
{
?>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"errors\">" . implode("<br>", $errors) . "</div>"; ?>
</div>
</div>
<?php
}
else
{
}
if(!empty($success))
{
?>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"success\">" . $success . "</div>"; ?>
</div>
</div>
<?php
}
else
{
}
?>
It would be helpful if you could share your PHP code, but without that I would guess that you're doing something like:
<div class="errors">
<?php
foreach ($errors as $error) {
echo $error . '<br>';
}
?>
</div>
When viewing the HTML for your website, the <div class="errors"></div> HTML is already present.
You should wrap that <div> with an isset() statement, so the HTML is only output when there is something to be shown.
For example:
<?php if (isset($errors) && !empty($errors)): ?>
<div class="errors">
<?php
foreach ($errors as $error) {
echo $error . '<br>';
}
?>
</div>
<?php endif; ?>
Simplest way to do this :
// your php code goes here
<?php
$http_post = ('POST' == $_SERVER['REQUEST_METHOD']);
if($http_post)
{
if(empty($error))
$class = 'error';
else
$class = 'success';
}
?>
// your html code
<div class="<?php echo $class?>"></div>
I am new to PHP.I had created the table with the button "save Form".I am unable to save the table data to database.When I click on the "save form" button no action is performing.Please help me.
This is my Form.phtml
<div>
<b> Hello <?php echo $_POST["name"]; ?>!</b><br>
<b>Email :</b> <?php echo $_POST["email"]; ?>.<br>
<b>Gender :</b> <?php echo $_POST["gender"]; ?>.<br>
<b>Birthday :</b>
<?php
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$date = $day."-".$month."-".$year;
$myDate = date("d F Y", strtotime($date));
echo $myDate;
?>
</div>
<form action="sendmail.php" method="post" id="vaccination-form">
<div>
<table border="1" style="width:100%">
<tr>
<th id= "sno" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class="bg-color" width="5%">S.No</th>
<th id= "vaccine" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class="bg-color center" width="32%">Vaccine</th>
<th id="decsription" style="font-family: sans-serif; font-size: 100%; font-weight: bold;">Description</th>
<th id="duedate" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class ="bg-color" width="15%">Due Date</th>
</tr>
<tr>
<td>1</td>
<tr>
</table>
</form>
<div>
<button type="submit" name="submit" style="margin-top: 1cm;"title="<?php echo $this->__('Save Form') ?>" value="submit "class="button"><span><span><?php echo $this->__('Save Form')?></span> </span> </button>
</div>
sendmail.php
<?php
//due dates
$myDate=$_POST['myDate'];
$dueDate=$_POST['dueDate'];
$rodueDate=$_POST['rodueDate'];
$didueDate=$_POST['didueDate'];
$pdueDate=$_POST['pdueDate'];
$hadueDate=$_POST['hadueDate'];
$indueDate=$_POST['indueDate'];
$idueDate=$_POST['idueDate'];
$rdueDate=$_POST['rdueDate'];
$vdueDate=$_POST['vdueDate'];
$tdueDate=$_POST['tdueDate'];
$hdueDate=$_POST['hdueDate'];
$mdueDate=$_POST['mdueDate'];
$email=$_POST['email'];
$name=$_POST['name'];
$to=$email;
$subject= "Vaccination Schedule For ".$name;
$message=
'
</table>';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers.= "From: someone#example.com" . "\r\n" ;
if( mail($to, $subject, $message, $headers))
{
echo 'Your mail has been sent successfully';
}
else
{
echo 'Unable to send email. Please try again.';
}
?>
Show us your sendmail.php.
Mainly you need to get POST variables into sendmail.php and then create a insert sql statement with data you got from POST variables..
How to do stuff above is totaly up to you, there are many ways, but we cannot tell you since you didnt give us enough info about your system.
I do not think the right thing to do , but still think you need to put the submit button between tags
I've been following a WordPress contact form tutorial on how to make a custom WordPress contact form.
I've checked, re-checked, and re-checked again, but still I can't get this working. When I submit the form with ALL fields completed,I just get 'Message was not sent. Try Again.'
I can't work out if something has changed maybe since this was written in WordPress. My code stands like this: Just to be sure I didn't make a mistake, I've copied exactly from the tutorial.
<?php
/*
Template Name: Contact Page
*/
?>
<?php
//response generation function
$response = "";
//function to generate response
function my_contact_form_generate_response($type, $message){
global $response;
if($type == "success") $response = "<div class='success'>{$message}</div>";
else $response = "<div class='error'>{$message}</div>";
}
//response messages
$not_human = "Human verification incorrect.";
$missing_content = "Please supply all information.";
$email_invalid = "Email Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];
//php mailer variables
$to = get_option('admin_email');
$subject = "Someone sent a message from ".get_bloginfo('name');
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
if(!$human == 0){
if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($name) || empty($message)){
my_contact_form_generate_response("error", $missing_content);
}
else //ready to go!
{
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/html-header', 'parts/shared/header' ) ); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content group">
<?php the_content(); ?>
<style type="text/css">
.error{
padding: 5px 9px;
border: 1px solid red;
color: red;
border-radius: 3px;
}
.success{
padding: 5px 9px;
border: 1px solid green;
color: green;
border-radius: 3px;
}
form span{
color: red;
}
</style>
<div id="respond">
<?php echo $response; ?>
<form action="<?php the_permalink(); ?>" method="post">
<p><label for="name">Name: <span>*</span> <br><input type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"></label></p>
<p><label for="message_email">Email: <span>*</span> <br><input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label></p>
<p><label for="message_text">Message: <span>*</span> <br><textarea type="text" name="message_text"><?php echo esc_textarea($_POST['message_text']); ?></textarea></label></p>
<p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>
<input type="hidden" name="submitted" value="1">
<p><input type="submit"></p>
</form>
</div>
</div><!-- .entry-content -->
</article><!-- #post -->
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/footer','parts/shared/html-footer' ) ); ?>
After checking the logs, i found on the contact page i get:
Notice: Undefined index: message_name in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 33
Notice: Undefined index: message_email in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 34
Notice: Undefined index: message_text in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 35
Notice: Undefined index: message_human in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 36
Notice: Undefined index: submitted in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 66
Notice: get_current_theme is deprecated since version 3.4! Use wp_get_theme() instead. in C:\xampp\htdocs\wordpress\wp-includes\functions.php on line 2841