How to add recaptcha to mailform - php

The website of the wife of a deceased friend has been misused for sending spam. This is probably because ReCaptcha is not used with the registration form. The site uses Joomla 3.9.
I would like to add ReCaptcha but I have no idea how I can do that.
I hope someone can help.
I have not tried anything myself because my knowledge of PHP is not good enough to get this done.
Here is the form:
<form id="form1" action="/script/mailforms/mailsend.php" enctype="multipart/form-data" method="post"><input name="redirect" type="hidden" value="/bedankt" />
<table border="0" width="200">
<tbody>
<tr>
<td> </td>
<td><strong>Gegevens ouder/verzorger</strong></td>
<td> </td>
<td> </td>
<td><strong>Gegevens kind</strong></td>
</tr>
<tr>
<td>Voornaam</td>
<td><span id="sprytextfield1"> <label for="Naam"></label> <input id="Naam" name="Naam" type="text" /> </span></td>
<td> </td>
<td>Naam kind</td>
<td><span id="sprytextfield9"> <label for="Naam Kind"></label> <input id="Naam Kind" name="Naam Kind" type="text" /> </span></td>
</tr>
<tr>
<td>Achternaam</td>
<td><span id="sprytextfield2"> <label for="Achternaam"></label> <input id="Achternaam" name="Achternaam" type="text" /> </span></td>
<td> </td>
<td>Geslacht</td>
<td><span id="spryradio1"> <label> <input id="Geslacht_0" name="Geslacht" type="radio" value="Jongen" /> jongen</label> <br /> <label> <input id="Geslacht_1" name="Geslacht" type="radio" value="Meisje" /> meisje</label> <br /> </span></td>
</tr>
<tr>
<td>Straat + nr</td>
<td><span id="sprytextfield3"> <label for="Straat en nr"></label> <input id="Straat en nr" name="Straat en nr" type="text" /> </span></td>
<td> </td>
<td>Geboortedatum</td>
<td><span id="sprytextfield10"> <label for="Geboortedatum"></label> <input id="Geboortedatum" name="Geboortedatum" type="text" /> </span></td>
</tr>
<tr>
<td>Postcode</td>
<td><span id="sprytextfield4"> <label for="Postcode"></label> <input id="Postcode" name="Postcode" type="text" /> </span></td>
<td> </td>
<td>Schoolnaam</td>
<td><span id="sprytextfield11"> <label for="Schoolnaam"></label> <input id="Schoolnaam" name="Schoolnaam" type="text" /> </span></td>
</tr>
<tr>
<td>Woonplaats</td>
<td><span id="sprytextfield5"> <label for="Woonplaats"></label> <input id="Woonplaats" name="Woonplaats" type="text" /> </span></td>
<td> </td>
<td>Voorkeur</td>
<td><span id="spryradio2"> <br /> <label> <input id="Voorkeur_1" name="Voorkeur" type="radio" value="Woensdag" /> Woensdagmiddag (Zonnestudio)</label> <br /> </span></td>
</tr>
<tr>
<td>Telefoonnr</td>
<td><span id="sprytextfield6"> <label for="Telefoonnr"></label> <input id="Telefoonnr" name="Telefoonnr" type="text" /> </span></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Email</td>
<td><span id="sprytextfield7"> <label for="Email"></label> <input id="Email" name="Email" type="text" /> </span></td>
<td> </td>
<td>Bijzonderheden</td>
<td><span id="sprytextarea1"> <label for="Bijzonderheden"></label> <textarea id="Bijzonderheden" cols="45" name="Bijzonderheden" rows="5"> </textarea> </span></td>
</tr>
<tr>
<td>Bank/gironr</td>
<td><span id="sprytextfield8"> <label for="Bankgiro"></label> <input id="Bankgiro" name="Bankgiro" type="text" /> </span></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan="4"><br /><span id="sprycheckbox1"> <input id="Algemene Voorwaarden" name="Algemene Voorwaarden" type="checkbox" /> </span>Ik heb de de <strong>algemene voorwaarden</strong> gelezen en ga hiermee akkoord</td>
</tr>
<tr>
<td> </td>
<td><input id="Verzenden" name="Verzenden" type="submit" value="Verzenden" /></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
</form>
Here is the mailsend.php:
<?php
require('class.mailer.info.php');
//phpinfo();
/* Vaste post waarden vastleggen */
// ontvanger
if(empty($_POST['redirect']))
{
die("error: geen ontvanger vastgelegd en redirect pad.");
}
// Instantiate your new class
$mail = new HeartmedicalMailer;
/*
Verwerken post informatie
*/
$body_text = '';
// waardes die je niet wilt tonen wegfilteren als redirect / receipient etc.
$excluded = array('redirect');
// html opmaak formulier voor loop
$body_text .= "<style>body{ font-family:Verdana, Arial; font-size:11px; color:#000000; }</style>";
// loop doorlopen met opmaak
foreach($_POST as $key => $value)
{
// vaste waardes wegfilteren
if(!in_array($key, $excluded))
{
if(!empty($value))
{
$key = preg_replace('#_#',' ', $key);
$body_text .= '<strong>'.$key.'</strong>: '.$value.'<br>';
}
}
}
// Now you only need to add the necessary stuff
$mail->Body = $body_text;
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
die("There was an error sending the message #code1");
}
// redirect
header("Location: ".$_POST['redirect']." ");
?>
Here is the class.mailer.info.php:
<?php
// class phpmailer
require("../phpmailer/class.phpmailer.php");
class HeartmedicalMailer extends PHPMailer
{
// Set default variables for all new objects
var $From = "xxxxx#xxxxx.nl";
var $FromName = "De Zwemschool";
var $Host = "localhost";
/* smtp auth */
var $Mailer = "smtp"; // Alternative to IsSMTP()
var $SMTPAuth = false; // turn on SMTP authentication
var $Username = ""; // SMTP username
var $Password = ""; // SMTP password
var $WordWrap = 75;
var $Subject = "Inschrijving De Zwemschool";
function HeartmedicalMailer()
{
$this->AddAddress('xxxx#xxxx.nl', 'Name');
$this->IsHTML(true);
}
// Replace the default error_handler
function error_handler($msg) {
print("Mailer Error");
print("Description:");
printf("%s", $msg);
exit;
}
}
?>
I would like the form to be sent with recaptcha

You could use Google reCAPTCHA.
I personally use the reCAPTACHA V2 invisible one so you'll find an example below.
First get the keys here: link
Then replace your submit input with this:
<button class="g-recaptcha form-control" data-sitekey="YOUR_RECAPTCHA_KEY" data-callback='onSubmit'>Send</button>
Then add these scripts to your HTML page:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmit(token) {
document.getElementById("form1").submit();
}
</script>
And finally add this at the begining of your mailsend.php:
function get_ip() {
// IP if shared network
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
return $_SERVER['HTTP_CLIENT_IP'];
}
// IP behind proxy
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
// Sinon : normal IP
else {
return (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');
}
}
// Anti spam using google Captcha
$post_params = [
'secret' => "YOUR",
'response' => $_POST['g-recaptcha-response'],
'remoteip' => get_ip()
];
# Create a connection
$url = 'https://www.google.com/recaptcha/api/siteverify';
$ch = curl_init($url);
# Form data string
$post_string = http_build_query($post_params, '', '&');
# Setting our options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Get the response
$response = curl_exec($ch);
$response = json_decode($response);
curl_close($ch);
if(!$response->success){
exit();
}
Hope it helps ;)

Related

How to send this with php mail function

I would like all additional input of benodigheden and ingrediënten are also sent. However, I do not know how you could pick up the cost of additional input. I hope somebody can help me.
This is my code
<div id="versturen">
<?php
if(isset ($_POST['Verzenden']))
{
$name = $_POST['Naam'];
$Email = $_POST['Email'];
$opmerking = $_POST ['Opmerking'];
$seizoen = $_POST ['Seizoen'];
/*-------------------------------------------------------------------------------------*/
/*---benodigheden---begin---*/
$benodigheden1 = $_POST ['benodigheden1'];
/*---benodigheden---eind---*/
/*---ingrediënten---begin---*/
$ingrediënten1 = $_POST ['ingrediënten1'];
/*---ingrediënten---eind---*/
$stappenplan = $_POST['stappenplan'];
/*-------------------------------------------------------------------------------------*/
$opmerking = $_POST ['Opmerking'];
/*-------------------------------------------------------------------------------------*/
$subject = 'GERECHT INDIENEN' . $seizoen . '' ;
$to = 'wross#live.nl, wvanhees35#hotmail.com';
$gerecht = 'BENODIGHEDEN <br />' . $benodigheden1 . '<br /> NGREDIËNTEN <br />' . $ingrediënten1 . '<br /> STAPPENPLAN <br />'. $stappenplan . '<br />';
$message = ''.$gerecht . $opmerking . ' Dit gerecht is ingestuurd door ' . $name . ' en is ingediend via ' .$Email;
mail($to, $subject, $message, "From:" . $Email)
?>
<div id="testing"><br /><h2>Uw mail is verzonden</h2></div>
<?php
header("Refresh: 3; URL=../index.php");
}
else{
?>
<form action="" method="post">
<legend>Neem contact op</legend>
<table>
<tr>
<td>
<label for="Naam">Naam: </label>
</td>
<td>
<input type="text" id="Naam" name="Naam" placeholder="Naam" required="required" />
</td>
</tr>
<tr>
<td>
<label for="Email">Email :</label>
</td>
<td>
<input type="email" id="Email"name="Email" placeholder="Email" required="required" />
</td>
</tr>
<tr>
<td>
<label for="Seizoen">Seizoen: </label>
</td>
<td>
<select name="Seizoen" id="Seizoen" required>
<option value="">Kies hier je seizoen</option>
<option value="Lente">Lente</option>
<option value="Zomer">Zomer</option>
<option value="Herfst">Herfst</option>
<option value="Winter">Winter</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td>
<label for="benodigheden1">Benodigheden:</label>
</td>
<td>
<input type="text" id="benodigheden1"name="benodigheden1" placeholder="Benodigheden" required="required" />
</td>
</tr>
<tr>
<td>
<label for="ingrediënten1">Ingrediënten:</label>
</td>
<td>
<input type="text" id="ingrediënten1"name="ingrediënten1" placeholder="Ingrediënten" required="required" />
</td>
</tr>
<tr>
<td>
<label for="stappenplan">Stappenplanm:</label>
</td>
<td>
<textarea name="stappenplan" id="stappenplan" cols="40" rows="5" placeholder="Stappenplan" required="required" /></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td>
<label for="Opmerking">Opmerking:</label>
</td>
<td>
<textarea name="Opmerking" id="Opmerking" cols="40" rows="5" placeholder="Opmerking" required="required" /></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<div class="submit"><input type="submit" value="Verzenden" name="Verzenden" /></div>
</td>
</tr>
</table>
</form>
<?php
};
?>
</div>
Here is a link to JSFiddle.
He does not address here all my code (just as here).
Name the original and newly spawned input fields ingredienten[] and benodigheden[]
this will make them come in as a array in php.
foreach($_POST['benodigheden'] as $value){
echo $value .'<br />';
}
offcourse you need to change it to something usefull
I made a example see jsfiddle here place the above php somewhere and see wat happens if you submit
changed the html form input fields
<input type="text" id="benodigheden1"name="benodigheden[]" placeholder="Benodigheden" required="required" />
<input type="text" id="ingrediënten1"name="ingrediënten[]" placeholder="Ingrediënten" required="required" />
changed the javascript by adding this line " .attr('name',res[0]+'[]') "
// Add a line, and make it non-mandatory
$(this).clone()
.attr('id', newId).removeAttr('required')
.attr('name',res[0]+'[]')
.val('')
.insertAfter(this)
.before($('<br>'));

My PHP form works on my godaddy hosted website, but will not work on a yahoo hosted website, how can I correct this?

This form is for a website hosted on yahoo. I know it accepts php because I ran and test.php and it went through. The form goes nowhere after hitting submit on the yahoo website. I uploaded to another site and got an error message that says " Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/ecdo02/public_html/emag/validate.class.php on line 4". So I tried it on my own website hosted by godaddy and no errors, the form went through successfully and it showed up in my email. Does anyone have an idea what's going on? Its very strange.
This is my request.php
<?php
define("EMAIL", "xxxxxxx#xxxxx.com");
if(isset($_POST['submit'])) {
include('validate.class.php');
//assign post data to variables
$firstname = trim($_POST['firstname']);
$lastname = trim($_POST['lastname']);
$company = trim($_POST['company']);
$address = trim($_POST['address']);
$city = trim($_POST['city']);
$state = trim($_POST['state']);
$zip = trim($_POST['zip']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$fax = trim($_POST['fax']);
$title = trim($_POST['title']);
$interest = trim($_POST['interest']);
$comment = trim($_POST['comment']);
//start validating our form
$v = new validate();
$v->validateStr($firstname, "firstname", 3, 75);
$v->validateStr($lastname, "lastname", 3, 75);
$v->validateEmail($email, "email");
$v->validateStr($phone, "phone");
$v->validateStr($comment, "comment", 10, 500);
//use php's mail function to send the email
#mail($email_to, $subject ,$message ,$header );
if(!$v->hasErrors()) {
$header = "From: $email\n" . "Reply-To: $email\n";
$subject = "Lead from Website";
$email_to = EMAIL;
$message = "A message was submitted.
From: $title $firstname $lastname
Company: $company
Address: $address
City: $city
State/ Province/ Region: $state
Postal/ Zip Code: $zip
E-mail: $email
Phone: $phone
Fax: $fax
What would you like? $interest
Comments:
$comment
-End of message
";
//grab the current url, append ?sent=yes to it and then redirect to that url
$url = "http". ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
header('Location: '.$url."?sent=yes");
} else {
//set the number of errors message
$message_text = $v->errorNumMessage();
//store the errors list in a variable
$errors = $v->displayErrors();
//get the individual error messages
$firstnameErr = $v->getError("firstname");
$lastnameErr = $v->getError("lastname");
$phoneErr = $v->getError("phone");
$emailErr = $v->getError("email");
$commentErr = $v->getError("comment");
}//end error check
}
// end isset
?>
This is my validate.class.php
<?php
class validate {
public $errors = array();
public function validateStr($postVal, $postName, $min = 1, $max = 500) {
if(strlen($postVal) < intval($min)) {
$this->setError($postName, ucfirst($postName)." field is required.");
} else if(strlen($postVal) > intval($max)) {
$this->setError($postName, ucfirst($postName)." must be less than {$max} characters long.");
}
}// end validateStr
public function validateEmail($emailVal, $emailName) {
if(strlen($emailVal) <= 0) {
$this->setError($emailName, "Please enter an e-mail address");
} else if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[#][a-zA-Z0-9_]+([.][a- zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $emailVal)) {
$this->setError($emailName, "Please enter a valid e-mail address");
}
}// end validateEmail
/* Validate phone no to allow numbers only */
public function validatePhone($phoneVal, $phoneName) {
if (strlen($phoneVal) <= 0) {
$this->setError($phoneName, "Please enter your phone number");
} else if (preg_match('/[^0-9]/', $phoneVal)) {
$this->setError($phoneName, "Please enter a valid phone number");
}
} // end validatephone
/* sets an error message for a form element*/
private function setError($element, $comment) {
$this->errors[$element] = $comment;
}
/* returns the error of a single form element*/
public function getError($elementName) {
if($this->errors[$elementName]) {
return $this->errors[$elementName];
} else {
return false;
}
}
/* displays the errors as an html un-ordered list*/
public function displayErrors() {
$errorsList = "<ul class=\"errors\">\n";
foreach($this->errors as $value) {
$errorsList .= "<li>". $value . "</li>\n";
}
$errorsList .= "</ul>\n";
return $errorsList;
}
/* returns whether the form has errors*/
public function hasErrors() {
if(count($this->errors) > 0) {
return true;
} else {
return false;
}
}
/* returns a string stating how many errors there were*/
public function errorNumMessage() {
if(count($this->errors) > 1) {
$message = "There were " . count($this->errors) . " errors sending your message!\n";
} else {
$message = "There was an error sending your message!\n";
}
return $message;
}// end hasErrors
}// end class
?>
Here is the form
<form id="contact_form" method="post" action="request.php">
<fieldset>
<legend>Thank You</legend>
<div style="padding-left:100px">
<?php if(isset($_GET['sent'])): ?><table width="590" border="0">
<tr>
<td width="590" colspan="2"></td>
</tr>
<tr>
<td colspan="2">
<div style="padding:30px; text-align:center"><span style="font-size:24px; font-weight:bold; ">Thank You!<br /><br /><span style="font-family:Arial, Helvetica, sans-serif; font-size:15px; background:; padding:20px; width:300px;text-align:center">Your submission has been submitted.</span></div>
</td>
</tr>
</table></div><?php endif; ?>
</fieldset>
<fieldset>
<legend>Request</legend>
<div style="padding-left:100px">
<table width="590" border="0">
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td width="195"><label>What would you like? </label></td>
<td width="395"><select name="interest">
<option value=""> -- Please select -- </option>
<option>Add me to your mailing list.</option>
<option>Send me an information kit.</option>
<option>Call me to set up a meeting.</option>
</select></td>
</tr>
</table></div>
</fieldset>
<fieldset>
<legend>Personal Info</legend>
<div style="padding-left:100px;">
<table width="590" border="0" cellpadding="10" cellspacing="5">
<tr>
<td width="195" align="right"><label><b>Title</b></label></td>
<td width="395"><select name="title" style="width:183px">
<option value=""> - Select Your Title - </option>
<option>Mr.</option>
<option>Mrs.</option>
<option>Miss</option>
<option>Ms.</option>
<option>Dr.</option>
<option>Prof.</option>
<option>Rev.</option>
<option>Other</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td align="right"><label><b>First Name</b><span style="color: red"> *</span></label></td>
<td><input type="text" name="firstname" value="<?php echo htmlentities($firstname); ?>" /> <br /></td>
</tr>
<tr>
<td> </td>
<td><span class="errors"><?php echo $firstnameErr; ?></span></td>
</tr>
<tr>
<td align="right"><label><b>Last Name<span style="color: red"> *</span></b></label></td>
<td><input type="text" name="lastname" value="<?php echo htmlentities($lastname); ?>" /> <br /></td>
</tr>
<tr>
<td> </td>
<td><span class="errors"><?php echo $lastnameErr; ?></span></td>
</tr>
<tr>
<td><label><b>Company</b></label></td>
<td><input type="text" name="company" value="<?php echo htmlentities($company); ?>" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><label><b>Street Address</b><span style="color: red"> *</span> </label></td>
<td><input type="text" name="address" value="<?php echo htmlentities($address); ?>" /></td>
</tr>
<tr>
<td> </td>
<td><span class="errors"><?php echo $addressErr; ?></span></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><label><b>City</b><span style="color: red"> *</span></label> </td>
<td><input type="text" name="city" value="<?php echo htmlentities($city); ?>" /></td>
</tr>
<tr>
<td> </td>
<td><span class="errors"><?php echo $cityErr; ?></span></td>
</tr>
<tr>
<td><label><b>State</b><span style="color: red"> *</span></label></td>
<td><input type="text" name="state" value="<?php echo htmlentities($state); ?>" /> <br /></td>
</tr>
<tr>
<td> </td>
<td><span class="errors"><?php echo $stateErr; ?></span></td>
</tr>
<tr>
<td><label><b>Postal/Zip Code</b><span style="color: red"> *</span> </label></td>
<td><input type="text" name="zip" value="<?php echo htmlentities($zip); ?>" /> <br /></td>
</tr>
<tr>
<td> </td>
<td><span class="errors"><?php echo $zipErr; ?></span></td>
</tr>
</table></div>
</fieldset>
<fieldset>
<legend>Contact Info</legend>
<div style="padding-left:100px;">
<table width="590" border="0" cellpadding="10" cellspacing="5">
<tr>
<td><label><b>E-mail</b><span style="color: red"> *</span></label></td>
<td><input type="text" name="email" value="<?php echo htmlentities($email); ?>" /></td>
</tr>
<tr>
<td> </td>
<td><span class="errors"><?php echo $emailErr; ?></span></td>
</tr>
<tr>
<td><label><b>Phone</b><span style="color: red"> *</span></label></td>
<td><input type="text" name="phone" value="<?php echo htmlentities($phone); ?>" /></td>
</tr>
<tr>
<td> </td>
<td><span class="errors"><?php echo $phoneErr; ?></span></td>
</tr>
<tr>
<td width="195"><label><b>Fax</b></label></td>
<td width="395"><input type="text" name="fax" value="<?php echo htmlentities($fax); ?>" />
</td>
</tr>
</table></div>
</fieldset>
<fieldset>
<legend>Additional Notes</legend>
<div style="padding-left:100px;">
<table width="590" border="0" cellpadding="10">
<tr valign="top">
<td width="195"><label><b>Your Comment:</b></label></td>
<td width="395"><textarea name="comment" rows="10" cols="50" style="width: 300px;"/>
<?php echo htmlentities($comment); ?></textarea><br />
<span class="errors"><?php echo $commentErr ?></span><br /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="submit" name="submit" class="button" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</div>
</form>
Have check phpinfo() on both Yahoo and GoDaddy servers? I'll expect your Yahoo server runs with PHP 4.x while your GoDaddy server runs with PHP 5.x.

not receiving attachments using php pear package on linux based server

Following is my code for sending attachments.
Any help is useful.enter code here
Thanks in advance.
send_attach.php
<?php
#include_once 'Mail.php'
#include_once 'Mail/mime.php' ;
$hdrs['email'] = $mime;
$headers['To'] = $work_mail;
$headers['Subject'] = "Application For Job";
$headers['Content-type'] = "text/html; charset=windows-1251";
$headers['MIME-Version'] = "1.0";
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = '/studylink.txt';
$crlf = "\n";
$hdrs = array(
'From' => 'me#here.com',
'Subject' => 'Test mime message'
);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
$body = $mime->get();
$body=$mime->get(array('html_charset'=>'windows-1251','text_charset'=>'windows-1251','head_charset'=>'windows-1251'));
$hdrs = $mime->headers($hdrs);
#$mail =& Mail::factory('mail');
#$mail->#$mail->send('there#somewhere.com', $hdrs, $body);
?>
send_file.html
<form action="send_attach.php" method="post" enctype="multipart/form-data">
<table width="35%" border="0" align="center" cellpadding="0" cellspacing="6">
<tr class="style14">
<td width="16%">Name <span class="style8">*</span></td>
<td width="84%"><input type="text" placeholder="Name" name="name" id="name" for='name'></td>
</tr>
<tr class="style14">
<td>Email <span class="style8">*</span></td>
<td> <input type="text" name="email" id="email" for='email' placeholder="Email"> </td>
</tr>
<tr class="style14">
<td>Phone</td>
<td> <input name="phone" type="text" id="phone" for='phone' maxlength="10" placeholder="Numbers Only"></td>
</tr>
<tr class="style14">
<td valign="top">Employment <br />
Eligibility <span class="style8">*</span></td>
<td valign="middle">
<select name="work" class="style14" id="work">
<option selected="selected">--- select option ---</option>
<option>a</option>
<option>b</option>
<option>c`enter code here`</option>
</select> </td>
</tr>`enter code here`
<tr class="style14">
<td>Experience<br />
Summary <span class="style8">*</span></td>
<td valign="middle"><textarea name="summary" cols="35" rows="2" id="summary" placeholder="Experience Summary"></textarea></td>
</tr>
<tr>
<td valign="top" class="style14">Resume<span class="style8"> * </span></td>
<td><label>
<input name="attachment" type="file" for='resume_uploaded_file'>
</label></td>
</tr>
<tr>
<td> </td>
<td><input type="reset" name="Reset" id="button" value="Reset" />
<input type="submit" name="btn_get" id="btn_get" value="Submit" /></td>
</tr>
</table>
</form>
It doesn't look like you've uploaded the file to the filesystem in the example you provided. You need to process the upload from the form POST, put the file on the filesystem, and then make sure you are referencing it correctly when you add it. /file looks at the root of the server, not of the web server where your website is.
If you do a file_exists('/studylink.txt')
...My guess is the file does not exist.

Integrating validation with Recaptcha (or required fields)

I have a form below that I want to make certain fields required based on different things.
I want the following fields to be a straight forward required field with a popup alert if they're not filled in:
Name
Email
I want the phone field to be required if phone is ticked.
Address to be required if post is ticked.
How can I make the alert's pop up if things are missing but if everything is ok then send the form?
<div id="contactform">
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'white'
};
</script>
<form class="form" method="POST" action="<?php the_permalink(); ?>" onsubmit="return validateCaptcha()">
<input type="hidden" name="valid" value="0" />
<table border="0" style="float:left;" width="490">
<tbody>
<tr>
<td>
<p>Name:</p>
</td>
<td> </td>
<td colspan="2"><input type="text" name="fullname" id="fullname" /></td>
</tr>
<tr>
<td>
<p>Organisation:</p>
</td>
<td> </td>
<td colspan="2"><input type="text" name="companyname" id="companyname" /></td>
</tr>
<tr>
<td>
<p>E-mail:</p>
</td>
<td> </td>
<td colspan="2"><input type="text" name="email" id="email" /></td>
</tr>
<tr>
<td>
<p>Daytime Tel:</p>
</td>
<td> </td>
<td colspan="2"><input type="text" name="tel" id="tel" /></td>
</tr>
<tr>
<td valign="top">
<p>Contact Method:</p>
</td>
<td> </td>
<td align="left" valign="top">
<p>
<input style="width:20px!IMPORTANT;" type="checkbox" name="CheckboxGroup1[]" value="Phone" id="phone" />
<label style="margin-right: 25px;">Phone</label>
</p>
<p>
<input style="width:20px!IMPORTANT;" type="checkbox" name="CheckboxGroup1[]" value="Email" id="email" />
<label style="margin-right: 25px;">Email</label>
</p>
<p>
<input style="width:20px!IMPORTANT;" type="checkbox" name="CheckboxGroup1[]" value="Post" id="post" />
<label style="margin-right: 25px;">Post</label>
</p>
</td>
</tr>
<tr>
<td>
<p>Address (if applicable):</p>
</td>
<td> </td>
<td colspan="2"><input type="text" name="address" id="address" /></td>
</tr>
<tr>
<td valign="top">
<p>Where did you hear about us?:</p>
</td>
<td> </td>
<td align="left" valign="top">
<p>
<input style="width:20px!IMPORTANT;" type="checkbox" name="CheckboxGroup2[]" value="Search Engine" id="search" />
<label style="margin-right: 25px;">Search Engine</label>
</p>
<p>
<input style="width:20px!IMPORTANT;" type="checkbox" name="CheckboxGroup2[]" value="NPA" id="NPA" />
<label style="margin-right: 25px;">NPA</label>
</p>
<p>
<input style="width:20px!IMPORTANT;" type="checkbox" name="CheckboxGroup2[]" value="Advertisement" id="advertisement" />
<label style="margin-right: 25px;">Advertisement</label>
</p>
<p>
<input style="width:20px!IMPORTANT;" type="checkbox" name="CheckboxGroup2[]" value="Brochure" id="brochure" />
<label style="margin-right: 25px;">Brochure</label>
</p>
<p>
<input style="width:20px!IMPORTANT;" type="checkbox" name="CheckboxGroup2[]" value="Show" id="show" />
<label style="margin-right: 25px;">Show</label>
</p>
<p>
<input style="width:20px!IMPORTANT;" type="checkbox" name="CheckboxGroup2[]" value="Other" id="other" />
<label style="margin-right: 25px;">Other</label>
</p>
</td>
</tr>
<tr>
<td>
<p>Please Specify:</p>
</td>
<td> </td>
<td colspan="2"><input type="text" name="specify" id="specify" /></td>
</tr>
</tbody>
</table>
<table border="0" style="float:left;margin-left:146px;" width="490">
<tbody>
<tr>
<td> </td>
<td>
<script type="text/javascript">
function validateCaptcha()
{
if ($('input[name="valid"]')) return true;
if ($('input[name="recaptcha_response_field"]').val() == "")
{
alert("Please complete the CAPTCHA field.");
return false
}
$.ajax({
url: "http://www.medilogicuk.com/wp-content/themes/default/verify.php",
type: "POST",
async:"false",
data: {
recaptcha_response_field: $('input[name="recaptcha_response_field"]').val(),
recaptcha_challenge_field: $('input[name="recaptcha_challenge_field"]').val()
},
success: function(data){
if (data == "OK")
{
$('input[name="valid"]').val(1);
$('.form').submit();
}
else
{
alert(data);
}
},
error: function(){
alert("An error occured, please try again later");
}
});
return false;
};
</script>
<?php require_once('recaptchalib.php');
$publickey = ""; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
</td>
</tr>
<tr>
<td colspan="2"><button type="submit" name="submit" value="Send message">Send message</button></td>
</tr>
</tbody>
</table>
<? if(isset($_POST['submit'])) {
$to = "rob#domain.com";
$header = 'From: info#domain.com';
$subject = "Website enquiry";
$companyname_field = $_POST['companyname'];
$fullname_field = $_POST['fullname'];
$email_field = $_POST['email'];
$tel_field = $_POST['tel'];
$address_field = $_POST['address'];
$specify_field = $_POST['specify'];
$CheckboxGroup1 = $_POST['CheckboxGroup1'];
$CheckboxGroup2 = $_POST['CheckboxGroup2'];
if( is_array($_POST['CheckboxGroup1']) ){
foreach ($_POST['CheckboxGroup1'] as $val) {
$checkbox1results .= $val.", ";
}
}
if( is_array($_POST['CheckboxGroup2']) ){
foreach ($_POST['CheckboxGroup2'] as $val) {
$checkbox2results .= $val.", ";
}
}
$body = "Hello,\n\n You have an enquiry from the website, please see the details below:\n\n Name: $fullname_field\n Company Name: $companyname_field\n E-Mail: $email_field\n Tel: $tel_field\n Method of contact: $checkbox1results\n Address: $address_field\n Hear about us?: $checkbox2results\n Specify: $specify_field\n\n Please reply to the enquiry asap.\n\n Kind Regards \n The website";
mail($to, $subject, $body, $header);
echo "</br><p style=\"color:#e41770!IMPORTANT;\">Thank you for getting in touch, we will contact you shortly.</p>";
} ?>
</form>
Okie a quick Example for you:
The Form
<html>
<body>
<form action="#" method="POST">
<input id="email" type="text" value="" />
<a id="submit" href="#">Submit Form</a>
</form>
</body>
</html>
The Jquery Part:
$(document).ready(function(){
$('a#submit').click(function(event){
event.preventDefault();
validateEmail();
})
});
function validateEmail(){
//declarations
rule = /^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
//check
if ($('input#email').val() === '' || !rule.test($('input#email').val())){
alert ('That is not a Valid Email Address !');
return false;
} else {
alert('validation passed! Submit the form !');
//submit the form action here
}
}
Also you can try it live here http://jsfiddle.net/XMbEK/
You already have your answer in the code:
<script type="text/javascript">
function validateCaptcha()
{
if ($('input[name="valid"]')) return true;
if ($('input[name="recaptcha_response_field"]').val() == "")
{
alert("Please complete the CAPTCHA field.");
return false
}
Add your own needs after this:
if ( phone == 'what it needs to be' )
{
if( email == 'asdasd' ) {
// all is good
} else {
alert( 'need email too' );
}
return true;
}
else
{
alert('Blah blah wrong phone');
return false;
}
etc.

How to place my message where I want it using php

this is the code I have, I need my message to appear in the top of my form but I got an error.
I need to print my message right where I put the code print $msg
any idea about this please.
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="300" border="0">
<tr>
<td colspan="2"><?php print($msg); ?></td>
</tr>
<tr>
<td> *First name</td>
<td><span id="sprytextfield3">
<input type="text" value="" name="RFname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Last name</td>
<td><span id="sprytextfield4">
<input type="text" value="" name="RLname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*User name</td>
<td><span id="sprytextfield1">
<input type="text" name="UserName" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Password</td>
<td><span id="sprypassword1">
<input type="password" name="UserPass" value="" />
<span class="passwordRequiredMsg"><br />
A value is required.</span></span></td>
</tr>
<tr>
<td>*Email</td>
<td><span id="sprytextfield2">
<input type="text" name="UserEmail" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
</tr>
<tr>
<td>Web site</td>
<td><input type="text" name="WebSite" value="" /></td>
</tr>
<tr>
<td> </td>
<td><input name="submited" type="submit" onClick="MM_swapImage('Image1','','images/blog.png',1)" value="Register"/></td>
</tr>
</table>
</form>
</div>
<div class="image_NewUserRegister"><iframe src="images/Untitled-2.jpg" name="now_regist" width="207" height="280" scrolling="no" frameborder="0"></iframe></div>
</div>
<?php
if(isset($_POST['submited'])) {
$RFname = $_POST['RFname'];
$RLname = $_POST['RLname'];
$USERname = $_POST['UserName'];
$USERpassword = $_POST['UserPass'];
$USERemail = $_POST['UserEmail'];
$USERwebsite = $_POST['WebSite'];
$check=$db->query("select 1 from loginaccess where Email ='".$USERemail."'");
if(mysqli_num_rows($check) > 0 ) {
$msg = "<span style='color:#F00; font-size:14px; font-weight:bold;'>This email already taken</span>";
}
this is the code I have, I need my message to appear in the top of my form but I got an error. I need to print my message right where I put the code print $msg
any idea about this please.
You really need to set the message before you try to output it.
To elaborate on David's answer: move the PHP block before the form markup, like so:
<?php
if(isset($_POST['submited'])) {
$RFname = $_POST['RFname'];
$RLname = $_POST['RLname'];
$USERname = $_POST['UserName'];
$USERpassword = $_POST['UserPass'];
$USERemail = $_POST['UserEmail'];
$USERwebsite = $_POST['WebSite'];
$check=$db->query("select 1 from loginaccess where Email ='".$USERemail."'");
if(mysqli_num_rows($check) > 0 ) {
$msg = "<span style='color:#F00; font-size:14px; font-weight:bold;'>This email already taken</span>";
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="300" border="0">
<tr>
<td colspan="2"><?php print($msg); ?></td>
</tr>
<tr>
<td> *First name</td>
<td><span id="sprytextfield3">
<input type="text" value="" name="RFname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Last name</td>
<td><span id="sprytextfield4">
<input type="text" value="" name="RLname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*User name</td>
<td><span id="sprytextfield1">
<input type="text" name="UserName" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Password</td>
<td><span id="sprypassword1">
<input type="password" name="UserPass" value="" />
<span class="passwordRequiredMsg"><br />
A value is required.</span></span></td>
</tr>
<tr>
<td>*Email</td>
<td><span id="sprytextfield2">
<input type="text" name="UserEmail" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
</tr>
<tr>
<td>Web site</td>
<td><input type="text" name="WebSite" value="" /></td>
</tr>
<tr>
<td> </td>
<td><input name="submited" type="submit" onClick="MM_swapImage('Image1','','images/blog.png',1)" value="Register"/></td>
</tr>
</table>
</form>
</div>
<div class="image_NewUserRegister"><iframe src="images/Untitled-2.jpg" name="now_regist" width="207" height="280" scrolling="no" frameborder="0"></iframe></div>
</div>
HTH.

Categories