im trying to send email with attachment using ajax, if dont use attachment code email is send successfully, but when i add attachment code then nothing happens no error nothing at all.
`<form enctype="multipart/form-data" id="first_form" method="POST" class="get-contact-form" action="get-quote.php">
<div class="row g-3">
<div class="col-sm-6">
<div class="form-floating">
<input type="hidden" class="form-control" id="spam" name="spam" placeholder="Spam:" value="<?php echo $silk; ?>">
<p class='response' id="spam_error"></p>
</div>
</div>
<div class="col-sm-6">
<div class="form-floating">
<input type="hidden" class="form-control" id="blank" name="blank" placeholder="blank:" value="">
<p class='response' id="blank_error"></p>
</div>
</div>
<div class="col-sm-6">
<div class="form-floating">
<input type="text" name="name" class="form-control" id="gname" placeholder="Gurdian Name">
<label for="gname">Your Name</label>
<p class='response' id="name_error"></p>
</div>
</div>
<div class="col-sm-6">
<div class="form-floating">
<input type="email" name="email" class="form-control" id="gmail" placeholder="Gurdian Email">
<label for="gmail">Your Email</label>
<p class='response' id="email_error"></p>
</div>
</div>
<div class="col-sm-6">
<div class="form-floating">
<input type="text" name="phone" class="form-control" id="cname" placeholder="Child Name">
<label for="cname">Your Phone</label>
<p class='response' id="phone_error"></p>
</div>
</div>
<div class="col-sm-6">
<div class="form-floating">
<input type="text" name="address" class="form-control" placeholder="Child Age">
<label for="cage">Address</label>
<p class='response' id="address_error"></p>
</div>
</div>
<div class="col-sm-6">
<div class="form-floating">
<input type="text" name="subject" class="form-control" id="cage" placeholder="Child Age">
<label for="cage">Subject</label>
<p class='response' id="subject_error"></p>
</div>
</div>
<div class="col-sm-6">
<div class="form-floating">
<input type="text" name="services" class="form-control" id="cage" placeholder="Child Age">
<label for="cage">Service Type</label>
<p class='response' id="services_error"></p>
</div>
</div>
<div class="col-sm-12">
<div class="form-floating">
<input type="file" name="my_file" class="form-control" id="my_case_file" placeholder="Child Age">
<p class='response' id="my_file_error"></p>
</div>
</div>
<div class="col-12">
<div class="form-floating">
<textarea class="form-control" name="message" placeholder="Leave a message here" id="message" style="height: 80px"></textarea>
<label for="message">Message</label>
<p class='response' id="message_error"></p>
</div>
</div>
<div class="col-12">
<button class="btn secondary-bg text-white py-3 px-5" name="submit" type="submit">Get A Free Quote</button>
</div>
</div>
</form>`
php code for sending mail, when i add attachement code for sending image through mail following code doesnt send mail... but if remove attachment code form following code the form works percetly with ajax submission
session_start();
$floor = $_SESSION['token'];
$errorsbag = [];
$count_errors = 0;
$post = [];
$errors = [];
// List of the words you want to block
$spam_words =
["http", "www", ".com","#",".mx", ".org",
".net", ".co.uk",
".jp", ".ch", ".info", ".me",
".mobi", ".us", ".biz", ".ca",
".ws", ".ag",".com.co", ".net.co",
".com.ag", ".net.ag", ".it", ".fr",
".tv", ".am", ".asia", ".at", ".be",
".cc", ".de","dollars",
"payment", "website", "games", "toys", "poker",
"cheap", "href","nude","cam","penis","pills",
"sale","cheapest", "script",'Mod', 'Owner',
'Mawd', 'M0d', '0wner','090','080','081','070','
091','0-','+','80','81','70','91','dot','f*ck',
'bitch','ww','cum','hacker','money','Buy','Order',
'Meet singles','Near you','Near you','Additional income',
'Be your own boss','Robot, '<','>'
];
function is_blank($value) {
return !isset($value) || trim($value) === '';
}
// post method form processing
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$post = array_map('trim',$_POST);
// validate inputs
if($post['name'] === '')
{
$errors['name'] = "Name is required.";
}
if($post['email'] === '')
{
$errors['email'] = "Email is required.";
}
else if(!filter_var($post['email'], FILTER_VALIDATE_EMAIL))
{
$errors['email'] = "Email format is not valid.";
}
if($post['phone'] === '')
{
$errors['phone'] = "Phone is required.";
}
if($post['address'] === '')
{
$errors['address'] = "Address is required.";
}
if($post['subject'] === '')
{
$errors['subject'] = "Subject is required.";
}
if($post['services'] === '')
{
$errors['services'] = "Services is required.";
}
if($post['message'] === '')
{
$errors['message'] = "Message is required.";
}
$comment = $post['message']; // This should be coming from a post request
$comment_str = (string)$comment;
$new_input = str_replace(' ', '', strtolower($comment_str));
if(!is_blank($comment)){
foreach($spam_words as $item) {
if (strpos($new_input, strtolower($item)) !== false) {
$errorsbag[] = $item;
}
}
}
$count_errors = count($errorsbag);
if ($count_errors >= 1) {
$errors['message'] = "";
}
if($post['spam'] !== $floor){
$errors['spam'] = "";
}elseif($post['blank'] !== ''){
$errors['blank'] = "";
}elseif($count_errors >= 1){
$errors['message'] = "";
}
// if no errors, use the form data
if(empty($errors)){
$name = contact_input($post['name']);
$email = contact_input($post['email']);
$phone = contact_input($post['phone']);
$address = contact_input($post['address']);
$subject = contact_input($post['subject']);
$services = contact_input($post['services']);
$message = contact_input($post['message']);
$info =
'<table border="1" cellspacing="0" width="60%" cellpadding="10px">
<tr style="height: 21px;">
<td colspan="2" bgcolor="#dddfe2 " align="center" style="font-family: Verdana, Geneva, sans-serif; font-size: 14px; color: #000; height: 21px;"><b>Mail From Client: '.$name.'</b></td>
</tr>
<tr>
<td>Name:</td>
<td>'.$name.'</td>
</tr>
<tr>
<td>Email:</td>
<td>'.$email.'</td>
</tr>
<tr>
<td>Phone:</td>
<td>'.$phone.'</td>
</tr>
<tr>
<td>Services:</td>
<td>'.$services.'</td>
</tr>
<tr>
<td>Message:</td>
<td>'.$message.'</td>
</tr>
</table>';
//Get uploaded file data
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("random");
//header
$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
$headers .= "From:".$email."\r\n"; // Sender Email
$headers .= "Content-Type: multipart/mixed;"; // Defining Content-Type
$headers .= "boundary = $boundary\r\n"; //Defining the Boundary
//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($info));
//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;
$subject = "Mail From Client";
$mailTo = "satish.innovstudio#gmail.com";
mail($mailTo, $subject, $body,$headers);
}
$response = [];
if(empty($errors) || $post['spam'] !== $floor || $post['blank'] !== '' || $count_errors >= 1){
$response['success_message'] = "Thank you for your message!<br>We'll get back to you as soon as We can.";
}
else{
foreach($errors as $key=>$value)
{
$response[$key.'_error'] = $value;
}
}
echo json_encode($response);
}
function contact_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}`
I have a form that I want to add ReCaptcha to, however, somewhere along the line something isnt working correctly because my form will still submit whether or not reCaptcha is verified.
This is the form:
<?php if(isset($_GET[ 'CaptchaPass'])){ ?>
<div>Thank you! Your Form was Successfully Submitted</div>
<?php } ?>
<?php if(isset($_GET[ 'CaptchaFail'])){ ?>
<div>Captcha Error. Please verify that you are human!</div>
<?php } ?>
<form action="http://vmobileautoglass.com/php/func_contact.php">
<label>Name</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="name">
</div>
</div>
<label>Email
<span class="color-red">*</span>
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="email">
</div>
</div>
<label>Phone
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="phone">
</div>
</div>
<label>Message</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-8 col-md-offset-0">
<textarea rows="8" class="form-control" name="message"></textarea>
</div>
</div>
<div class="g-recaptcha" data-sitekey="MYSITEKEYFROMGOOGLE"></div>
<p>
<button type="submit" class="btn btn-primary" name="ContactButton">Send Message</button>
</p>
</form>
This goes at the very top of the page where the form is located:
<?php
if (isset($_POST['ContactButoon'])) {
$url = 'https://google.com/recaptcha/api/siteverify';
$privatekey = "MYPRIVATEKEYFROMGOOGLE";
$response = file_get_contents($url . "?secret=" . $privatekey . "&response=" . $_POST['g-recaptcha-response'] . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
$date = json_decode($response);
if (isset($data->success) AND $data->success == true) {
header('Location: contact.php?CaptchaPasss=True');
} else {
header('Location: contact.php?CaptchaFail=True');
}
}
?>
And this is the forms functionality:
// Receiving variables
#$pfw_ip = $_SERVER['REMOTE_ADDR'];
#$name = addslashes($_GET['name']);
#$email = addslashes($_GET['email']);
#$phone = addslashes($_GET['phone']);
#$message = addslashes($_GET['message']);
// Validation
if (strlen($name) == 0) {
header("Location: http://vmobileautoglass.com/php/err_name.php");
exit;
}
if (strlen($email) == 0) {
header("Location: http://vmobileautoglass.com/php/err_email.php");
exit;
}
if (strlen($message) == 0) {
header("Location: http://vmobileautoglass.com/php/err_message.php");
exit;
}
//Sending Email to form owner
$pfw_header = "From: $email\n"
. "Reply-To: $email\n";
$pfw_subject = "vMobile Contact Form";
$pfw_email_to = "vmobileag#gmail.com";
$pfw_message = "Visitor's IP: $pfw_ip\n"
. "name: $name\n"
. "email: $email\n"
. "phone: $phone\n"
. "message: $message\n";
#mail($pfw_email_to, $pfw_subject, $pfw_message, $pfw_header);
header("Location: http://vmobileautoglass.com/php/successform.php");
You have a typo:
$date = json_decode($response);
if(isset($data->success) AND $data->success==true){
$date should be $data.
Thank you in advance for your assistance.
I am struggling to get all the information within this form to print properly to email. At present I am receiving data from the 'email', 'checkin' and 'checkout' input fields. However 'name' , 'guests' and 'message' do not appear.
This is the HTML code for the form:
<form class="reservation-vertical clearfix" role="form" method="post" action="php/reservation.php" name="reservationform" id="reservationform">
<div id="message"></div>
<!-- Error message display -->
<div class="form-group">
<label for="name"><span class="required">*</span> Your Name</label>
<input name="name" type="text" class="form-control" value="" placeholder="Full Name"/>
</div>
<div class="form-group">
<label for="email">E-mail</label>
<input name="email" type="text" value="" class="form-control" placeholder="Please enter your E-mail"/>
</div>
<div class="form-group">
<label for="checkin">Check-in</label>
<div class="popover-icon" data-container="body" data-toggle="popover" data-trigger="hover" data-placement="right" data-content="Check-in is from 2:00pm"> <i class="fa fa-info-circle fa-lg"> </i> </div>
<i class="fa fa-calendar infield"></i>
<input name="checkin" type="text" value="" class="form-control" placeholder="Check-in"/>
</div>
<div class="form-group">
<label for="checkout">Check-out</label>
<div class="popover-icon" data-container="body" data-toggle="popover" data-trigger="hover" data-placement="right" data-content="Check-out is by 11:00am"> <i class="fa fa-info-circle fa-lg"> </i> </div>
<i class="fa fa-calendar infield"></i>
<input name="checkout" type="text" value="" class="form-control" placeholder="Check-out"/>
</div>
<div class="form-group">
<label for="guests">Guests</label>
<i class="fa fa-user infield"></i>
<input name="guests" type="text" value="" class="form-control" placeholder="Number of guests"/>
</div>
<div class="form-group">
<label for="extra"> Your message</label>
<textarea name="extra" rows="9" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary btn-block">Enquire Now</button>
</div>
</div>
</div>
</form>
Here is the PHP code:
<?php
if(!$_POST) exit;
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$guests = $_POST['guests'];
$extra = $_POST['extra'];
if(get_magic_quotes_gpc()) {
$extra = stripslashes($extra);
$email = stripslashes($email);
$name = stripslashes($name);
$guests = stripslashes($guests);
}
$address = "myemail#gmail.com";
$e_subject = "Hotel booking enquiry submitted by $fullname" . PHP_EOL;
$e_body = "
A hotel booking enquiry has been made by: $name
Their email is: $email
The customer wants to check-in at: $checkin
and check-out at: $checkout
The customer requested accommodation for: $guests guest(s).
They also included this message: $extra" . PHP_EOL;
$e_reply = "You can contact the customer via email, $email";
$msg = wordwrap( $e_body . $e_reply, 70 );
$headers = "From: myemail#gmail.com\r\n" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id=success_page style=text-align:center>";
echo "<h4>Booking enquiry sent successfully!</h4>";
echo "<p><br>Thank you, your enquiry has been received. We will contact you shortly to complete your booking.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo "ERROR!";
}
This line
$e_subject = "Hotel booking enquiry submitted by $fullname" . PHP_EOL;
Should be
$e_subject = "Hotel booking enquiry submitted by $name" . PHP_EOL;
I am trying to get a mail script to work (well, it's working except for one part). I added a dropdown menu, but for some reason it's value is empty when reading the mail that was sent.
The dropdown menu that I got:
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Type werkzaamheden</label>
<select type="select" id="select" name="select">
<option value="kameronderhoud">kameronderhoud</option>
<option value="glas-bewassing">glas-bewassing</option>
<option value="specialistisch reinigen">specialistisch reinigen</option>
<option value="vloer-onderhoud">vloer-onderhoud</option>
<option value="woningontruiming">woningontruiming</option>
<option value="gevel-onderhoud">gevel-onderhoud</option>
<option value="overige">overige</option>
</select>
</div>
</div>
</div>
These are my POSTS:
$tel = $_POST['phone'];
$name = $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$select = $_POST['select'];
$comments = $_POST['comments'];
$phone = $_POST['phone'];
All of them work except select.
Edit: here is the full form
$e_body = "Er is contact opgenomen door $name." . PHP_EOL . PHP_EOL;
$e_content = "<br><br>
Gegevens:<br>
Naam: $name <br>
E-mail: $email <br>
Tel: $phone <br>
Type: $select <br>
$commentsb<br> " . PHP_EOL . PHP_EOL;
$e_reply = " $email2";
Everything displays correctly except the select part. Anybody know what could be causing that?
If it's needed, here is a working part of the form (HTML):
<form method="post" id="contactform" name="contactform" class="contact-form" action="mail/contact.php" type="contact">
<div class="col-md-12">
<h2 class="short">Vraag een <strong>Offerte</strong> aan</h2>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Uw naam*</label>
<input type="text" id="name" name="name" class="form-control input-lg" placeholder="">
</div>
<div class="col-md-6">
<label>Uw mail*</label>
<input type="email" id="email" name="email" class="form-control input-lg" placeholder="">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Type werkzaamheden</label>
<select type="select" id="select" name="select">
<option value="kameronderhoud">kameronderhoud</option>
<option value="glas-bewassing">glas-bewassing</option>
<option value="specialistisch reinigen">specialistisch reinigen</option>
<option value="vloer-onderhoud">vloer-onderhoud</option>
<option value="woningontruiming">woningontruiming</option>
<option value="gevel-onderhoud">gevel-onderhoud</option>
<option value="overige">overige</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Uw telefoonnummer</label>
<input type="text" id="phone" name="phone" class="form-control input-lg" placeholder="">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Onderwerp</label>
<input type="text" id="email2" name="email2" class="form-control input-lg" placeholder="">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Bericht</label>
<textarea cols="6" rows="7" id="comments" name="comments" class="form-control input-lg" placeholder=""></textarea>
</div>
</div>
</div>
<div class="col-md-12">
<input id="submit" name="submit" type="submit" class="btn btn-primary btn-lg pull-right" value="Verstuur">
</div>
</div>
</form>
PHP script:
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$tel = $_POST['phone'];
$name = $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$select = $_POST['select'];
$comments = $_POST['comments'];
$phone = $_POST['phone'];
if ($_POST['form_type'] == 'contact'){
}else{
if(trim($name) == '') {
echo '<div style="color:red;border:solid 1px red;" class="alert alert-error">Vul uw naam in.</div>';
exit();
} else if(trim($email) == '') {
echo '<div style="color:red;border:solid 1px red;" class="alert alert-error">Vul uw email adres in.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div style="color:red;border:solid 1px red;" class="alert alert-error">Vul een geldige emailadres in.</div>';
exit();
} else if(trim($phone) == '') {
echo '<div style="color:red;border:solid 1px red;" class="alert alert-error">Vul uw telefoonnummer in.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe#yourdomain.com";
//$address = "example#themeforest.net";
$address = "*email*";
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
if ($_POST['form_type'] == 'contact'){
$e_subject = 'Terugbelafspraak';
}else{
$e_subject = 'Contact Form';
}
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
if ($_POST['form_type'] == 'contact'){
$e_body = "Er is een terugbelverzoek gemaakt." . PHP_EOL . PHP_EOL;
$e_content = "" . PHP_EOL . PHP_EOL;
$e_reply = "Nummer: $tel";
}else{
if($comments == ''){
$commentsb = '';
}else{
$commentsb = 'Bericht:'.$comments.'';
}
$e_body = "Er is contact opgenomen door $name." . PHP_EOL . PHP_EOL;
$e_content = "<br><br>
Gegevens:<br>
Naam: $name <br>
E-mail: $email <br>
Tel: $phone <br>
Type: $select <br>
$commentsb<br> " . PHP_EOL . PHP_EOL;
$e_reply = " $email2";
}
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: *email*" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
if ($_POST['form_type'] == 'contact'){
echo "<div class='alert alert-success'>";
echo "<h3 style='text-transform:none;'>Aanvraag is verzonden.</h3><br>";
echo "<p>Er zal zo spoedig mogelijk contact opgenomen worden door ons.</p>";
echo "</div>";
}
if ($_POST['form_type'] != 'contact'){
echo "<div class='alert alert-success'>";
echo "<h3 style='text-transform:none;'>Email is verzonden.</h3><br>";
echo "<p>Bedankt <strong>$name</strong>, uw mail is ontvangen door sfsdf.</p>";
echo "</div>";
}
} else {
echo 'ERROR!';
}
This question gets asked all the time, but I can't figure out why mine isn't working. I have a form that redirects to itself. If PHP decides it is submitted, there is a success/failure message and it displays the user input as the default value and disables the fields: using phpinfo I can see that the form is being submitted, but this first conditional doesn't work. I've tried a couple of versions, but no luck. It's weird because it sends the email
Specifically, the result and disable functions don't display their code after the form has been sent.
<?php
function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}
function result(){
if($sent) echo $result;
}
function disable($field){
if($sent){
if($field != null){
$ret .= $field . '", disabled, placeholder!="';
}
$ret .= '", disabled, placeholder!="';
echo $ret;
}
}
function option($item){
$ret = "<option>";
if($sent){
if($eventType == $item){
$ret = "<option selected>";
}
}
$ret .= $item . "</option>";
echo $ret;
}
if(isset($_POST['name'])){
$sent = TRUE;
$result = null;
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$eventDate = $_POST['eventDate'];
$eventTime = $_POST['eventTime'];
$eventLength = $_POST['eventLength'];
$eventLocation = $_POST['eventLocation'];
$eventType = $_POST['eventType'];
$message = $_POST['message'];
$recipient = "";
$subject = " Form Submission";
$mailheader = "From: \r\n";
$formcontents = "You received this e-mail message through your website: \n\n";
$formcontents .= "Name: " . clean($name) . "\r\n";
$formcontents .= "Phone: " . clean($phone) . "\r\n";
$formcontents .= "Email: " . clean($email) . "\r\n";
$formcontents .= "Event Date: " . clean($eventDate) . "\r\n";
$formcontents .= "Event Time: " . clean($eventTime) . "\r\n";
$formcontents .= "Event Length: " . clean($eventLength) . "\r\n";
$formcontents .= "Event Location: " . clean($eventLocation) . "\r\n";
$formcontents .= "Event Type: " . clean($eventType) . "\r\n";
$formcontents .= "Message: " . clean($message) . "\r\n";
$formcontents .= "\r\n";
$formcontents .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n";
$formcontents .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n";
// Send mail
if(mail($recipient, $subject, $formcontents, $mailheader)){;
$result = '<h3 class="alert alert-success"> Thank you, your form was successfully sent and I will contact you shortly.</h3>';
} else {
$result = '<h3 class="alert alert-error"> Your mail could not be sent at this time.</h3>';
}
}
?>
<form action="contact.php" method="POST" class="form-horizontal span4">
<fieldset>
<legend>
<h2>Or send me a message. </h2>
</legend>
<p class="help-block">None of the fields are required, but the more information I have about your event, the more detailed I can be in my response.</p>
<legend class="help-block">Your Details</legend>
<div class="control-group">
<label for="name" class="control-label">Your Name</label>
<div class="controls">
<input id="name" type="text" name="name" placeholder="<?php disable($name); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="phone" class="control-label">Your Contact Number</label>
<div class="controls">
<input id="phone" type="tel" name="phone" placeholder="<?php disable($phone); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="email" class="control-label">Your Email</label>
<div class="controls">
<input id="email" type="email" name="email" placeholder="<?php disable($email); ?>" class="input-xlarge"/>
</div>
</div>
<legend class="help-block">Your Event </legend>
<div class="control-group">
<label for="eventDate" class="control-label">Your Event's Date</label>
<div class="controls">
<input id="eventDate" type="date" name="eventDate" placeholder="<?php disable($eventDate); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventTime" class="control-label">Your Event's Start Time</label>
<div class="controls">
<input id="eventTime" type="time" name="eventTime" placeholder="<?php disable($eventTime); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventLength" class="control-label">Your Event's Length</label>
<div class="controls">
<input id="eventLength" type="text" name="eventLength" placeholder="<?php disable($eventLength); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventLocation" class="control-label">Your Event's Location</label>
<div class="controls">
<input id="eventLocation" type="text" name="eventLocation" placeholder="<?php disable($eventLocation); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventType" class="control-label">What Kind of Event</label>
<div class="controls">
<select id="eventType" name="eventType" placeholder="<?php disable($eventType); ?>"><?php option("Charity Event"); option("Expo/Trade Show"); option("Personal Event"); option("Other"); ?></select>
</div>
</div>
<div class="control-group">
<label for="message" class="control-label">Other comments or the best time to reach you.</label>
<div class="controls">
<textarea id="message" name="message" rows="10" placeholder="<?php disable($message); ?>" class="input-xxlarge"></textarea>
</div>
</div>
<div class="form-actions">
<button type="submit" name="submit" placeholder="<?php disable(null); ?>" class="btn btn-primary">Send Message</button>
</div>
</fieldset>
</form>
You have to import your global variables into function scope, like:
function result(){
global $sent, $result;
if($sent) echo $result;
}
..in functions disable() and option(), too.
if(isset($_POST['name'])){
should be
if(isset($_POST['submit'])){