Passing Strings (email address) from form to PHP - php

I have a php contact form and all works great going to a single address but I'm trying to modify my script to handle a drop down selector, which enables choosing a recipient (which email address to send to).
Here is the part of the code that I have so far in trying to deal with this issue:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="recipient" id="recipient">
<p class="text">
Please select recipient<br>
<select name="recipient" size="4"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['recipient'], ENT_QUOTES).'"';
} ?>
>
<option value="">Select...</option>
<option value="1">Artistic Director</option>
<option value="2">Site Administrator</option>
<option value="3">Someone else</option>
</select>
</p>
</form>
<?php if (array_key_exists('send', $_POST)) {
// mail processing script
if ('recipient' == 1) {
$to = 'soandso#mail.com';
}
elseif('recipient' == 2) {
$to = 'soandso#mail.com';
}
elseif('recipient' == 3) {
$to = 'soandso#mail.com';
}
else {
echo 'Sorry for no recipient';
}
//then remainder code to process the rest which works fine
I'm sure my problem lies in the calling/getting the value of recipient but I can't figure out where to go from here.

You're trying to do something weird here. It should be:
if ($_POST['recipient'] == 1) {
$to = 'soandso#mail.com';
}
else if($_POST['recipient'] == 2) {
$to = 'soandso#mail.com';
}
else if($_POST['recipient'] == 3) {
$to = 'soandso#mail.com';
}
else {
echo 'Sorry for no recipient';
}
Of course 'recipient' will never be equal to 1, 2 or 3.
I also noticed the form and the select has the same name 'recipient'. I don't know is that is an issue though. But I would like to address it anyway.

This code is working 100% :
(function($) {
$('#recipient').on('click', function() {
$('#recipient-form').submit();
});
})(jQuery);
<div id="page">
<?php
$to = '';
if (isset($_POST['recipient'])) :
// mail processing script
if ($_POST['recipient'] == 1) {
$to = 'recipient1';
}
else if($_POST['recipient'] == 2) {
$to = 'reciipient2';
}
else if($_POST['recipient'] == 3) {
$to = 'recipient3';
}
else {
$to = 'Sorry for no recipient';
}
echo $to;
else : ?>
<form action="" method="post" id="recipient-form">
<select id="recipient" name="recipient" size="4">
<option value="">Select...</option>
<option value="1">Artistic Director</option>
<option value="2">Site Administrator</option>
<option value="3">Someone else</option>
</select>
</form>
</div>
<?php endif; ?>

The page in it's (mostly) entirety for clarification purposes hopefully:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="recipient-form">
<p class="text">
Please select recipient<br>
<select id="recipient" name="recipient" size="4">
<option value="">Select...</option>
<option value="1">Artistic Director</option>
<option value="2">Site Administrator</option>
<option value="3">Someone else</option>
</select>
</p>
</form>
<?php if (array_key_exists('send', $_POST)) {
// mail processing script
$to = '';
if ($_POST['recipient']) {
// mail processing script
if ($_POST['recipient'] == 1) {
$to = '';
}
else if($_POST['recipient'] == 2) {
$to = '';
}
else if($_POST['recipient'] == 3) {
$to = '';
}
else {
$to = 'Sorry for no recipient';
}
}
$subject = 'Feedback From Website';
// list expected fields
$expected = array('name', 'email', 'comments', 'subscribe');
// set required fields
$required = array('name', 'email', 'comments');
// set additional headers
$headers = 'From: ';
// set the include
$process = 'includes/process.inc.php';
if (file_exists($process) && is_readable($process)) {
include($process);
}
else {
$mailSent = false;
mail($me, 'Server Problem', "$process cannot be read", $headers);
}
}
?>
<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
<p class="warning">Please complete the missing item(s) indicated.</p>
<?php
}
elseif ($_POST && $link) {
?>
<p class="warning">Sorry, Messages sent that contain links will not be sent.</p>
<?php
}
elseif ($_POST && !$mailSent) {
?>
<p class="warning">Sorry, there was a problem sending your message. Please try again later.</p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p class="success">Your message has been sent. Thank you for your message!</p>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contact" id="contact" onSubmit="MM_validateForm('name','','R','email','','RisEmail','comments','','R');return document.MM_returnValue">
<p>
<label for="name">Name: <?php
if (isset($missing) && in_array('name', $missing)) { ?>
<span class="warning">Please enter your name</span><?php } ?>
</label>
<input name="name" type="text" class="textInput" id="name"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['name'], ENT_QUOTES).'"';
} ?>
>
</p>
<p>
<label for="email">Email: <?php
if (isset($missing) && in_array('email', $missing)) { ?>
<span class="warning">Please enter your email address</span><?php } ?>
</label>
<input name="email" type="text" class="textInput" id="email"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['email'], ENT_QUOTES).'"';
} ?>
>
</p>
<p>
<label for="comments">Message:<?php
if (isset($missing) && in_array('comments', $missing)) { ?>
<span class="warning">Please enter your message</span><?php } ?>
</label>
<textarea name="comments" id="comments" cols="45" rows="5"><?php
if (isset($missing)) {
echo htmlentities($_POST['comments'], ENT_QUOTES);
} ?></textarea>
</p>
<p>
<p class="text">
Please check the box if you would like to sign up for our Mailing List!
<input type="checkbox" name="subscribe" value="Yes"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['subscribe'], ENT_QUOTES).'"';
} ?>
>
</p>
<p>
<?php
require_once('recaptchalib.php');
$publickey = "6Lf3NdQSAAAAAOAwgPGRybLnY175X6k9PJ1F2vHx"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
</p>
<p class="last">
<input type="submit" name="send" id="send" value="Send Message">
</p>
</form>
Hopefully having all of it now will help someone come up with the best solution!

The entirety again. I think/hope we're getting closer:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="getEmail">
<p class="text">Please select recipient</p><br>
<select name="recipient" size="4">
<option value="">Select...</option>
<option value="1">Artistic Director</option>
<option value="2">Site Administrator</option>
<option value="3">Someone else</option>
</select>
<input type='hidden' name='do' value='1'>
</form>
<?php
if (array_key_exists('send', $_POST)) {
if (isset($_POST['do'])) {
// mail processing script
if ($_POST['recipient'] == 1) { $to = ''; }
else if($_POST['recipient'] == 2) { $to = ''; }
else if($_POST['recipient'] == 3) { $to = ''; }
else echo 'Sorry for no recipient';
}
echo $to;
$subject = 'Feedback From Website';
// list expected fields
$expected = array('name', 'email', 'comments', 'subscribe');
// set required fields
$required = array('name', 'email', 'comments');
// set additional headers
$headers = 'From:';
// set the include
$process = 'includes/process.inc.php';
if (file_exists($process) && is_readable($process)) {
include($process);
}
else {
$mailSent = false;
mail($me, 'Server Problem', "$process cannot be read", $headers);
}
}
?>
<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
<p class="warning">Please complete the missing item(s) indicated.</p>
<?php
}
elseif ($_POST && $link) {
?>
<p class="warning">Sorry, Messages sent that contain links will not be sent.</p>
<?php
}
elseif ($_POST && !$mailSent) {
?>
<p class="warning">Sorry, there was a problem sending your message. Please try again later.</p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p class="success">Your message has been sent. Thank you for your message!</p>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contact" id="contact" onSubmit="MM_validateForm('name','','R','email','','RisEmail','comments','','R');return document.MM_returnValue">
<p>
<label for="name">Name: <?php
if (isset($missing) && in_array('name', $missing)) { ?>
<span class="warning">Please enter your name</span><?php } ?>
</label>
<input name="name" type="text" class="textInput" id="name"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['name'], ENT_QUOTES).'"';
} ?>
>
</p>
<p>
<label for="email">Email: <?php
if (isset($missing) && in_array('email', $missing)) { ?>
<span class="warning">Please enter your email address</span><?php } ?>
</label>
<input name="email" type="text" class="textInput" id="email"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['email'], ENT_QUOTES).'"';
} ?>
>
</p>
<p>
<label for="comments">Message:<?php
if (isset($missing) && in_array('comments', $missing)) { ?>
<span class="warning">Please enter your message</span><?php } ?>
</label>
<textarea name="comments" id="comments" cols="45" rows="5"><?php
if (isset($missing)) {
echo htmlentities($_POST['comments'], ENT_QUOTES);
} ?></textarea>
</p>
<p>
<p class="text">
Please check the box if you would like to sign up for our Mailing List!
<input type="checkbox" name="subscribe" value="Yes"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['subscribe'], ENT_QUOTES).'"';
} ?>
>
</p>
<p>
<?php
require_once('recaptchalib.php');
$publickey = "6Lf3NdQSAAAAAOAwgPGRybLnY175X6k9PJ1F2vHx"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
</p>
<p class="last">
<input type="submit" name="send" id="send" value="Send Message">
</p>
</form>

Your form name and select name is the same. also you are echoing the post value inside the select. i think it is left over from your previous input box.
try this;
<form action="" method="post" id="getEmail">
<p class="text">Please select recipient<br></p>
<select name="recipient" size="4">
<option value="">Select...</option>
<option value="1">Artistic Director</option>
<option value="2">Site Administrator</option>
<option value="3">Someone else</option>
</select>
<input type='hidden' name='do' value='1'>
<input type='sumbit' value='Go'>
</form>
<?php if (isset($_POST['do'])) {
// mail processing script
if ($_POST['recipient'] == 1)$to = 'email1';
else if($_POST['recipient'] == 2)$to = 'email2';
else if($_POST['recipient'] == 3)$to = 'email3';
else echo 'Sorry for no recipient';
}
//echo $to;
//to send mail
$sub = 'Mail from web Form';
$msg = 'My message';
$mail_status= mail($to, $sub, $msg);
if($mail_status){do something on success}; else {do something on failure};
?>

Related

Protect form from spam (PHP) with empty fields (honeypot)

I have a simple contact form in a Wordpress website, that needs some protecting.
I gave it two empty fields named "website" and "email" and hid them with CSS (visibility: hidden;). So far, so good.
The problem now is, I just cannot give the PHP commands
if(isset($_POST['website'])) die();
if(isset($_POST['email'])) die();
the proper position in my PHP file. Can you tell me where to position it correctly?
Here is my PHP file:
<?php
if(isset($_POST['website'])) die();
if(isset($_POST['email'])) die();
if(isset($_POST['submitted'])) {
if(trim($_POST['contactVorname']) === '') {
$vornameError = '*';
$hasError = true;
} else {
$vorname = trim($_POST['contactVorname']);
}
if(trim($_POST['contactName']) === '') {
$nameError = '*';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
if(trim($_POST['contactEmail']) === '') {
$emailError = '*';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['contactEmail']))) {
$emailError = '*';
$hasError = true;
} else {
$email = trim($_POST['contactEmail']);
}
if(trim($_POST['unternehmen']) === '') {
/* $unternehmenError = '*';
$hasError = true; */
} else {
$unternehmen = trim($_POST['unternehmen']);
}
if(trim($_POST['ort']) === '') {
/* $ortError = '*';
$hasError = true; */
} else {
$ort = trim($_POST['ort']);
}
if(trim($_POST['telefon']) === '') {
/* $telefonError = '*';
$hasError = true; */
} else {
$telefon = trim($_POST['telefon']);
}
if(trim($_POST['betreff']) === '') {
$betreffError = '*';
$hasError = true;
} else {
$betreff = trim($_POST['betreff']);
}
if(trim($_POST['comments']) === '') {
$commentError = '*';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
if(!isset($hasError)) {
$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = get_option('admin_email');
}
$subject = 'Kontaktformular | '.$vorname.' '.$name;
$body = "\n.: Kontaktformular-E-Mail :. \n\nName: $vorname $name \nE-Mail: $email \n\nUnternehmen: $unternehmen \nOrt: $ort \nTelefon: $telefon \n\nBetreff: $betreff \n\nNachricht: $comments";
$headers = 'From: '.$vorname.' '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="post" id="post-<?php the_ID(); ?>">
<h2 class="gross"><?php the_title(); ?></h2>
<div id="inhalt">
<div class="seitebeitrag">
<?php if(isset($emailSent) && $emailSent == true) { ?>
<div><p>Vielen Dank für die Nachricht. Wir melden uns so schnell wie möglich zurück.</p></div>
<?php } else { ?>
<?php the_content(); ?>
<form action="" id="contactForm" method="post">
<div id="kf0"> </div>
<div id="kf1">
<p><label for="contactVorname">Vorname *</label><br />
<input type="text" name="contactVorname" id="contactVorname" value="<?php if(isset($_POST['contactVorname'])) echo $_POST['contactVorname'];?>" maxlength="50" />
<?php if(!empty($vornameError)) { ?>
<span class="fehler"><?=$vornameError;?></span>
<?php } ?></p>
<p><label for="contactName">Nachname *</label><br />
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" maxlength="50" />
<?php if(!empty($nameError)) { ?>
<span class="fehler"><?=$nameError;?></span>
<?php } ?></p>
<p><label for="contactEmail">E-Mail *</label><br />
<input type="text" name="contactEmail" id="contactEmail" value="<?php if(isset($_POST['contactEmail'])) echo $_POST['contactEmail'];?>" maxlength="50" />
<?php if(!empty($emailError)) { ?>
<span class="fehler"><?=$emailError;?></span>
<?php } ?></p>
<p><label for="unternehmen">Unternehmen</label><br />
<input type="text" name="unternehmen" id="unternehmen" value="" maxlength="50" /></p>
<p><label for="ort">Ort</label><br />
<input type="text" name="ort" id="ort" value="" maxlength="50" /></p>
<p><label for="telefon">Telefon</label><br />
<input type="text" name="telefon" id="telefon" value="" maxlength="50" /></p>
<input type="text" id="website" name="website" value="" maxlength="80" /><br />
<input type="text" id="email" name="email" value="" maxlength="80" />
</div>
<div id="kf2">
<p><label for="betreff">Betreff *</label><br />
<input type="text" name="betreff" id="betreff" value="<?php if(isset($_POST['betreff'])) echo $_POST['betreff'];?>" maxlength="50" />
<?php if(!empty($betreffError)) { ?>
<span class="fehler"><?=$betreffError;?></span>
<?php } ?></p>
<p><label for="commentsText">Nachricht *</label><br />
<textarea name="comments" id="commentsText" rows="20" cols="30"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
<?php if(!empty($commentError)) { ?>
<span class="fehler"><?=$commentError;?></span>
<?php } ?></p>
<p>* Pflichtfelder</p>
</div>
<div id="kf3">
<input type="submit" value="SENDEN" alt="senden" class="btn" /><br /><input type="hidden" name="submitted" id="submitted" value="true" />
</div>
<div id="kf4">
<?php if(isset($hasError) || isset($captchaError)) { ?>
<div><p class="error fehler">* ungültige oder fehlende Daten</p></div>
<?php } ?></div>
</form>
<?php } ?>
<?php wp_link_pages(array('before' => __('Pages: '), 'next_or_number' => 'number')); ?>
</div>
<?php // edit_post_link(__('Edit this entry.'), '<p>', '</p>'); ?>
</article>
<?php // comments_template(); ?>
<?php endwhile; endif; ?>
<?php // get_sidebar(); ?>
<?php get_footer(); ?>
Right now, the form gets totally blocked out, after sending the data, ALTHOUGH the two fields in question are NOT FILLED IN.
$_POST['website'] & $_POST['email'] will always be 'set'. An empty form field still sets the corresponding $_POST entry to an empty string ('') and will always be true to isset. Try using !empty.
if (!empty($_POST['website'])) die();
if (!empty($_POST['email'])) die();
See more here: http://php.net/manual/en/function.empty.php and with a bit more detail here: https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
Be careful using this approach with commonly named fields. They may be automatically filled in by a browser's auto-fill feature meaning you'll be getting false-positives and real users will end up on a blank screen.

PHP Simple Form Validation

I am trying to create a simple form validation and the form wont submit until all of the fields were set. I have two files here.
-form.php
-process.php
For some reason the $error won't appear and the radio button wont submit. Is there anything wrong?
here's the form.php:
<?php
if(isset($_GET['error']) && $_GET['error']!=""){
echo $_GET['error'];
}
?>
<body>
<form action="process.php" method="POST">
<p>
<label for="name">Your Name:</label>
<input type="text" name="name" id="name" value="">
</p>
<p>
<label for="location">Dojo Location:</label>
<select name="location">
<option value="Mountain View">Mountain View</option>
<option value="San Francisco">San Francisco</option>
<option value="South Korea">South Korea</option>
<option value="Philippines">Philippines</option>
</select>
</p>
<p>
<label for="language">Favorite Language:</label>
<select name="language">
<option value="JavaScript">JavaScript</option>
<option value="PHP">PHP</option>
<option value="Ruby">Ruby</option>
<option value="Python">Python</option>
</select>
</p>
<p>
<label for="comment">Comment: (Optional)</label><br/>
<textarea rows="10" cols="50" name="comment"></textarea>
</p>
<p>
<label for="comment">Can we store cookies in your computer?</label>
<input type="radio" name="cookies" value="yes">Yes
<input type="radio" name="cookies" value="no">No
</p>
<input type="submit" value="Submit">
</form>
here's the process.php:
<?php
if (isset($_POST["submit"])) {
if (empty($_POST["name"])) {
$Error = "Missing Name";
}
if (empty($_POST["location"])) {
$Error = "Missing Location";
}
if (empty($_POST["language"])) {
$Error = "Missing language";
}
if (empty($_POST["cookies"])) {
$Error = "Select cookies";
}
}else{
$name = $_POST['name'];
$location = $_POST['location'];
$language = $_POST['language'];
$comment = $_POST['comment'];
$cookies = $_POST['cookies'];
}
if($Error!=""){
header("Location:form.php?error=".$Error);
}
?>
<h2>Submitted Information:</h2>
<p><?php echo "NAME: {$name}"; ?> </p>
<p><?php echo "DOJO LOCATION: {$location}"; ?></p>
<p><?php echo "FAVORITE LANGUAGE: {$language}:"; ?> </p>
<p><?php echo "COMMENT: {$comment}"; ?></p>
<p><?php echo "COOKIES: {$cookies}"; ?></p>
Any idea?
Try something like this in your process.php
if($Error!=""){
header("Location:form.php?error=".$Error);
}
On your form.php
if(isset($_GET['error']) && $_GET['error']!=""){
echo $_GET['error'];
}
In your process.php change the code to
<?php
if (isset($_POST["submit"])) {
$Error ="";
if (isset($_POST["name"]) && $_POST["name"]!="") {
$Error = "Missing Name";
}
if (isset($_POST["location"]) && $_POST["location"]!="") {
$Error = "Missing Location";
}
if (isset($_POST["language"]) && $_POST["language"]!="") {
$Error = "Missing language";
}
if (isset($_POST["cookies"]) && $_POST["cookies"]!="") {
$Error = "Select cookies";
}
if($Error!=""){
header("Location:form.php?error=".$Error);
}
$name = $_POST['name'];
$location = $_POST['location'];
$language = $_POST['language'];
$comment = $_POST['comment'];
$cookies = $_POST['cookies'];
}
?>
Either you need a form to redirect back to your form.php, or move the echo $Error to your process.php so you can show the error from that page.

Echo other content after form validation and submission

How to clear page content and show other content after I validate and submit a form?
<?php
if(isset($_POST['Confirm'])) {
$to = "email#email.com";
$error = 0;
$first_name = $_POST['first_name'];
//Validation things
if(trim($first_name) == '') {$error = 1; $first_namerr = 1;}
$msg ="
First Name:$first_name
-------------------------
";
$sub ="Contact";
$from = "From: Support form";
#mail($to, $sub, $msg, $from);
//How to clear actual content and echo other ?
}
}
?>
<form name="contact" id="contact" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<label for="first_name" class="inner_text"><?php if ($error != 0){ if ($first_namerr == 1) {print("<font style='color: Red;'>");} }?>First Name<?php if ($error != 0){if ($first_namerr == 1) {print("</font>");}} ?></label>
<input id="first_name" name="first_name" size="30" type="text" value="<? echo $first_name; ?>" /><?php if ($error != 0){ if ($first_namerr == 1) {print('<img src="images/error.gif">');} }?>
<input type="submit" id="Confirm" name="Confirm" value="Confirm" />
</form>
I'm beginner in PHP, so please be explicit if you want to give an answer!
You could use an if/else statement to control the content being displayed - e.g:
<?php
if (isset($_POST['Confirm'])) {
// Your mail code
?>
Thank you, your message has been sent! <!-- This content is shown after form submission -->
<?php } else { ?>
<!-- Display your email form -->
<?php }; ?>

How to return to a certain section of a page when a form comes back with errors

I have a one page website. Each menu item scrolls you to a different part of the page. I have a HTML and PHP form which reloads the page and displays an error message if the form is submitted with a required field not filled. The problem is it reloads to the top of the page whereas my form is at the bottom of the page.
How can I get the form to reload the page to the section where the contact form is? The form already does exactly that when the form is submitted successfully but not when there are errors.
Any help would be great. Thanks for reading.
HTML
<div id="contactBox">
<form name="contact" id="contactForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="formLeft">
<p>
<label for="name">Name</label>
<?php if ($missing && in_array('name', $missing)) { ?>
<div class="warningDivLeft">
<span class="warning">Please enter your name</span>
</div>
<?php } ?>
<input type="text" name="name" id="name" tabindex="10"
<?php
if ($errors || $missing) {
echo 'value="' . htmlentities($name, ENT_COMPAT, 'utf-8') . '"';
}
?>
>
</p>
<p>
<label for="company">Company Name/Website</label>
<input type="text" name="company" id="company" tabindex="30"
<?php
if ($errors || $missing) {
echo 'value="' . htmlentities($company, ENT_COMPAT, 'utf-8') . '"';
}
?>
>
</p>
<p>
<label for="email">Email</label>
<?php if ($missing && in_array('email', $missing)) { ?>
<div class="warningDivLeft">
<span class="warning">Please enter your email</span>
</div>
<?php } elseif (isset($errors['email'])) { ?>
<div class="warningDivLeft">
<span class="warning">Invalid email address</span>
</div>
<?php } ?>
<input type="email" name="email" id="email" tabindex="40"
<?php
if ($errors || $missing) {
echo 'value="' . htmlentities($email, ENT_COMPAT, 'utf-8') . '"';
}
?>
>
</p>
<p>
<label for="phone">Phone</label>
<input type="text" name="phone" id="phone" tabindex="50"
<?php
if ($errors || $missing) {
echo 'value="' . htmlentities($phone, ENT_COMPAT, 'utf-8') . '"';
}
?>
>
</p>
<p>
<label for="contactYou">Contact you by...</label>
<?php if ($missing && in_array('contactYou', $missing)) { ?>
<div class="warningDivLeft">
<span class="warning">Please select one</span>
</div>
<?php } ?>
<select name="contactYou" size="1" id="contactYou" tabindex="60">
<option value="" selected="selected">- select</option>
<option value="email" <?php echo ($contactYou == 'email') ? ' selected="selected"' : ''; ?>>Email</option>
<option value="phone" <?php echo ($contactYou == 'phone') ? ' selected="selected"' : ''; ?>>Phone</option>
</select>
</p>
</div>
<div id="formRight">
<p>
<label for="interest">I am interested in...</label>
<?php if ($missing && in_array('interest', $missing)) { ?>
<div class="warningDiv">
<span class="warning">Please select one</span>
</div>
<?php } ?>
<select name="interest" size="1" id="interest" tabindex="80">
<option value="" selected="selected">- select</option>
<option value="new" <?php echo ($interest == 'new') ? ' selected="selected"' : ''; ?>>Creating a new website</option>
<option value="current" <?php echo ($interest == 'current') ? ' selected="selected"' : ''; ?>>Redesigning a current website</option>
<option value="responsive" <?php echo ($interest == 'responsive') ? ' selected="selected"' : ''; ?>>Reponsive web design</option>
<option value="wordpress" <?php echo ($interest == 'wordpress') ? ' selected="selected"' : ''; ?>>A WordPress website</option>
<option value="general" <?php echo ($interest == 'general') ? ' selected="selected"' : ''; ?>>General enquiry</option>
</select>
</p>
<p>
<label for="budget">My budget is...</label>
<?php if ($missing && in_array('budget', $missing)) { ?>
<div class="warningDiv">
<span class="warning">Please select one</span>
</div>
<?php } ?>
<select name="budget" size="1" id="budget" tabindex="90">
<option value="" selected="selected">- select</option>
<option value="100" <?php echo ($budget == '100') ? ' selected="selected"' : ''; ?>>€100 - €500</option>
<option value="500" <?php echo ($budget == '500') ? ' selected="selected"' : ''; ?>>€500 - €1,000</option>
<option value="1000" <?php echo ($budget == '1000') ? ' selected="selected"' : ''; ?>>€1,000 - €2,000</option>
<option value="2000" <?php echo ($budget == '2000') ? ' selected="selected"' : ''; ?>>€2,000 - €5,000</option>
<option value="5000" <?php echo ($budget == '5000') ? ' selected="selected"' : ''; ?>>€5,000 - €10,000</option>
<option value="10000" <?php echo ($budget == '10000') ? ' selected="selected"' : ''; ?>>€10,000+</option>
</select>
</p>
<p>
<label for="comments">How can I help you?</label>
<?php if ($missing && in_array('comments', $missing)) { ?>
<div class="warningDiv">
<span class="warning">Please leave a comment</span>
</div>
<?php } ?>
<textarea name="comments" id="comments" cols="45" rows="5" tabindex="100"><?php
if ($errors || $missing) {
echo htmlentities($comments, ENT_COMPAT, 'utf-8');
}
?></textarea>
</p>
</div>
<div id="formSubmit">
<ul>
<li>
<input type="submit" name="submit" id="submit" value="Send Message" tabindex="70">
</li>
</ul>
</div>
</form>
PHP
<?php
$errors = array();
$missing = array();
if (isset($_POST['submit'])) {
$to = 'celinehalpin#hotmail.com';
$subject = 'Web Design';
$expected = array('name', 'company', 'email', 'phone', 'contactYou', 'interest', 'budget', 'comments');
$required = array('name', 'email', 'contactYou', 'interest', 'budget', 'comments');
$headers = "From: webmaster#example.com\r\n";
$headers .= "Content-type: text/plain; charset=utf-8";
$authenticate = '-fcelinehalpin#hotmail.com';
require './_includes/mail_process.php';
if ($mailSent) {
header('Location: ' . $_SERVER['REQUEST_URI'] . '?success=1#c');
exit;
}
}
?>
<?php if (($_POST && $suspect) || ($_POST && isset($errors['mailfail']))) { ?>
<div class="globalWarning">
<p class="warning">Sorry your mail could not be sent</p>
</div>
<?php } elseif ($errors || $missing) { ?>
<div class="globalWarning">
<p class="warning">Please fix the item(s) indicated</p>
</div>
<?php } elseif (isset($_GET['success'])) { ?>
<div class="globalAlert">
<p class="warning">Thank you! Your message has been sent!</p>
</div>
<?php } ?>
<?php
$suspect = false;
$pattern = '/Content-Type:|Bcc:|Cc:/i';
function isSuspect($val, $pattern, &$suspect) {
if (is_array($val)) {
foreach ($val as $item) {
isSuspect($item, $pattern, $suspect);
}
} else {
if (preg_match($pattern, $val)) {
$suspect = true;
}
}
}
isSuspect($_POST, $pattern, $suspect);
if (!$suspect) {
foreach ($_POST as $key => $value) {
$temp = is_array($value) ? $value : trim($value);
if (empty($temp) && in_array($key, $required)) {
$missing[] = $key;
$$key = '';
} elseif(in_array($key, $expected)) {
$$key = $temp;
}
}
}
if (!$suspect && !empty($email)) {
$validemail = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($validemail) {
$headers .= "\r\nReply-to: $validemail";
} else {
$errors['email'] = true;
}
}
if (!$suspect && !$missing && !$errors) {
$message = '';
foreach ($expected as $item) {
if (isset($$item) && !empty($$item)) {
$val = $$item;
} else {
$val = 'Not selected';
}
if (is_array($val)) {
$val = implode(', ', $val);
}
$item = str_replace(array('_', '-'), ' ', $item);
$message .= ucfirst($item) . ": $val\r\n\r\n";
}
$message = wordwrap($message, 70);
$mailSent = mail($to, $subject, $message, $headers, $authenticate);
if (!$mailSent) {
$errors['mailfail'] = true;
}
}
It sounds like what you need is some Javascript to do the scrolling. I don't know if you're using jQuery or not, but it makes this fairly easy. This Stack Overflow link describes a number of approaches to it, such as:
$.fn.scrollView = function () {
return this.each(function () {
$('html, body').animate({
scrollTop: $(this).offset().top
}, 1000);
});
}
and calling it like:
$('#your-form').scrollView();
In addition to that, you might want to consider seeing if you can do any input validation BEFORE the form is submitted. Check out HTML5's input patterns and required fields
I would put some javascript within your error php so that when that loads it runs the javascript thus scrolling to the correct location based on form ID
DEMO http://jsfiddle.net/RbxVJ/2/
$(function() {
$(document).scrollTop( $("#header").offset().top );
});
Actually I would use pure javascript as you may not have called the jQuery library at this point
EDITED
Your PHP
DEMO http://jsfiddle.net/RbxVJ/430/
<?php
if(!isset($_GET['success']))
{
?>
<script>
window.location.hash = '#your-form-ID';
</script>
<?php
}
?>
If you are going to be using JavaScript to solve the problem, you might as well use it to validate your form rather/in addition to PHP. It is quicker (avoiding the necessary refresh with PHP), and you should never find yourself in the position to have to reload the form in any way other than with a successful submission.
Write a function that executes when the submit button is pressed that gathers the required fields, checks them for appropriate values, stops form submission if any are missing, and then, alters the HTML with reminder text next to those that 'broke the rules' as it were.
$(document).ready(function(){
$('#contactBox a[href="' + window.location.hash + '"]').click();
});
I was having a similar issue. When the form on the current page was submitted I would reload the current page. My form was at the bottom of the page, but when the page was reloaded the page would be scrolled to the very top again. Reloading the current page is done with;
<form action="">
So if my form is on my index.html page, this is the same as;
<form action="index.html">
To solve the scrolling issue I gave the form element an id and then referenced this id in the 'action' attribute value, like so;
<form id="contact-form" action="#contact-form">
Again if the form is on my index.html page, this is the same as;
<form id="contact-form" action="index.html#contact-form">
If you weren't aware, when you append an element id to the end of the web page URL, it will load that web page and scroll directly to that element.
If there are any concerns with this method please chime in.

Strange letters in mail sent from mail-form in Wordpress

I need to change the charset of the mail form used on this side: http://www.erik-dalsgaard.dk/kontakt/
I need to include these letters: Æ, æ, Ø, ø, Å, å
Right now the letters it output when it sends a mail is Æ, Ø,à instead of the letters above.
The php for the mailform is:
<?php
/*
Template Name: Contact
*/
get_header(); ?>
<?php
//If the form is submitted
if(isset($_POST['submitted'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactName']) === '') {
$nameError = 'You forgot to enter your name.';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) === '') {
$emailError = 'You forgot to enter your email address.';
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['comments']) === '') {
$commentError = 'You forgot to enter your comments.';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = get_option_tree('pr_contact_email');
$subject = 'Henvendelse fra hjemmeside fra '.$name;
$msubject = trim($_POST['subject']);
$body = "Navn: $name \n\nE-Mail: $email \n\nEmne: $msubject \n\nBesked: $comments";
$headers = 'From: Besked fra hjemmeside <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<?php get_header(); ?>
<div class="inner custom_content">
<div class="content <?php global_template(content); ?>">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if(the_content()){ ?>
<div class="divider"></div>
<?php } ?>
<?php endwhile; endif; ?>
<?php if(isset($emailSent) && $emailSent == true) { ?>
<div class="form-success">
<?php echo get_option_tree('pr_form_success'); ?>
</div>
<?php } else { ?>
<div class="form-success">
<?php echo get_option_tree('pr_form_success'); ?>
</div>
<form action="<?php the_permalink(); ?>" id="contactForm" class="big_form" method="post" accept-charset="UTF-8">
<ul class="forms">
<li>
<label for="contactName">Navn: *</label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField <?php if($nameError != '') { ?>hightlight<?php } ?>" />
</li>
<li><label for="email"><?php tr_translate(email); ?>: *</label>
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="requiredField email <?php if($emailError != '') { ?>hightlight<?php } ?>" />
</li>
<li><label for="subject">Emne:</label>
<input type="text" name="subject" id="subject" value="<?php if(isset($_POST['subject'])) echo $_POST['subject'];?>" />
</li>
<li class="textarea"><label for="commentsText">Besked: *</label>
<textarea name="comments" id="commentsText" rows="8" cols="60" class="requiredField <?php if($commentError != '') { ?>hightlight<?php } ?>"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
</li>
<li class="buttons">
<input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit" class="button light"><?php tr_translate(submit_contact); ?></button>
<div class="loading"></div>
</li>
</ul>
</form>
</div><!-- .content End -->
<!-- Content End -->
<?php } ?>
<?php global_template(sidebar); ?>
<?php get_footer(); ?>
There are more places where this issue might come but fist verify if:1. MySQL 5 doesn't support full UTF-8 characters2. The e-mail client/host doesn't support full UTF-8 charactersIt is easy to verify this, check your website/your database if you detect this issues there also then you might try to use this plugin or search for similar ones.The bad part is that the issue might come from the e-mail client (ex: thunderbird or outlook) or even from the e-mail host.
I encontered an issue with some language specific characters that showed just fine in both yahoo and gmail webmail but not in any roundcube hosts. I ended up replaceing my characters with "normal" ones.(I haven't tryed the ubove plugin).Check the plugin it says it recodes the characters so it should do the trick.Regards.

Categories