Im trying to create a form to send me an email when the user fills all the fields, I followed a tutorial to put it together so whats there works, but I want to implement an option that allows people to pick other... Heres how it is:
I have a form that asks for your: Name, Email, Type and Message.
The type has 4 options: Feedback, Bug Report, Feature Request and other, I would love if when the other radio button is clicked then the text box HAS to have an input, so they cant just skip by pressing other, but if they pick something else that isnt other then I dont want them to have to type in the text box, but no matter what I try it either doesnt work at all or HAS to have an input in the text box even if I dont select the other radio button...
The problem is resolved, I request that this topic remains open so that others can continue to use this page, or incase I break something... Thanks Rasclatt, You were an amazing help :)
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<link href="contact/css/contactForm.css" rel="stylesheet">
<title>Contact Us</title>
<?php error_reporting(0); ?>
<?php if(isset($_POST['Email'])): ?>
<?php
print_r($_POST);
// This is where you process the email and display the thank you page
$to = 'harryfelton12#gmail.com';
$subject = 'ALERT! Website Form Submission';
$message = 'Users Email: '.strip_tags($_POST['Email'])."\n";
$message .= 'Submitted Message: '.strip_tags($_POST['Comment'])."\n";
$message .= 'Message Type: '.strip_tags($_POST['Type'])."\n";
$headers = 'From: harrywebsite#form.com' . "\r\n" .
'Reply-To: '.strip_tags($_POST['Email']) . "\r\n";
// If the mail is successful, display thanks or display failed
?>
<?php if(mail($to, $subject, $message, $headers)): ?>
// Display the thank you page }
<div id="comments_form">
<h2 style="size:35px;">Thanks For Your Message!</h2>
<p style="font-size: 20px; font-family:Arial, Helvetica, sans-serif; color:#000; margin-left: 25px;">You Will Be Redirected Shortly!</p>
<p style="font-size: 15px; font-family:Arial, Helvetica, sans-serif; color:#000; margin-left: 25px;">Expect your message to be responded to within 2 working days</p>
<script type="text/javascript">
setTimeout('redirectPage()', 3000)
function redirectPage() {
location.href="index.html"
};
</script>
<?php else: ?>
<div id="comments_form">
<h2 style="size:35px;">Uh Oh! Your Message Could Not Be Sent</h2>
<p style="font-size: 20px; font-family:Arial, Helvetica, sans-serif; color:#000; margin-left: 25px;">An Unexpected Error Occured While Trying To Send Your Message</p>
<p style="font-size: 15px; font-family:Arial, Helvetica, sans-serif; color:#000; margin-left: 25px;">You Are Being Redirected To The Home Page</p>
<script type="text/javascript">
setTimeout('redirectPage()', 3000)
function redirectPage() {
location.href="index.html"
};
</script>
<?php endif ?>
</div>
<?php else: ?>
<form method="post" id="comments_form">
<div class="row">
<div class="label">
Your Name
</div>
<!--.label end-->
<div class="input">
<input type="text" id="fullname" class="detail" name="Name"
value="<?php echo isset($_POST['Name'])? $_POST['Name'] : ''; ?>" />
<?php if(in_array('Name', $validation)): ?>
<span class="error"><?php echo $error_messages['Name']; ?></span>
<?php endif; ?>
</div>
<!--.input end-->
<div class="context">
e.g. John Smith or Jane Doe
</div>
<!--end .context-->
</div>
<!--.row end-->
<div class="row">
<div class="label">
Your Email
</div>
<!--.label end-->
<div class="input">
<input type="text" id="email" class="detail" name="Email" value="<?php echo isset($_POST['Email'])? $_POST['Email'] : ''; ?>" />
<?php if(in_array('Email', $validation)): ?>
<span class="error"><?php echo $error_messages['Email']; ?></span>
<?php endif; ?>
</div>
<!--.input end-->
<div class="context">
We wont spam you! We only need this to reply to questions you might pose
</div>
<!--end .context-->
</div>
<!--.row end-->
<div class="row">
<div class="label">
Type Of Message
</div>
<!--.label end-->
<div class="input">
<input type="radio" name="Type" onChange="GetValue(this)" value="Feedback" checked="checked" />
Feedback <br />
<input type="radio" name="Type" onChange="GetValue(this)" value="Feature Request" <?php echo (isset($_POST['Type']) && $_POST['Type'] == 'Feature Request')? 'checked="checked"' : ''; ?> />
Feature Request<br>
<input type="radio" name="Type" onChange="GetValue(this)" value="Bug Report" <?php echo (isset($_POST['Type']) && $_POST['Type'] == 'Bug Report')? 'checked="checked"' : ''; ?> />
Bug Report<br>
<input type="radio" name="Type" onChange="GetValue(this)" value="Other" id="other" <?php echo (isset($_POST['Type']) && $_POST['Type'] == 'Other')? 'checked="checked"' : ''; ?> />
Other<br />
<input type="text" style="display:none;" id="option" name="Type" <?php echo (isset($_POST['Type']) && $_POST['Type'] == 'option')? 'checked="checked"' : ''; ?> />
<?php if(in_array('Type', $validation)): ?>
<span class="error"> <?php echo $error_messages['Type']; ?> </span>
<?php endif; ?>
</div>
<!--.input end-->
<div class="context">
This is to help us better respond to your message
</div>
<!--end .context-->
</div>
<!--.row end-->
<div class="row">
<div class="label">
Your Message
</div>
<!--.label end-->
<div class="input2">
<textarea id="Comment" name="Comment" class="mess"><?php echo isset($_POST['Comment'])? $_POST['Comment'] : ''; ?></textarea>
<?php if(in_array('Comment', $validation)): ?>
<span class="error"><?php echo $error_messages['Comment']; ?></span>
<?php endif; ?>
</div>
<!--.input end-->
</div>
<!--.row end-->
<div class="submit">
<input type="submit" id="submit" name="Submit" value="Send Message" />
</div>
</form>
<?php endif; ?>
<script>
// This function just checks if the 'other' gets checked
function GetValue(ThisVal) {
var Writing = $(ThisVal).val();
// This just shows you what is happening via the feedback div
$('#feederback').html(Writing);
if (Writing == 'Other') {
// If other, disable the submit
$("#submit").prop("disabled", true);
// Fade in the message textarea
$('#option').fadeIn('fast');
// Check the message area to see if there is text in it already.
// If there is text enable the submit button
CheckVal();
} else {
// If not other, fade out the message
$('#option').fadeOut('fast');
// Enable the submit button
$('#submit').prop("disabled", false);
}
}
function CheckVal() {
var SetMess = $('#option').val();
$('#feedback').html(SetMess);
if (SetMess !== '') {
$('#submit').prop('disabled', false);
}
else {
$('#submit').prop('disabled', true);
}
}
// As one types, check that the message is not empty
$('#option').keyup(function () {
CheckVal();
});
// As one clicks into the field, see if it has content
$('#option').click(function () {
CheckVal();
});
$(document).ready(function() {
// validate form
$("#comments_form").validate({
// This will allow you to extend the validator functions
invalidHandler:
function(form, validator) {
// $("#get_online").val("CHECK");
},
rules: {
// For every named input that you want to validate,
// you create a little set of prefs
Name: {
required: true,
},
Email: {
required: true,
email: true
},
Type: { required: true },
Comment: { required: true },
},
messages: {
// Here are just the custom messages you can modify per field
Name: {
required: 'Please Enter Your Name',
},
Email: {
required: 'Please Enter An Email',
email: 'Email address not valid',
},
Type: { required: 'Please Select A Type' },
Comment: { required: 'Please Enter A Message'},
},
});
});
</script>
-Thanks in advance, harry
Ok so the validation is at the very bottom. I have it validating basic stuff. If you visit or google jQuery validation, you can see how to validate different fields. It's pretty easy. Make sure in your PHP that you filter ( strip_tags() ) the inputs so that you don't get hacked.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<link href="contact/css/contactForm.css" rel="stylesheet">
<title>Contact Us</title>
<?php error_reporting(0); ?>
<?php if(isset($_POST['Email'])): ?>
<?php
print_r($_POST);
// This is where you process the email and display the thank you page
$to = 'harryfelton12#gmail.com';
$subject = 'ALERT! Website Form Submission';
$message = 'Users Email: '.strip_tags($_POST['Email'])."\n";
$message .= 'Submitted Message: '.strip_tags($_POST['Comment'])."\n";
$message .= 'Message Type: ';
$message .= ($_POST['Type'] !== 'Option')? $_POST['Type']."\n": strip_tags($_POST['option'])."\n";
$headers = 'From: harrywebsite#form.com' . "\r\n" .
'Reply-To: '.strip_tags($_POST['Email']) . "\r\n";
// If the mail is successful, display thanks or display failed
?>
<?php if(mail($to, $subject, $message, $headers)): ?>
// Display the thank you page }
<div id="comments_form">
<h2 style="size:35px;">Thanks For Your Message!</h2>
<p style="font-size: 20px; font-family:Arial, Helvetica, sans-serif; color:#000; margin-left: 25px;">You Will Be Redirected Shortly!</p>
<p style="font-size: 15px; font-family:Arial, Helvetica, sans-serif; color:#000; margin-left: 25px;">Expect your message to be responded to within 2 working days</p>
<script type="text/javascript">
setTimeout('redirectPage()', 3000)
function redirectPage() {
location.href="index.html"
};
</script>
<?php else: ?>
<div id="comments_form">
<h2 style="size:35px;">Uh Oh! Your Message Could Not Be Sent</h2>
<p style="font-size: 20px; font-family:Arial, Helvetica, sans-serif; color:#000; margin-left: 25px;">An Unexpected Error Occured While Trying To Send Your Message</p>
<p style="font-size: 15px; font-family:Arial, Helvetica, sans-serif; color:#000; margin-left: 25px;">You Are Being Redirected To The Home Page</p>
<script type="text/javascript">
setTimeout('redirectPage()', 3000)
function redirectPage() {
location.href="index.html"
};
</script>
<?php endif ?>
</div>
<?php else: ?>
<form method="post" id="comments_form">
<div class="row">
<div class="label">
Your Name
</div>
<!--.label end-->
<div class="input">
<input type="text" id="fullname" class="detail" name="Name"
value="<?php echo isset($_POST['Name'])? $_POST['Name'] : ''; ?>" />
<?php if(in_array('Name', $validation)): ?>
<span class="error"><?php echo $error_messages['Name']; ?></span>
<?php endif; ?>
</div>
<!--.input end-->
<div class="context">
e.g. John Smith or Jane Doe
</div>
<!--end .context-->
</div>
<!--.row end-->
<div class="row">
<div class="label">
Your Email
</div>
<!--.label end-->
<div class="input">
<input type="text" id="email" class="detail" name="Email" value="<?php echo isset($_POST['Email'])? $_POST['Email'] : ''; ?>" />
<?php if(in_array('Email', $validation)): ?>
<span class="error"><?php echo $error_messages['Email']; ?></span>
<?php endif; ?>
</div>
<!--.input end-->
<div class="context">
We wont spam you! We only need this to reply to questions you might pose
</div>
<!--end .context-->
</div>
<!--.row end-->
<div class="row">
<div class="label">
Type Of Message
</div>
<!--.label end-->
<div class="input">
<input type="radio" name="Type" onChange="GetValue(this)" value="Feedback" checked="checked" />
Feedback <br />
<input type="radio" name="Type" onChange="GetValue(this)" value="Feature Request" <?php echo (isset($_POST['Type']) && $_POST['Type'] == 'Feature Request')? 'checked="checked"' : ''; ?> />
Feature Request<br>
<input type="radio" name="Type" onChange="GetValue(this)" value="Bug Report" <?php echo (isset($_POST['Type']) && $_POST['Type'] == 'Bug Report')? 'checked="checked"' : ''; ?> />
Bug Report<br>
<input type="radio" name="Type" onChange="GetValue(this)" value="Other" id="other" <?php echo (isset($_POST['Type']) && $_POST['Type'] == 'Other')? 'checked="checked"' : ''; ?> />
Other<br />
<input type="text" style="display:none;" id="option" name="option" <?php echo (isset($_POST['option']))? $_POST['option']: ''; ?> />
<?php if(in_array('Type', $validation)): ?>
<span class="error"> <?php echo $error_messages['Type']; ?> </span>
<?php endif; ?>
</div>
<!--.input end-->
<div class="context">
This is to help us better respond to your message
</div>
<!--end .context-->
</div>
<!--.row end-->
<div class="row">
<div class="label">
Your Message
</div>
<!--.label end-->
<div class="input2">
<textarea id="Comment" name="Comment" class="mess"><?php echo isset($_POST['Comment'])? $_POST['Comment'] : ''; ?></textarea>
<?php if(in_array('Comment', $validation)): ?>
<span class="error"><?php echo $error_messages['Comment']; ?></span>
<?php endif; ?>
</div>
<!--.input end-->
</div>
<!--.row end-->
<div class="submit">
<input type="submit" id="submit" name="Submit" value="Send Message" />
</div>
</form>
<?php endif; ?>
<script>
// This function just checks if the 'other' gets checked
function GetValue(ThisVal) {
var Writing = $(ThisVal).val();
// This just shows you what is happening via the feedback div
$('#feederback').html(Writing);
if (Writing == 'Other') {
// If other, disable the submit
$("#submit").prop("disabled", true);
// Fade in the message textarea
$('#option').fadeIn('fast');
// Check the message area to see if there is text in it already.
// If there is text enable the submit button
CheckVal();
} else {
// If not other, fade out the message
$('#option').fadeOut('fast');
// Enable the submit button
$('#submit').prop("disabled", false);
}
}
function CheckVal() {
var SetMess = $('#option').val();
$('#feedback').html(SetMess);
if (SetMess !== '') {
$('#submit').prop('disabled', false);
}
else {
$('#submit').prop('disabled', true);
}
}
// As one types, check that the message is not empty
$('#option').keyup(function () {
CheckVal();
});
// As one clicks into the field, see if it has content
$('#option').click(function () {
CheckVal();
});
$(document).ready(function() {
// validate form
$("#comments_form").validate({
// This will allow you to extend the validator functions
invalidHandler:
function(form, validator) {
// $("#get_online").val("CHECK");
},
rules: {
// For every named input that you want to validate,
// you create a little set of prefs
Name: {
required: true,
},
Email: {
required: true,
email: true
},
Type: { required: true },
Comment: { required: true },
},
messages: {
// Here are just the custom messages you can modify per field
Name: {
required: 'Please Enter Your Name',
},
Email: {
required: 'Please Enter An Email',
email: 'Email address not valid',
},
Type: { required: 'Please Select A Type' },
Comment: { required: 'Please Enter A Message'},
},
});
});
</script>
Related
I have a form:
When you press submit the page redirect to another (I embedded the .php in a page) where the errors are shown like "Name and Last Name are not valid" but I want to make when you press Submit and the text is not valid the standard Form HTML to see like "You need to insert email" or "This field is required". I have a headache because I tried a lot of thigs but nothing seems to work.
/*
* Template Name: ThankyouPage
*/
//wp_header();
get_header();
if(is_single() || is_page())
include ('inc/page_header_transparent.php');
else
include ('inc/page_header.php');
?>
<head>
<link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri();?>/css/bootstrap.min.css">
<link href="<?php echo get_stylesheet_directory_uri();?>/css/pages.scss">
<link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri();?>/css/default_thank_you.css">
</head>
<style type="text/css">
body {
min-width: 300px;
font-family: 'Akkurat-Regular', sans-serif;
background-color: #fffffe;
color: #1a1a1a;
text-align: center;
word-wrap: break-word;
-webkit-font-smoothing: antialiased;
}
</style>
<?php
if(isset($_POST['email'])) {
$email_to = "";
$email_subject = "";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
$errors = array(
'nume' => false,
);
$values = array(
'nume' => isset($_GET['nume']) ? $_GET['nume'] : "",
);
if (isset($_POST['nume'])) {
if (!isset($_POST['nume']) || empty($_POST['nume'])) {
$errors['nume'] = true;
}
}
$first_name = $_POST['nume'];
$email_from = $_POST['email'];
$telephone = $_POST['telefon'];
$city = $_POST['localitate'];
$comments = $_POST['detalii'];
$date = $_POST['data'];
$specializare = $_POST['specializare'];
$medic = $_POST['medic']; // required
$agreement= $_POST['agree']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "<h2></h2>\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "<br>Name: ".clean_string($first_name)."\n";
$email_message .= "<br>Email: ".clean_string($email_from)."\n";
$email_message .= "<br>Telephone: ".clean_string($telephone)."\n";
$email_message .= "<br>City: ".clean_string($city)."\n";
$email_message .= "<br>Comments: ".clean_string($comments)."\n";
$email_message .= "<br>Date: ".clean_string($date)."\n";
$email_message .= "<br>Speciality: ".clean_string($specializare)."\n";
$email_message .= "<br>Medic: ".clean_string($medic)."\n";
$email_message .= "<br><h3>Contact him now!</h3>";
// create email headers
$headers = "From: contact#template.com\r\n".
"Content-type:text/html;charset=UTF-8" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link href='https://fonts.googleapis.com/css?family=Lato:300,400|Montserrat:700' rel='stylesheet' type='text/css'>
<style>
#import url(//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.min.css);
#import url(//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css);
</style>
<script src="https://2-22-4-dot-lead-pages.appspot.com/static/lp918/min/jquery-1.9.1.min.js"></script>
<script src="https://2-22-4-dot-lead-pages.appspot.com/static/lp918/min/html5shiv.js"></script>
</head>
<body>
<header class="site-header" id="header">
<h1 class="site-header__title" data-lead-id="site-header-title">SUCCES!</h1>
</header>
<div class="main-content">
<i class="fa fa-check main-content__checkmark" id="checkmark"></i>
<p class="main-content__body" data-lead-id="main-content-body"></p>
</div>
</body>
</html>
<?php
}
?>
<?php
include ('inc/page_footer.php');
?>
<?php
get_footer();
?>
<form method="POST" id="contactForm" novalidate class="contact-form" action="/form">
<fieldset>
<legend><span class="number">1</span> Informatii Personale</legend>
<div class="row">
<div class="col-md-12 form-group">
<input type="text" class="form-control" name="nume" id="nume" placeholder="Nume Prenume *" required="" data-error="Completati numele dvs.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Email *" required="" data-error="Tastati o adresa de email valida">
<span class="error">* <?php echo $email_error;?></span>
</div>
<div class="col-md-6 form-group">
<input type="text" class="form-control" id="telefon" name="telefon" placeholder="Telefon *" required="" data-error="Tastati numarul corect de telefon">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<input type="text" class="form-control" id="localitate" name="localitate" placeholder="Localitatea *" required="" data-error="Completati localitatea dvs.">
<div class="help-block with-errors"></div>
</div>
</div>
</fieldset>
<fieldset>
<legend><span class="number">2</span> Ce va supara?</legend>
<div class="row">
<div class="col-md-12 form-group">
<textarea name="detalii" id="detalii" class="form-control" placeholder="detaliati aici ce va supara ....." required="" data-error="completati simtomatologia"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label for="data">Cand doresti programarea?</label>
<input class="mb0 form-control" type="date" name="data" id="data" required="" data-error="Completati data">
</div>
<div class="col-md-6 form-group">
<label for="specializare">Specializare:</label>
<select id="specializare" class="form-control" name="specializare" required="" data-error="Selectati o optiune">
<option value="" selected="" disabled="">Selecteaza</option>
<?php if( have_rows('specializare') ): ?>
<?php while( have_rows('specializare') ): the_row();
$medic = get_sub_field('medic');
?>
<option value="<?php echo $medic;?>"><?php echo $medic;?></option>
<?php endwhile; ?>
<?php endif; ?>
</select>
</div>
</div>
</fieldset>
<fieldset>
<div class="row">
<div class="col-md-12">
<legend><span class="number">3</span>Medic</legend>
<div class="form-group">
<select id="medic" name="medic" class="form-control" required="" data-error="Selectati medic">
<option value="" selected="" disabled="">Selecteaza medic</option>
<?php if( have_rows('formular') ): ?>
<?php while( have_rows('formular') ): the_row();
$medic = get_sub_field('medic');
?>
<option value="<?php echo $medic;?>"><?php echo $medic;?></option>
<?php endwhile; ?>
<?php endif; ?>
</select>
</div>
<div class="form-group" id="medicf" style="display:none">
<select id="medic" name="medic" class="form-control" required="" data-error="Alege medicul">
<option value="" selected="" disabled="">Alege medicul</option>
</select>
</div>
<div class="help-block with-errors"></div>
</div>
</div>
</fieldset>
<div class="row m-t15">
<div class="col-md-12">
<label class="gdpr">
<input type="checkbox" name="agree" id="agree" required="" data-error="Bifeaza">
Am citit si sunt de acord cu Termeni si Conditii si Politica GDPR </label>
<button type="submit" name="submit" id="submit" class="font-weight-500 text-uppercase btn disabled" style=" border-radius: 8px; background: #5CDB94; pointer-events: all; cursor: pointer;">Trimite solicitarea</button>
<div class="clearfix"></div>
</div>
</div>
</form>
Very simple! First prevent default action of submit button then validate and finally trigger submit.
Use this after the end tag
<script>
$(document).ready(()=>{
$("#contactForm").submit(function (e) {
//e.preventDefault(); //uncomment if form still submits
//e.stopImmediatePropagation(); //uncomment if form still submits
//now validate fields
if($('#nume').val().lenght < 3){
$('#nume').next().text('name is too short');
return false;
}else if($('#email').val().length < 5 && !$('#email').val().includes('#')){
$('#email').next().text('invalid email');
return false;
}
//else if....
else{
//$("#contactForm").submit();
//or
return true;
}
});
});
</script>
Note : This is untested code.
I am very new to php and I need help.
I have developed a php contact page but I have problems with validation when there is a field blank. When I test my validation it opens the php page and displays the message instead of showing an alert .Please see picture below
Validation problem
Here is my code:
HTML:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Exam Portal </title>
<!--META-->
<meta name="viewport" content="width=device-width initial-scale=1.0">
<!--FONT-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.0/css/font-awesome.min.css">
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300,700,500' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Merriweather:400,300' rel='stylesheet' type='text/css'>
<!--CSS-->
<link rel="stylesheet" type="text/css" href="css/grid.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/responsive.css">
<link rel="stylesheet" type="text/css" href="css/animate.css">
</head>
<body>
<div class="se-pre-con">
<div class="cssload-container">
<div class="cssload-loading"><i></i><i></i><i></i><i></i></div>
</div>
</div>
<header style="background-color:black" id="top">
<div class="container">
<div class="header-bottom">
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-5 col-xs-12">
<div class="logo">
<a href="#banner" data-scroll><img src="img/logo.png" alt="" /></a>
</div>
</div>
<div class="col-lg-8 col-md-8 col-sm-7 col-xs-12">
<nav class="nav-collapse">
<ul>
<li class="menu-item"><a href="index.html" data-scroll>HOME</a></li>
<li class="menu-item"><a href="aboutus.html" data-scroll>ABOUT US</a></li>
<li class="menu-item"><a href="howdoesitwork.html" data-scroll>HOW DOES IT WORK</a></li>
<li class="menu-item"><a href="whousesexamportal.html" data-scroll>WHO USES EP</a></li>
<li class="menu-item"><a href="gallery.html" data-scroll>GALLERY</a></li>
<li class="menu-item"><a href="news.html" data-scroll>NEWS</a></li>
<li class="menu-item active"><a href="contactus.html" class="btnstart" data-scroll>CONTACT US </a></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
</header> <br><br><br><br><br><br>
<section id="contact2">
<div id="contact-us-map">
<iframe width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://www.google.com/maps/embed?pb=!1m23!1m12!1m3!1d114610.04099879299!2d28.110395278010596!3d-26.145762465850492!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!4m8!3e6!4m0!4m5!1s0x1e9513bfa79eae3f%3A0xabf626720595f367!2shighway+gardens+office+park!3m2!1d-26.1457794!2d28.1804356!5e0!3m2!1sen!2sza!4v1497301543030"></iframe>
<br />
<small>
</small>
</iframe>
</div>
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="contact_area">
<div class="client_title">
<hr>
<h2>We Would Love To Hear From You</h2>
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="contact_left wow fadeInLeft">
<form method="post" class="submitphoto_form" action="contact.php" id="contactpage">
<input type="text" class="form-control wpcf7-text" placeholder="Your name" name="name" id="name">
<input type="mail" class="form-control wpcf7-email" placeholder="Email address" name="email" id="email">
<input type="text" class="form-control wpcf7-text" placeholder="Subject" name="subject" id="subject">
<textarea style="height:300px" class="form-control wpcf7-text" placeholder="What would you like to tell us" name="message" id="message"></textarea>
<input type="submit" value="Submit" class="wpcf7-submit photo-submit" name="send" id="send">
</form>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="contact_right wow fadeInRight">
<img src="img/phone_icon.png" alt="img">
<p>Say hello! </p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<footer>
<div class="container">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="footersocial">
<i class="fa fa-twitter" aria-hidden="true"></i>
<i class="fa fa-facebook" aria-hidden="true" ></i>
<i class="fa fa-instagram" aria-hidden="true"></i>
<i class="fa fa-dribbble" aria-hidden="true"></i>
<i class="fa fa-gg-circle" aria-hidden="true"></i>
</div>
</div>
</div>
</div>
</footer>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="js/fixed-responsive-nav.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<script type="text/javascript" src="js/waypoints.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
//$('#contactpage').validate();
$('#contactpage').submit(function(){
var action = $(this).attr('action');
$.ajax({
type: "POST",
url: action,
data: {
name: $('#name').val(),
email: $('#email').val(),
subject: $('#subject').val(),
message: $('#message').val()
},
dataType: "json",
cache: false,
success: function (data) {
if(data.type == 'success') {
alert(data.text);
}
else {
alert(data.text);
}
}
});
/*$.post(action, {
name: $('#name').val(),
email: $('#email').val(),
subject: $('#subject').val(),
message: $('#message').val()
},
function(data){
if(data.type == 'success') {
alert("Message sent successfully.");
}
else {
alert("An error occured.");
}
}
);*/
return false;
});
});
</script>
</body>
</html>
PHP:
<?php
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'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if(empty($name)) {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Please Enter Your Name.'
));
die($output);
} else if(trim($email) == '') {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'You Have Enter An Invalid E-mail Address, Try Again.'
));
die($output);
}
else if(!isEmail($email)) {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'You Have Enter An Invalid E-mail Address, Try Again.'
));
die($output);
}
else if(empty($subject)) {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Please Enter Your Mail Subject.'
));
die($output);
}
else if(empty($message)) {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Please Enter Your Message.'
));
die($output);
}
else {
$fromMail = $email;
$toMail = "myemail#gmail.com";
$boundary = str_replace(" ", "", date('l jS \of F Y h i s A'));
$subjectMail = "Contact Form From $name | Examportal";
$body = "Name : $name <br/>
Email: $email <br/>
Message Title : $subject <br/>
Message : \n $message
";
$contentHtml = '
<div style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333;">
<div style="color:#565656;">'.$body.'</div>
</div>
<div style="clear:both"></div>
';
$headersMail = '';
$headersMail .= 'From: ' ."noreply#emailexample.co.za\r\n" . 'Reply-To: ' . $fromMail . "\r\n";
$headersMail .= 'Return-Path: ' . $fromMail . "\r\n";
$headersMail .= 'MIME-Version: 1.0' . "\r\n";
$headersMail .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
try {
if (mail($toMail, $subjectMail, $contentHtml, $headersMail)) {
$output = json_encode(array( //create JSON data
'type'=>'success',
'text' => 'Thank you for contacting us, we will get back to you real soon. :).'
));
die($output);
} else {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'An unknown error occuredx. Please try again later.'
));
die($output);
}
} catch(Exception $e) {
$msg = $e->getMessage();
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Error occur with message : '.$msg
));
die($output);
}
}
?>
Simplest and easiest way to validate any blank field is by adding required in your input HTML as below.
<form method="post" class="submitphoto_form" action="contact.php" id="contactpage">
<input type="text" class="form-control wpcf7-text" placeholder="Your name" name="name" id="name" required>
<input type="email" class="form-control wpcf7-email" placeholder="Email address" name="email" id="email" required>
<input type="text" class="form-control wpcf7-text" placeholder="Subject" name="subject" id="subject" required>
<textarea style="height:300px" class="form-control wpcf7-text" placeholder="What would you like to tell us" name="message" id="message" required></textarea>
<input type="submit" value="Submit" class="wpcf7-submit photo-submit" name="send" id="send">
</form>
Also note that your input email type should be "email" and not "mail".
Drawback of using PHP validation is that the user will have to re-enter all the fields.
Drawback of using JS validation is that if the user has disabled JS, then the validation will not work.
Benefit of using simple HTML validation is that it will work on all the browsers and will stop the form submission in case of any empty field and/or invalid email format.
With this approach, you are not required to validate using JS. It is pure HTML.
Hope this helps.
i have a user login page and php script to check the login credentials with data stored in a database. Everything works fine, i just tried adding a msg box styled with CSS containing the text "wrong email" if login details didn't match the data in the database and it will automatically return to login page.
Unfortunately, the msg is not being echoed and it is returning to login page..
my php code goes like this..
<style>
.alert {
padding: 20px;
background-color: #f44336;
color: white;
}
.closebtn {
margin-left: 15px;
color: white;
font-weight: bold;
float: right;
font-size: 22px;
line-height: 20px;
cursor: pointer;
transition: 0.3s;
}
.closebtn:hover {
color: black;
}
</style
<?php
//15 2 2015
session_start();
$Cser = mysqli_connect("localhost","root","*****","database") or die("Server connection failed : ".mysqli_error($Cser));
$email = $_REQUEST["email"];
$password = $_REQUEST["password"];
$s = "select * from fuser where email='".$email."' and password = '".$password."'";
$result = mysqli_query($Cser,$s);
$count = mysqli_num_rows($result);
if($count>0)
{
$_SESSION["username"] = $username;
$_SESSION["login"]="1";
header("location:../home.php");
}
else
{
echo '<div class="alert">';
echo '<span class="closebtn" onclick="this.parentElement.style.display="none";">×</span>';
echo '<strong>Danger!</strong> wrong email id';
echo '</div>';
header("location:sign.php");
}
?>
My sign.php code as follows::
<!DOCTYPE html>
<html >
<?php
$servername = "localhost";
$username = "root";
$password = "*******";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
?>
<head>
<meta charset="UTF-8">
<title>title</title>
<script src="http://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script>
<link href='http://fonts.googleapis.com/css?family=PT+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/148866/reset.css">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header role="banner">
<nav class="main-nav">
<ul>
<!-- inser more links here -->
<li><a class="cd-signin" href="#0">Sign In</a></li>
<li><a class="cd-signup" href="#0">Sign Up</a></li>
</ul>
</nav>
</header>
<label class="thought">
<p> find your passion, <br><br> make it as your profession </p>
</label>
<div class="cd-user-modal"> <!-- this is the entire modal form, including the background -->
<div class="cd-user-modal-container"> <!-- this is the container wrapper -->
<ul class="cd-switcher">
<li>Sign In</li>
<li>Sign Up</li>
</ul>
<div id="cd-login"> <!-- log in form -->
<form class="cd-form" action="logincheck.php" method="post">
<p class="fieldset">
<label class="image-replace cd-email" for="signin-email">E-mail</label>
<input class="full-width has-padding has-border" name="email" id="signin-email" type="text" placeholder="E-mail" required autocomplete="off">
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<label class="image-replace cd-password" for="signin-password">Password</label>
<input class="full-width has-padding has-border" name="password" id="signin-password" type="text" placeholder="Password" required autocomplete="off">
Hide
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<input type="checkbox" id="remember-me">
<label for="remember-me">Remember me</label>
</p>
<button class="button button-block" type="submit" > Sign In </button>
</form>
<p class="cd-form-bottom-message">Forgot your password?</p>
<!-- Close -->
</div> <!-- cd-login -->
<div id="cd-signup"> <!-- sign up form -->
<form class="cd-form" action="register.php" method="post" >
<p class="fieldset">
<label class="image-replace cd-username" for="signup-username">Profile Name</label>
<input class="full-width has-padding has-border" name="name" id="signup-username" type="text" placeholder="Type your Name" required autocomplete="off">
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<label class="image-replace cd-email" for="signup-email">E-mail</label>
<input class="full-width has-padding has-border" name="email" id="email" type="text" placeholder="example#mail.com"required autocomplete="off" onkeyup="checkemail();">
<span class="cd-error-message" id="result"></span>
</p>
<label id="title" class="dropdown"><font face="verdana"> Select any three streams that your skills suits the best: </font><br><br>
<select id="job" name="category1" required>
<option value="Engineering"><font face="verdana">Engineering</font></option>
<option value="Medical"><font face="verdana">Medical</font></option>
<option value="Programming"><font face="verdana">Programming</font></option>
<option value="Business"><font face="verdana">Business</font></option>
<option value="Admin"><font face="verdana">Admin</font></option>
<option value="Other"><font face="verdana">Other</font></option>
</select>
<select id="job" name="category2" required>
<option value="Engineering"><font face="verdana">Engineering</font></option>
<option value="Medical"><font face="verdana">Medical</font></option>
<option value="Programming"><font face="verdana">Programming</font></option>
<option value="Business"><font face="verdana">Business</font></option>
<option value="Admin"><font face="verdana">Admin</font></option>
<option value="Other"><font face="verdana">Other</font></option>
</select>
<select id="job" name="category3" required>
<option value="Engineering"><font face="verdana">Engineering</font></option>
<option value="Medical"><font face="verdana">Medical</font></option>
<option value="Programming"><font face="verdana">Programming</font></option>
<option value="Business"><font face="verdana">Business</font></option>
<option value="Admin"><font face="verdana">Admin</font></option>
<option value="Other"><font face="verdana">Other</font></option>
</select>
</label>
<p class="fieldset">
<label class="image-replace cd-password" for="signup-password">Password</label>
<input class="full-width has-padding has-border" name="password" id="signup-password" type="text" placeholder="Password" required autocomplete="off">
Hide
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<input type="checkbox" id="accept-terms" required>
<label for="accept-terms">I agree to the Terms</label>
</p>
<button class="button button-block" type="submit" > Create an Account </button>
</form>
<!-- Close -->
</div> <!-- cd-signup -->
<div id="cd-reset-password"> <!-- reset password form -->
<p class="cd-form-message">Lost your password? Please enter your email address. You will receive a link to create a new password.</p>
<form class="cd-form">
<p class="fieldset">
<label class="image-replace cd-email" for="reset-email">E-mail</label>
<input class="full-width has-padding has-border" id="reset-email" type="email" placeholder="E-mail" autocomplete="off" required>
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<input class="full-width has-padding" type="submit" value="Reset password">
</p>
</form>
<p class="cd-form-bottom-message">Back to log-in</p>
</div> <!-- cd-reset-password -->
Close
</div> <!-- cd-user-modal-container -->
</div> <!-- cd-user-modal -->
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
Any help is greatly appreciated..
Remove the redirection code from the else condition, PHP will redirect without echoing/displaying anything on-screen.
... else
{
echo '<div class="alert">';
echo '<span class="closebtn" onclick="this.parentElement.style.display="none";">×</span>';
echo '<strong>Danger!</strong> wrong email id';
echo '</div>';
}
To keep your code you need to remove the header() redirect and either put a link on the bottom of the error message or use a META tag for redirect.
The code below will redirect after 10 seconds, so the user can read your error message:
... else
{
echo '<div class="alert">';
echo '<span class="closebtn" onclick="this.parentElement.style.display="none";">×</span>';
echo '<strong>Danger!</strong> wrong email id';
echo '</div>';
echo '<meta http-equiv="refresh" content="10;url=sign.php">';
}
or, with a link
... else
{
echo '<div class="alert">';
echo '<span class="closebtn" onclick="this.parentElement.style.display="none";">×</span>';
echo '<strong>Danger!</strong> wrong email id';
echo '</div>';
echo 'go back to login page';
}
I am totally stumped by this one.
My PHP form works just fine when within a folder on my server
see it here
http://getnice.co.uk/formTest/
but the same code at root level does not work.
Yes everything is pointing to the right places, so it is not that.
http://getnice.co.uk/form.html
Below is the code..
However I think it is a server issue? correct me if I am wrong.
<!-- Contact start -->
<section id="contact" class="pfblock pfblock-gray">
<div class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<div class="pfblock-header">
<h2 class="pfblock-title">Contact us</h2>
<div class="pfblock-line"></div>
<div class="pfblock-subtitle">
Tell us about your business and what your needs are:
</div>
</div>
</div>
</div><!-- .row -->
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<form id="contact-form" role="form" method="post" action="assets/php/contact.php">
<!-- <div class="contact.php"> -->
<div class="form-group wow fadeInUp">
<label class="sr-only" for="c_name">Name</label>
<input type="text" id="c_name" class="form-control" name="c_name" placeholder="Name">
</div>
<div class="form-group wow fadeInUp" data-wow-delay=".1s">
<label class="sr-only" for="c_email">Email</label>
<input type="email" id="c_email" class="form-control" name="c_email" placeholder="E-mail">
</div>
<div class="form-group wow fadeInUp" data-wow-delay=".1s">
<label class="sr-only" for="c_design">Design you like</label>
<select id="c_design" class="form-control" name="c_design" placeholder="Name of the Design you like">
<option value="" disabled selected>Select the design you like</option>
<option value="Office">Office Based Business</option>
<option value="Cafe or shop">Cafe or Shop</option>
<option value="Portfolio">Portfolio style</option>
<option value="Other small">Other Small Business</option>
<option value="Music">Musician Band style</option>
<option value="Agency">Agency style</option>
<option value="custom">Custom design required</option>
</select>
</div>
<div class="form-group wow fadeInUp" data-wow-delay=".2s">
<textarea class="form-control" id="c_message" name="c_message" rows="7" placeholder="Message"></textarea>
</div>
<button type="submit" class="btn btn-lg btn-block wow fadeInUp" data-wow-delay=".3s">Send Message</button>
</div>
<div class="ajax-response"></div>
</form>
</div><!-- .row -->
</div><!-- .container -->
</section>
<!-- Contact end -->
<!-- Javascript files -->
<script src="assets/js/jquery-1.11.1.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/js/jquery.parallax-1.1.3.js"></script>
<script src="assets/js/imagesloaded.pkgd.js"></script>
<script src="assets/js/jquery.sticky.js"></script>
<script src="assets/js/smoothscroll.js"></script>
<script src="assets/js/wow.min.js"></script>
<script src="assets/js/waypoints.min.js"></script>
<script src="assets/js/jquery.cbpQTRotator.js"></script>
<script src="assets/js/custom.js"></script>
PHP
<?php
if(isset($_POST['c_message'])){
$name = $_POST['c_name'];
$email = $_POST['c_email'];
$varDropdown = $_POST['c_design'];
$message = $_POST['c_message'];
$to = 'me#gmail.com';
$subject = 'Get Nice Contact Form';
$mailcontent2="<html>
<head>
</head>
<body>
<table width='800' border='0' cellspacing='2' cellpadding='0'>
<tr>
<td align='left'>
<p>Hi,</p>
<strong style='color:#C59800'>New request has been sent with the below details.</strong><br /><br />
<strong>Name: </strong>".stripslashes($_REQUEST['c_name'])."<br />
<strong>Email: </strong>".stripslashes($_REQUEST['c_email'])."<br />
<strong>Design: </strong>".stripslashes($_REQUEST['c_design'])."<br />
<strong>Message: </strong>".nl2br(stripslashes($_REQUEST['c_message']))."<br /><br />
<p>Thanks</p>
</td>
</tr>
</table>
</body>
</html>
";
//echo $mailcontent2;
$headers2 = "MIME-Version: 1.0" . "\r\n";
$headers2 .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers2 .= "From: ".$email." <".$email.">" . "\r\n";
$status = mail($to,$subject,$mailcontent2,$headers2);
if($status == TRUE){
$res['sendstatus'] = 'done';
//Edit your message here
$res['message'] = 'Thank you your mail has been sent';
}
else{
$res['message'] = 'Failed to send mail. Please email me at hello#getnice.co.uk';
}
echo json_encode($res);
}
JS
/* ---------------------------------------------- /*
* E-mail validation
/* ---------------------------------------------- */
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};
/* ---------------------------------------------- /*
* Contact form ajax
/* ---------------------------------------------- */
$('#contact-form').submit(function(e) {
e.preventDefault();
var c_name = $('#c_name').val();
var c_email = $('#c_email').val();
var c_message = $('#c_message ').val();
var c_design = $('#c_design ').val();
var response = $('#contact-form .ajax-response');
var formData = {
'c_name' : c_name,
'c_email' : c_email,
'c_design' : c_design,
'c_message' : c_message
};
if (( c_name== '' || c_email == '' || c_message == '') || (!isValidEmailAddress(c_email) )) {
response.fadeIn(500);
response.html('<i class="fa fa-warning"></i> Please fix the errors and try again.');
}
else {
$.ajax({
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'assets/php/contact.php', // the url where we want to POST
data : formData, // our data object
dataType : 'json', // what type of data do we expect back from the server
encode : true,
success : function(res){
var ret = $.parseJSON(JSON.stringify(res));
response.html(ret.message).fadeIn(500);
}
});
}
return false;
});
});
})(jQuery);
move the e.preventDefault(); towards the end in custom.js
I got a template online... They included this contact.php - And how do I add extra fields so this contact.php will send it to me.
<?php
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."#"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');
$your_email = "myemail#live.ca";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
if(#mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
?>
The HTML contact page is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function(){
$('#contactform').submit(function(){
var action = $(this).attr('action');
$.post(action, {
name: $('#name').val(),
email: $('#email').val(),
telephone: $('#telephone').val(),
company: $('#company').val(),
subject: $('#subject').val(),
message: $('#message').val()
},
function(data){
$('#contactform #submit').attr('disabled','');
$('.response').remove();
$('#contactform').before('<p class="response">'+data+'</p>');
$('.response').slideDown();
if(data=='Message sent!') $('#contactform').slideUp();
}
);
return false;
});
});
// ]]>
</script>
</head>
<body>
<div class="main">
<div class="blok_header">
<div class="header">
<div class="search">
</div>
<div class="clr"></div>
<div class="withe_bg">
<div class="logo"></div>
<div class="menu"> </div>
<div class="clr"></div>
<div class="body">
<h3> </h3>
<p> </p>
<div class="left">
<h2>Send us a Message:</h2>
<p>Please use this convenient form to your send your message, and we will get back to you shortly.</p>
<form action="contact.php" method="post" id="contactform">
<ol>
<li>
<label for="name">Name <span class="red">*</span></label>
<input id="name" name="name" class="text" />
</li>
<li>
<label for="telephone">Telephone <span class="red"></span></label>
<input id="telephone" name="telephone" class="text" />
</li>
<li>
<label for="email">Your email <span class="red">*</span></label>
<input id="email" name="email" class="text" />
</li>
<li>
<label for="company">Company</label>
<input id="company" name="company" class="text" />
</li>
<li>
<label for="subject">Subject</label>
<input id="subject" name="subject" class="text" />
</li>
<li>
<label for="message">Message <span class="red">*</span></label>
<textarea id="message" name="message" rows="6" cols="50"></textarea>
</li>
<li class="buttons">
<input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
<div class="clr"></div>
</li>
</ol>
</form>
</div>
<div class="right last">
<p> </p>
<p>
</p>
</div>
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
</div>
<div class="clr"></div>
</div>
<div class="FBG">
<div class="FBG_resize">
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
</div>
<div class="clr"></div>
</div>
<div class="FBG">
<div class="FBG_resize">
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
<div class="footer">
<div class="footer_resize">
<p class="leftt"> </p>
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
</body>
</html>
And still does not work.....
this isn't the form itself, it's the form processor. you probably have an HTML file somewhere with the form, posting to this contact.php file.
anyway, it looks like you have to match the # of fields you're requesting in the form to the # of items (with the correct names) in the $values array. It also looks like you can require certain ones in the $required array.
change the $your_email to your email address.
you should get the emails through the form.
does that answer your question? It wasn't terribly specific.
The fix is:
edit the: $required = array('name','email','telephone','message');
And it works... 100% - thanks all...
If you want it to email to multiple people, you should take a look at the parameters section (the To section)here. This will go in your
$your_email = "myemail#live.ca";
Here's an example:
$your_email = "<user#example.com>, Another User <anotheruser#example.com>";
That should send it to user#example.com and anotheruser#example.com
You mean you want the contact mail to be sent to you also,right? if so try the following code. mail is the php mail function which done the trick. For more reference you can check the MANUAL.
<?php
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."#"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');
$your_email = "seanbridge#live.ca";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
if(#mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
$myEmail = "myemai#your.com";//put your email address here
if(#mail($myEmail,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
?>