I have a form which I created using PHP (something I do no understand nearly as much as i’d like) – I made the form using an online tutorial a while back, cannot quite remember where from and can’t find it again, however the form works as it should so i’m content.
The only thing is, I need to add checkboxes to my form, they do not need to validate (if nothing is checked, the form can still be sent, if they are all checked the form can still be sent..)
What I do need to happen is for the e-mail that is sent back to me, to let me know which checkboxes were ticked.
This is my HTML/PHP page:
<!--Contact Form Section -->
<div id="contact-form" class="clearfix">
<?php
//init variables
$cf = array();
$sr = false;
if(isset($_SESSION['cf_returndata'])){
$cf = $_SESSION['cf_returndata'];
$sr = true;
}
?>
<form method="post" action="process.php">
<input type="email" id="email" name="email" placeholder="Your e-mail" value=" <?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>" required="required" />
<input type="text" id="name" name="name" placeholder="Your name" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>" required="required" />
<textarea id="message" name="message" placeholder="Your message..." required="required" data-minlength="20"><?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['message'] : '' ?></textarea>
<span id="loading"></span>
<input type="submit" value=" " id="submit-button" />
<ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">
<li id="info">There is a problem:</li>
<?php
if(isset($cf['errors']) && count($cf['errors']) > 0) :
foreach($cf['errors'] as $error) :
?>
<li><?php echo $error ?></li>
<?php
endforeach;
endif;
?>
</ul>
<p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks for your message!</p>
</div>
</form>
<?php unset($_SESSION['cf_returndata']); ?>
<!--End Contact Form Section -->
And here is my process PHP document:
<?php
if( isset($_POST) ){
//form validation vars
$formok = true;
$errors = array();
//submission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');
//form data
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];
$message = $_POST['message'];
//validate form data
//validate name is not empty
if(empty($name)){
$formok = false;
$errors[] = "You have not entered a name";
}
//validate email address is not empty
if(empty($email)){
$formok = false;
$errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$formok = false;
$errors[] = "You have not entered a valid email address";
}
//validate message is not empty
if(empty($message)){
$formok = false;
$errors[] = "You have not entered a message";
}
//validate message is greater than 20 characters
elseif(strlen($message) < 20){
$formok = false;
$errors[] = "Your message must be greater than 20 characters";
}
//send email if all is ok
if($formok){
$headers = "From: Goldie Locks online contact form" . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$emailbody = "<p>You have received a new message from the enquiries form on your website.</p>
<p><strong>Name: </strong> {$name} </p>
<p><strong>Email Address: </strong> {$email} </p>
<p><strong>Message: </strong> {$message} </p>
<p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";
mail("MY EMAIL!","New Enquiry",$emailbody,$headers);
}
//what we need to return back to our form
$returndata = array(
'posted_form_data' => array(
'name' => $name,
'email' => $email,
'telephone' => $telephone,
'enquiry' => $enquiry,
'message' => $message
),
'form_ok' => $formok,
'errors' => $errors
);
//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
//set session variables
session_start();
$_SESSION['cf_returndata'] = $returndata;
//redirect back to form
header('location: ' . $_SERVER['HTTP_REFERER']);
}
}
As you can see from looking at the html code, I took out some of the functions of the PHP but left them in the PHP process (such as enquiry type and telephone number) because I Was completely unsure what would break what.
All I need to do is add check boxes to the html/php document (5 to be precise) and have the ones that are ticked show up in the e-mail I receive when some one fills out the page.
Hopefully this is an easy thing for someone with PHP knowledge, but sadly I have very very little. Hope some one can help, if you need any more info please comment and i’ll try my best.
Thank you
you can add checkboxes to the form by simply adding a checkbox input element:
set the
value=""to whatever you want. You can then pick up thevaluein your PHP script using:The variable
$myCheckboxwill then contain the value of the checkbox with name ‘myCheckbox’ in the form if it was checked, or it’ll be blank if the checkbox was not checked.You can then add the value to the email as you have with the other variables, i.e.
If you wanted to keep the checked / not checked status on the form if it’s submitted and fails validation for whatever reason, then you can add the checkbox value to the
$returndatavariable:This will send it back to the form and then you need to alter the checkbox to look for this value as the other elements do, and add a value of ‘checked=Checked”‘ to the checkbox if it was previously checked:
where the
truepart of the line$cf['posted_form_data']['myCheckbox]=="true"is thevalue=""value of the checkbox.I hope that makes sense.