I'm having all kinds of issues with contact form. When I test on my home server everything runs smoothly. Once I upload it online it doesn't work. First there were problems with headers and now apparently "This web page has a redirect loop".
Here's my code. Please advice me what to do.
Thanks.
<?php
// Title: Contact Form - Dolce Forno GB
// Updated: 5/9/2012
//Validation code
if (!empty($_POST)) {
$errors = array();
//variables
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//All field are required
if (empty($name) === true || empty($email) === true || empty($phone) === true || empty($subject) === true || empty($message) === true ){
$errors[] = 'Please fill in all the fields.';
}
else {
//This regex allows only: a-z,A-Z, space, comma, full stop, apostrophe, dash
if (!preg_match("/^[a-zA-Z\s,.'-]+$/", $name)) {
$errors[] = 'Invalid name.';
/*die ("Invalid name."); */
}
//var_filter php function
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'Invalid email address.';
}
//This regex allows only: 0-9, space, dash, brackets, min-max length: 10-15
if (!preg_match("/^[0-9\s]{10,15}$/", $phone)){
$errors[] = 'Invalid phone number.';
}
}
}
if (empty($errors)) {
//send email
mail('info#dolcefornogb.com', 'Contact Form', $subject, 'Message:' . $message,'From: ' . $name . $email . $phone);
header('Location:mail.php?sent');
exit ();
}
print_r($errors);
?>
<DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
if (isset($_GET['sent']) === true) {
echo '<p>Thanks for contacting us!</p>';
}
else {
if (!empty($errors)){
echo '<ul>';
foreach ($errors as $error){
echo '<li>', $error,'</li>';
echo '</ul>';
}
}
?>
<form action="" method="post">
<p> <label for="name">Name
<span class="small">Add your name </span></label>
<input type="text" name="name" id="name"
<?php
if(isset($_POST['name']) === true){
echo 'value="', strip_tags($_POST['name']),'"';
}
?>
>
</p>
<p> <label for="email">E-mail address
<span class="small"> Add your e-mail</span></label>
<input type="text" name="email" id="email"
<?php
if(isset($_POST['email']) === true){
echo 'value="', strip_tags($_POST['email']),'"';
}
?>
>
</p>
<p><label for="phone">Phone<span class="small"> Add your phone number</span></label>
<input type="text" name="phone" id="phone"
<?php
if(isset($_POST['phone']) === true){
echo 'value="', ($_POST['phone']),'"';
}
?>
>
</p>
<p><label for="suject">Subject </label>
<input type="text" name="subject" id="subject"
<?php
if(isset($_POST['subject']) === true){
echo 'value="', strip_tags($_POST['subject']),'"';
}
?>
>
</p>
<p><label for="message">Message:</label>
<textarea name="message" id="messgae" rows="10" cols="50">
<?php
if(isset($_POST['message']) === true){
echo strip_tags($_POST['message']);
}
?></textarea>
</p>
<p><label for="call">Request Phone Call</label>
Yes:<input type="radio" value="Yes" name="call">
No:<input type="radio" value="No" name="call">
</p>
<p class="buttons">
<input type="submit" value="Send"> <input type="reset" value="Clear">
</p>
</form>
<?php
}
?>
Try redirecting to a different page with just the success message in it.
i.e. replace
header('Location:mail.php?sent');
with
header('Location:mail-success.php?sent');
Then get rid of (move to the new page)
if (isset($_GET['sent']) === true) {
echo '<p>Thanks for contacting us!</p>';
}
Also try adding 303 status to the header call
http://www.electrictoolbox.com/php-303-redirect/
Related
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.
how to pass the collected input to another page after self validate in php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Title Goes Here</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="form1.css"/>
</head>
<body>
<?php
//define variable and set to empty value
$forenameErr = $surnameErr = $emailErr = $postalAddressErr = $landLineTelNoErr =$mobileTelNoErr = $sendMethodErr = "";
$forename = $surname = $email = $postalAddress = $landLineTelNo = $mobileTelNo = $sendMethod = "";
if($_SERVER["REQUEST_METHOD"] =="POST"){
$valid = true;
if(empty($_POST["forename"])){
$forenameErr = "Forename is required";
$valid = false; //false
} else {
$forename = test_input($_POST["forename"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$forename)) {
$forenameErr = "Only letters and white space allowed";
}
}
if(empty($_POST["surname"])){
$surnameErr = "Surname is required";
$valid = false; //false
} else {
$surname = test_input($_POST["surname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
$surnameErr = "Only letters and white space allowed";
}
}
if(empty($_POST["postalAddress"])){
$postalAddressErr =" Please enter postal address";
$valid = false; //false
} else {
$postalAddress = test_input($_POST["postalAddress"]);
}
if(empty($_POST["landLineTelNo"])){
$landLineTelNoErr = "Please enter a telephone number";
$valid = false; //false
} else {
$landLineTelNo = test_input($_POST["landLineTelNo"]);
// check if invalid telephone number added
if (!preg_match("/^[0-9 ]{7,}$/",$landLineTelNo)) {
$landLineTelNoErr = "Invalid telephone number entered";
}
}
if(empty($_POST["mobileTelNo"])){
$mobileTelNoErr = "Please enter a telephone number";
$valid = false; //false
} else {
$mobileTelNo = test_input($_POST["mobileTelNo"]);
// check if invalid telephone number added
if (!preg_match("/^[0-9 ]{7,}$/",$mobileTelNo)) {
$mobileTelNoErr = "Invalid telephone number entered";
}
}
if(empty($_POST["email"])){
$emailErr = "Email is required";
$valid = false; //false
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if(empty($_POST["sendMethod"])){
$sendMethodErr = "Contact method is required";
$valid = false; //false
} else {
$sendMethod = test_input($_POST["sendMethod"]);
}
//if valid then redirect
if($valid){
header('Location: userdetail.php');
exit();
}
}
//check
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="wrapper">
<h1>Welcome to Chollerton Tearoom! </h1>
<nav>
<ul>
<li>Home</li>
<li>Find out more</li>
<li>Offer</li>
<li>Credit</li>
<li>Admin</li>
<li>WireFrame</li>
</ul>
</nav>
<form id = "userdetail" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<fieldset id="aboutyou">
<legend id="legendauto">user information</legend>
<p>
<label for="forename">Forename: </label>
<input type="text" name="forename" id="forename" value="<?php echo $forename;?>">
<span class="error">* <?php echo $forenameErr;?></span>
</p>
<p>
<label for="surname">Surname:</label>
<input type="text" name="surname" id="surname" value="<?php echo $surname;?>">
<span class="error">* <?php echo $surnameErr;?></span>
</p>
<p>
<label for="postalAddress">Postal Address:</label>
<input type="text" name="postalAddress" id="postalAddress" value="<?php echo $postalAddress;?>">
<span class="error"> </span>
</p>
<p>
<label for="landLineTelNo">Landline Telephone Number:</label>
<input type="text" name="landLineTelNo" id="landLineTelNo" value="<?php echo $landLineTelNo;?>" >
<span class="error"> * <?php echo $landLineTelNoErr;?></span>
</p>
<p>
<label for="mobileTelNo">Moblie:</label>
<input type="text" name="mobileTelNo" id="mobileTelNo" placeholder="example:012-3456789" value="<?php echo $mobileTelNo;?>" />
<span class="error"><?php echo $mobileTelNoErr;?></span>
</p>
<p>
<label for="email">E-mail:</label>
<input type="text" name="email" id="email" value="<?php echo $email;?>" placeholder="example:123#hotmail.com"/>
<span class="error"> </span>
</p>
<fieldset id="future">
<legend>Lastest news</legend>
<p>
Choose the method you recommanded to recevive the lastest information
</p>
<br>
<input type="radio" name="sendMethod" id="sendMethod" <?php if (isset($sendMethod) && $sendMethod=="email") echo "checked";?> value="email">
Email
<input type="radio" name="sendMethod" id="sendMethod" <?php if (isset($sendMethod) && $sendMethod=="post") echo "checked";?> value="post">
Post
<input type="radio" name="sendMethod" id="sendMethod" <?php if (isset($sendMethod) && $sendMethod=="SMS") echo "checked";?> value="SMS">
SMS
<span class="error">* <?php echo $sendMethodErr;?></span>
</fieldset>
<p><span class="error">* required field.</span></p>
<input type="checkbox" name="checkbox" value="check" id="agree" />
I have read and agree to the Terms and Conditions and Privacy Policy
<p>
<input type="submit" name="submit" value="submit" />
</p>
</form>
</fieldset>
</form>
</div>
</body>
</html>
here is my php form...
it can validate itself in the same page but couldn't pass the data to another php page....
here is my another php code...
<?php
$forenameErr = $surnameErr = $emailErr = $postalAddressErr = $landLineTelNoErr =$mobileTelNoErr = $sendMethodErr = "";
$forename = $surname = $email = $postalAddress = $landLineTelNo = $mobileTelNo = $sendMethod = "";
echo "<h1>Successfull submission :</h1>";
echo "<p>Forename : $forename <p/>";
echo "<p>Surname : $surname <p/>";
echo "<p>Email: $email</p>";
echo "<p>Post Address: $postalAddress</p>";
echo "<p>Landline: $landLineTelNo</p>";
echo "<p>Mobile : $mobileTelNo</p>";
echo "<p>Contact method: $sendMethod";
?>
You can use $_SESSION variables.
PHP $_SESSIONS
PHP Sessions and Cookies
So after the users has been validated set $_SESSION['surname'] = $surname;
Then on the top of each page add session_start(); to the top.
Then Under that add
if (isset($_SESSION['surname'])) {
$surname = $_SESSION['surname'];
} else {
die();
}
View the PHP docs for a more thorough understanding.
You may also want to look into setting up a MYSQL database if you want users to be able to create accounts.
Edit: form page
if($valid){
$_SESSION['surname'] = $surname;
$_SESSION['postalAddress'] = $postalAddress;
header('Location: userdetail.php');
exit();
}
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.
I am trying to send an email from the same PHP page after Javascript validation but have some issues sending email. Can someone help me out with this?
Here is the JavaScript code:
<!-- -->
<script type="text/javascript">
function checkCheckBoxes() {
if (document.form.inforequired.checked == true)
{
if (document.form.option1.checked == false && document.form.option2.checked == false && document.form.option3.checked == false && document.form.option4.checked == false && document.form.option5.checked == false && document.form.option6.checked == false && document.form.option7.checked == false && document.form.option8.checked == false && document.form.option9.checked == false && document.form.option10.checked == false)
{
document.getElementById('error').innerHTML = "Please choose your options.";
return false;
}
if (document.form.option8.checked == true || document.form.option9.checked == true || document.form.option10.checked == true)
{
if (document.form.address.value == "" )
{
document.getElementById('erroraddress').innerHTML = "Please enter your address.";
return false;
}
}
else
{
document.forms["form"].submit();
return true;
}
}
else
{
document.forms["form"].submit();
return true;
}
}
</script>
<!-- Email -->
Here is the PHP code to send email in the else part.
<div id="content">
<h1 id="formSpacingHeading"> Visitor Information </h1>
<?if( !isset($_POST['submit'])):?>
<form id="form" name="form" onsubmit="return checkCheckBoxes();" action="" method="POST">
<p id="formSpacing"><label for="username" class="iconic user" > Name <span class="required">*</span></label> <input type="text" name="username" id="username" required="required" placeholder="Enter your name" /></p>
<p id="formSpacing"><label for="usermail" class="iconic mail-alt"> E-mail address <span class="required">*</span></label> <input type="email" name="usermail" id="usermail" placeholder="Enter your E-mail ID" required="required" /> </p>
<p id="formSpacing"><label for="contactno" class="iconic link"> Contact number <span class="required">*</span></label> <input type="text" pattern="^\d\d\d\d\d\d\d\d\d\d$" name="contact" id="contactno" placeholder="Enter your contact number" required="required" /></p>
<input type="submit" name="submit" value="Submit Form" />
</form>
<?php else :
$name=$_POST['username'];
$mail=$_POST['usermail'];
$contact=$_POST['contact'];
$to = "admin#xxx.com";
$subject = "New Visitor Information";
$message = $name;
$from = $mail;
$headers = "From:" . $from;
$email = mail($to, $subject, $message, $headers);
if($email){
echo "Thank you. We will keep you updated with the latest information.";
}
else{
echo "Error processing your request. Please try again later.";
}
?>
<? endif ?>
I'm not a PHP guru, but I have programmed a few basic things in it.
I think you have your if statement all wrong.
Change..
<?if( !isset($_POST['submit'])):?>
to
<?if( !isset($_POST['submit'])){?>
Change
<?php else :
To
<?php } else {
Change
?>
<? endif ?>
to
}?>
Hi I got a contact from script the internet that I have been messing around with, only problem is that the verification that I am trying to add to it just doesn't work. Basically in the form it has name, email, number, type and comment. My main verification woes are with the number field I would like it so that if it is empty it echos "no number" and when the person type in letters instead of numbers it will echo "you need to type with numbers". Something like lol. but I’m stuck. Can any of you geniuses help me? Thanks in advance here is the full code below. p.s. sorry about previous post i accidently cut off the script:$
<?php
$nowDay=date("d.m.Y");
$nowTime=date("H:i:s");
$subject = "E-mail from my site!";
if (isset($_POST['submit'])) {
//contactname
if (trim($_POST['name'] == '')) {
$hasError = true;
} else {
$name = htmlspecialchars(trim($_POST['name']));
}
//emailaddress
if (trim($_POST['email'] == '')) {
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i",trim($_POST['name']))) {
$hasError = true;
} else {
$email = htmlspecialchars(trim($_POST['email']));
}
//phonenumber
if (trim($_POST['number'] == ''))
{
$fake_num = true;
}
else if(!is_numeric($_POST['phonenumber']))
{
$fake_num = true;
}
else
{
$number = htmlspecialchars(trim($_POST['number']));
}
//type
$type = trim($_POST['type']);
//comment
if (trim($_POST['comment'] == '')) {
$hasError = true;
} else {
$comment = htmlspecialchars(trim($_POST['comment']));
}
if (!isset($hasError) && !isset($fake_num)) {
$emailTo = 'email#hotmail.com';
$body = " Name: $name\n\n
Email: $email\n\n
Phone number: $number\n\n
Type: $type\n\n
Comment: $comment\n\n
\n This message was sent on: $nowDay at $nowTime";
$headers = 'From: My Site <'.$emailTo.'>'."\r\n" .'Reply-To: '. $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<?php
if(isset($hasError))
{
echo"<p>form has error</p>";
}
?>
<?php
if(isset($fake_num))
{
echo"<p>wrong num</p>";
}
?>
<?php
if(isset($emailSent) && $emailSent == true)
{
echo "<p><strong>Email Successfully Sent!</strong></p>";
echo "<p>Thank you <strong> $name </strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>";
}
?>
<div id="stylized" class="myform">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="bookingform" id="bookingform">
<h1>Booking form</h1>
<label>
Name
<span class="small"></span>
</label>
<input type="text" name="name" id="name" class="required"/>
<label>
Your email:
<span class="small"></span>
</label>
<input type="text" name="email" id="email" class="required"/>
<label>
Contact Number:
<span class="small"></span>
</label>
<input type="text" name="number" id="number" class="required" />
<label>
Car required:
<span class="small"></span>
</label>
<select name="type" id="type">
<option selected="selected">Manual</option>
<option>Automatic</option>
</select>
<label>
Comment:
<span class="small"></span>
</label>
<textarea cols="" rows="" name="comment" id="comment" class="required"></textarea>
<button type="submit" name="submit">Submit</button>
</form>
</div>
For checking whether a number has been entered you can use:
if (!preg_match('/^?[0-9]{0,10}$/', $_POST['number'])) {
echo "Please enter a valid number"; //Failed to meet criteria
}
Here you can also specify the amount of numbers that would constitute your valid number with the braces {0,10}, in this case, the number can be up to 11 digits long. If you required it to be only 11 digits long, then use {11}.
If you wish to check if a number has been entered at all you can use:
if (empty($_POST['number'])) {
echo "Please enter a valid number"; //Field was left empty
}
PHP does have a built-in email validation function that you can use
filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)
which returns true if the entered email is in the correct format.