Jump to content

Form Required Field


kcelsi
Go to solution Solved by ginerjm,

Recommended Posts

Hello,

I obtained this code for a php form including a Captcha that I have modified.  My question is, how do I make a field "not required", but if filled out, the entry will also be sent with the form?  I'm planning to add fields for fax, address, etc that won't be necessary, but helpful if entered.  Thank you.

 

Here is my code:

 

<?php
 
if(isset($_POST['submitted'])) {
 
require_once('captcha/recaptchalib.php');
 
$privatekey = "6Ldke-wSAAAAAPrYCfs4gR28Y10hEttom0ax-nKD";
 
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
 
 
if(trim($_POST['checking']) !== '') {
$blindError = true;
} else {
 
$authorName = (filter_var($_POST['formAuthor'], FILTER_SANITIZE_STRING));
if ($authorName == ""){
$authorError = true;
$hasError = true;
}else{
$formAuthor = $authorName;
};
 
 
$authorEmail = (filter_var($_POST['formEmail'], FILTER_SANITIZE_EMAIL));
if (!(filter_var($authorEmail, FILTER_VALIDATE_EMAIL))){
$emailError = true;
$hasError = true;
} else{
$formEmail = $authorEmail;
};
 
$msgSubject = (filter_var($_POST['formSubject'], FILTER_SANITIZE_STRING));
if ($msgSubject == ""){
$subjectError = true;
$hasError = true;
}else{
$formSubject = $msgSubject;
};
 
//Check to make sure content has been entered
$msgContent = (filter_var($_POST['formContent'], FILTER_SANITIZE_STRING));
if ($msgContent == ""){
$commentError = true;
$hasError = true;
}else{
$formContent = $msgContent;
};
 
if (($resp->is_valid) && (!isset($hasError))) {
$emailTo = 'kcelsi@littlechisel.com'; // here you must enter the email address you want the email sent to
$subject = 'Marketing Inquiry From: ' . $formAuthor . ' | ' . $formSubject; // This is how the subject of the email will look like
$body = "Email: $formEmail \n\nContent: $formContent  \n\n$formAuthor"; // This is the body of the email
$headers = 'From: <'.$formEmail.'>' . "\r\n" . 'Reply-To: ' . $formEmail . "\r\n" . 'Return-Path: ' . $formEmail; // Email headers
 
mail($emailTo, $subject, $body, $headers);
 
$emailSent = true;
}
 
if (!($resp->is_valid)){
$captchaErrorMsg = true;
}
}
} ?>
 
<?php // if the page the variable "email sent" is set to true show confirmation instead of the form ?>
<?php if(isset($emailSent) && $emailSent == true) { ?>
        <style type="text/css">
#wrapper {
width: 960px;
margin-right: auto;
margin-left: auto;
}
#content {
font-family: Verdana, Geneva, sans-serif;
text-align: center;
}
.thankyou {
font-size: 24px;
font-weight: bold;
}
.text {
margin-top: -5px;
padding-top: 0px;
font-size: 16px;
}
a {
color: #900;
text-decoration: none;
font-size: 14px;
}
</style>
</head>
 
<body>
<div id="wrapper">
  <div id="content">
    <p class="thankyou">Thank You</p>
    <p class="text">Your form has been submitted and someone from Ideal Window will contact you shortly.</p>
  <a href="http://www.idealwindow.com/">Back to Ideal Window Home Page</a> </div>
</div>
            
<?php } else { ?>
<?php // if there are errors in the form show a message ?>
<?php if(isset($hasError) || isset($blindError)) { ?>
<p>There was an error submitting the form. Please check all the marked fields.</p>
<?php } ?>
<?php // if there are recaptcha errors show a message ?>
<?php if ($captchaErrorMsg){ ?>
<p>Captcha error. Please, type the check-words again.</p>
<?php } ?>
<?php 
 
?>
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'clean'
};
</script>
<?php
 
?>
<form id="contactForm" action="" method="post">
<div id="singleParagraphInputs">
<div>
<label for="formAuthor">
Full Name
</label>
<input class="requiredField <?php if($authorError) { echo 'formError'; } ?>" type="text" name="formAuthor" id="formAuthor" value="<?php if(isset($_POST['formAuthor']))  echo $_POST['formAuthor'];?>" size="40" />
</div>
<div>
<label for="formEmail">
Email
</label>
<input class="requiredField <?php if($emailError) { echo 'formError'; } ?>" type="text" name="formEmail" id="formEmail" value="<?php if(isset($_POST['formEmail']))  echo $_POST['formEmail'];?>" size="40" />
</div>
<div>
<label for="formSubject">
Subject
</label>
<input class="requiredField <?php if($subjectError) { echo 'formError'; } ?>" type="text" name="formSubject" id="formSubject" value="<?php if(isset($_POST['formSubject']))  echo $_POST['formSubject'];?>" size="40" />
</div>
</div>
<div id="commentTxt">
<label for="formContent">
Message
</label>
<textarea class="requiredField <?php if($commentError) { echo 'formError'; } ?>" id="formContent" name="formContent" cols="40" rows="5"><?php if(isset($_POST['formContent'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['formContent']); } else { echo $_POST['formContent']; } } ?></textarea>
<?php 
 
?>
<div id="screenReader">
<label for="checking">
If you want to submit this form, do not enter anything in this field
</label>
<input type="text" name="checking" id="checking" value="<?php if(isset($_POST['checking']))  echo $_POST['checking'];?>" />
</div>
</div>
<?php
 
require_once('captcha/recaptchalib.php');
 
$publickey = "6Ldke-wSAAAAAE7yRt4cUb3G6hYU__6JIwsc6lwL";
 
echo recaptcha_get_html($publickey);
?>
<input type="hidden" name="submitted" id="submitted" value="true" />
<?php // submit button ?>
<input type="submit" value="Send Message" tabindex="5" id="submit" name="submit">
</form>
<?php } // yay! that's all folks! ?>
 
</script>
        <script src="js/custom.js" type="text/javascript">
</script>
 
 
CSS:
 
body{
font: 14px/1.5em sans-serif;
margin: 0;
padding: 0;
}
 
label {
    display: block;
    padding: 1px 7px 0;
    position: absolute;
    top: 0;
    z-index: 1;
}
 
input[type=text], textarea {
    display: block;
    background: none;
    font: inherit;
    padding: 0 7px;
    position: relative;
    z-index: 10;
overflow: auto; 
}
 
label, input[type=text], #singleParagraphInputs div{
line-height: 1.5em;
height: 1.5em;
}
 
#singleParagraphInputs div, #commentTxt, #recaptcha_widget_div{
margin-bottom: 1.5em;
}
 
input, textarea, #recaptcha_table{
border: 1px solid #ccc;
}
 
.formError {
    border: 1px solid red;
}
 
#contactForm, #recaptcha_table{
width: 600px;
margin: 0 auto;
margin-top: 50px;
}
 
#singleParagraphInputs div, #commentTxt {
    position: relative;
}
 
#singleParagraphInputs input {
    width: 584px; /* 16px less than form width
}
 
#commentTxt textarea {
    min-height: 9em;
min-width: 584px; /* 16px less than form width 
font: 14px/1.5em sans-seif;
}
 
#screenReader, #checking {
    float: left;
display: none;
}
 
#recaptcha_response_field {
    width: 286px !important; 
}
 
input[type=submit]{
}
 
javascript:
 
$(function(){
 
 
 
$formItems = $("input:text, textarea");
 
$formItems
// fires after the page has loaded
// if the field has already some value the label becomes invisible
.each(function(){
if ($(this).val() !== '') {
$(this).prev().css({
opacity: '0'
});
};
})
 
.focus(function(){ 
if ($(this).val() == '') {
$(this).prev().stop().animate({
opacity: '0'
}, 200);
}
})
 
.blur(function(){
if ($(this).val() == '') {
$(this).prev().stop().animate({
opacity: '1'
}, 200);
}
}) 
 
});

 

Link to comment
Share on other sites

Well it seems that your if statements all over the place.

 

Simple store the POST variable $field = $_POST['field_name']

 

As long as your not checking using a if statement like the code you have here:

$msgSubject = (filter_var($_POST['formSubject'], FILTER_SANITIZE_STRING));
if ($msgSubject == ""){
$subjectError = true;
$hasError = true;
}else{
$formSubject = $msgSubject;
};
Edited by GetFreaky
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.