Contact form not providing error, confirmation or email - php

I've used this same form a number of times but this particular occasion it doesn't seem to actually be sending any email nor providing me any error messages or confirmation.
The output of the: print_r($_POST); from a test just a moment ago is the following:
Array ( [Name] => test name [Email] => test#test.com [Company] => example company [Position] => ceo [Captcha] => 1 )
So the form is grabbing all details correctly and attempting to pass them on, but no email or confirmation/error(s).
PHP / HTML
<?php
if (count($error)>=1) {
foreach($error as $one_error) {
echo '<h3>'.$one_error.'</h3>';
echo '<div class="clear"></div>';
}
}
if ($message_sent) {
echo '<h2>Thank you for your request. We will be in touch soon!</h2>';
echo '<hr class="hr" />';
}
?>
<form action="index.php" method="POST">
<span>
<label><strong>Name</strong></label><br />
<input type="text" name="Name" placeholder="Name" value="<?=$Name;?>" required />
</span>
<span>
<label><strong>Your Email Address</strong></label><br />
<input type="text" name="Email" placeholder="Email Address" value="<?=$Email;?>" required />
</span>
<br /><br />
<span>
<label><strong>Company</strong></label><br />
<input type="text" name="Company" placeholder="Company" value="<?=$Company;?>" required />
</span>
<span>
<label><strong>Position</strong></label><br />
<input type="text" name="Position" placeholder="Company Position" value="<?=$Position;?>" required />
</span>
<br /> <br />
<label><strong>Captcha</strong></label><br />
<img src="<?php HTTP_HOST ?>/Contact/answer.php">
<input type="text" placeholder="Enter Sum Calculation" name="Captcha" value="<?=$Captcha;?>" required />
<div class="clear"></div><br />
<input type="submit" value="Send Enquiry" />
</form>
PHP (Inside of the same index.php file)
<?php
session_start();
print_r($_POST);
// print_r($_SESSION['code']);
function isValidInetAddress($data, $strict = false) {
$regex = $strict ? '/^([.0-9a-z_-]+)#(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i' : '/^([*+!.&#$¦\'\\%\/0-9a-z^_`{}=?~:-]+)#(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i';
if (preg_match($regex, trim($data), $matches)) {
return array($matches[1], $matches[2]);
} else {
return false;
}
}
$Name = '';$Email = '';$Company = '';$Position = '';$Captcha = '';
if (isset($_POST)&&(isset($_POST['Submit'])&&(strlen($_POST['Submit'])>1))) {
$error = array();
if (isset($_POST['Name'])&&(strlen($_POST['Name'])>1)) {
$Name = $_POST['Name'];
} else {
$error[]='Name is empty';
}
if (isset($_POST['Email'])&&(strlen($_POST['Email'])>1)) {
$Email = $_POST['Email'];
} else {
$error[]='Email is empty';
}
if (isset($_POST['Company'])&&(strlen($_POST['Company'])>1)) {
$Company = $_POST['Company'];
} else {
$error[]='Company is empty';
}
if (isset($_POST['Position'])&&(strlen($_POST['Position'])>1)) {
$Position = $_POST['Position'];
} else {
$error[]='Position is empty';
}
if (isset($_POST['Captcha'])&&($_POST['Captcha']==$_SESSION['code'])) {
} else {
$error[]='Captcha is wrong';
}
if (count($error)<1) {
$headers = 'From: jordan#gmail.com' . "\r\n" .
'Reply-To: jordan#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message ='New DiSC Profile Request'."\r\n";
$message .= 'Name: '.$Name."\r\n";
$message .= 'Email: '.$Email."\r\n";
$message .= 'Company: '.$Company."\r\n";
$message .= 'Position: '.$Position."\r\n";
mail('jordan#gmail.com', 'New DiSC Profile Request', $message, $headers);
// mail('galin#mangcreative.com', 'New Enquiry from Urban Country', $message, $headers);
unset($Name);unset($Email);unset($Company);unset($Position);
$message_sent = TRUE;
}
}
?>

Change the form submit button and give it a name
<input type="submit" value="Send Enquiry" />
to
<input type="submit" value="Send Enquiry" name="Submit"/>

You need to have name="" on the button;
<input type="submit" value="Send Enquiry" name="submit" />
You also don't need to have action="index.php" if the php is on the same page;
<form action="" method="POST">
Also you don't need
if (isset($_POST)&&(isset($_POST['Submit'])&&(strlen($_POST['Submit'])>1))) {
Use: if (isset($_POST['submit'])===true) {

Related

simple php captcha contact form

i am trying to create simple contact form with captcha in php. However it turns out implementing captcha is out of my league.
I found a simple answer on stackoverflow opn similar problem which pushed me 1 step closer to the end, but again i got stuck.
So i need a contact form that only check if text is entered and if correct captcha is answered, email is not mandatory.
</br>
<?php
$a=rand(2,9);
$b=rand(2,9);
$c=$a+$b;
if (isset($_POST['contact_text']) && isset($_POST['contact_email']) ) {
$contact_text = $_POST['contact_text'];
$contact_email = $_POST['contact_email'];
$recaptcha = $_POST['recaptcha'];
$info = 'Pranešimas apie korupciją: ';
$sender = 'Atsiuntė: ';
if (!empty($contact_text) && ($recaptcha == $c )) {
echo $recaptcha;
$to = 'muksinovas#gmail.com';
$subject = 'Korupcija';
$body = $sender."\n".$contact_email."\n".$info."\n".$contact_text;
$headers = 'From: '.$contact_email;
if (#mail($to,$subject, $body, $headers)) {
echo 'Jūsų pranešimas sėkmingai išsiustas. ';
} else {
} echo 'Įvyko klaida, bandykite dar karta.';
} else {
echo 'Neteisingai užpildyta forma.';
}
}
?>
<form action="contact1.php" method="post">
Pranešimas apie korupciją:<br><textarea name="contact_text" rows="6" cols="30" maxlength="1000" ></textarea><br><br> <!-- -->
Email (nebūtinas):<br><input type="text" name="contact_email" maxlength="30">
<?php echo $a."+".$b."="?><input type="number" name="recaptcha" maxlength="2" style="width:40px" />
<input type="submit" value="Siusti">
<br>
</form>
Now the problem is that I always get the message that details are incorrect. I tried to echo recaptcha just to see if $c is correct and it works. But for some reason not able to compare $recaptcha with $c or some other issue I am not sure.
The value of $c will be a completely different value when the user submits the contact form vs when your validation checks it. The value will change on every request because the script is re-interpreted.
You will have to save the value of $c on the initial page load, so that you can compare it afterwards in the next request. You can do that by storing it in $_SESSION.
You can write this
<?php
$min_number = 2;
$max_number = 9;
$random_number1 = mt_rand($min_number, $max_number);
$random_number2 = mt_rand($min_number, $max_number);
if (isset($_POST['contact_text']) && isset($_POST['contact_email']) ) {
$contact_text = $_POST['contact_text'];
$contact_email = $_POST['contact_email'];
$recaptcha = $_POST['recaptcha'];
$firstNumber = $_POST["firstNumber"];
$secondNumber = $_POST["secondNumber"];
$checkTotal = $firstNumber + $secondNumber;
$info = 'Pranešimas apie korupciją: ';
$sender = 'Atsiuntė: ';
if (!empty($contact_text) && ($recaptcha != $checkTotal )) {
echo $recaptcha;
$to = 'muksinovas#gmail.com';
$subject = 'Korupcija';
$body = $sender."\n".$contact_email."\n".$info."\n".$contact_text;
$headers = 'From: '.$contact_email;
if (#mail($to,$subject, $body, $headers)) {
echo 'Jūsų pranešimas sėkmingai išsiustas. ';
} else {
} echo 'Įvyko klaida, bandykite dar karta.';
} else {
echo 'Neteisingai užpildyta forma.';
}
}
?>
<form action="contact1.php" method="post">
Pranešimas apie korupciją:<br><textarea name="contact_text" rows="6" cols="30" maxlength="1000" ></textarea><br><br> <!-- -->
Email (nebūtinas):<br><input type="text" name="contact_email" maxlength="30">
<?php
echo $random_number1 . ' + ' . $random_number2 . ' = ';
?>
<input type="number" name="recaptcha" maxlength="2" style="width:40px" />
<input name="firstNumber" type="hidden" value="<?php echo $random_number1; ?>" />
<input name="secondNumber" type="hidden" value="<?php echo $random_number2; ?>" />
<input type="submit" value="Siusti">
<br>
</form>
This might solve your problem
you should to use session to solve your problem, i did little changes in your code, it should to work perfectly.
<?php
#session_start();
if (isset($_POST['contact_text']) && isset($_POST['contact_email']) ) {
$contact_text = $_POST['contact_text'];
$contact_email = $_POST['contact_email'];
$recaptcha = $_POST['recaptcha'];
$info = 'Pranešimas apie korupciją: ';
$sender = 'Atsiuntė: ';
if (!empty($contact_text) && ($recaptcha == $_SESSION["captcha"])) {
echo $recaptcha;
$to = 'muksinovas#gmail.com';
$subject = 'Korupcija';
$body = $sender."\n".$contact_email."\n".$info."\n".$contact_text;
$headers = 'From: '.$contact_email;
if (#mail($to,$subject, $body, $headers)) {
echo 'Jūsų pranešimas sėkmingai išsiustas. ';
} else {
} echo 'Įvyko klaida, bandykite dar karta.';
}else{
echo 'Neteisingai užpildyta forma.';
}
}else{
$a=rand(2,9);
$b=rand(2,9);
$c=$a+$b;
//setting captcha code in session
$_SESSION["captcha"] = $c;
?>
<form action="contact1.php" method="post">
Pranešimas apie korupciją:<br><textarea name="contact_text" rows="6" cols="30" maxlength="1000" ></textarea><br><br> <!-- -->
Email (nebūtinas):<br><input type="text" name="contact_email" maxlength="30">
<?php echo $a."+".$b."="?><input type="number" name="recaptcha" maxlength="2" style="width:40px" />
<input type="submit" value="Siusti">
<br>
</form>
<?php
}
?>

PHP form validation not working after first submit

I'm using a contact form on my website and I'm trying to validate the user input using PHP. The problem with it is that although it will validate the input alright, if you then enter correct details and hit send, it doesn't do anything. I think this is because it's not doing the second part of the if statement after an error has been entered, but I'm not sure how to fix it. Below is the code I'm using so far.
<?php
error_reporting(E_ALL ^ E_NOTICE);
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
{
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
}
$send = $_POST['send'];
$message = "";
$email = $details['email'];
foreach ($details as $field => $detail)
$message .= ucfirst($field) . ": " . $detail . "\r\n";
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$message = wordwrap($message, 70, "\r\n");
$headers = 'From: ' .$email . "\r\n" .
'Reply-To: ' .$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
if ($send)
{
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";
}
if (!is_numeric($details['number']))
echo "<p class='error'>Please enter a valid telephone number</p>";
}
else
{
mail($to, $subject, $message, $headers);
echo "<p class='success'>Thank you for your message, you will receive a response shortly.</p>";
}
?>
<div id="contactform">
<form action="" method="post">
<fieldset>
<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" required />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" required />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" required></textarea>
<p class="small"><span class="star">*</span> Denotes a required field </p>
<input type="submit" id="send" name="send" value="Send" />
</fieldset>
</form>
I tried changing it to a while loop and just doing the validation until it is valid and then sending the email, however my attempts at this didn't work either.
Could you try change your if ($send) {} block to the following and test?
if ($send) {
$bHasError = false;
foreach ($details as $field => $detail) {
if (empty($detail) && $field!='number') {
echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";
$bHasError = true;
}
}
if (!is_numeric($details['number'])) {
echo "<p class='error'>Please enter a valid telephone number</p>";
$bHasError = true;
}
if (!$bHasError) {
mail($to, $subject, $message, $headers);
echo "<p class='success'>Thank you for your message, you will receive a response shortly.</p>";
}
}

php contact form not sending mail after validation

I'm using a contact form for my website, which I validate and then email to myself, the validation is working correctly and it emails me if the user enters all the details correctly first time. However if the user enters incorrect data, then corrects it and hits send again, it won't send an email, below is the form and PHP code I have so far.
HTML code for contact form
<form action="contact.php" method="post">
<fieldset>
<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" required />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" required />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" required></textarea>
<p class="small"><span class="star">*</span> Denotes a required field </p>
<input type="submit" id="send" name="send" value="Send" />
</fieldset>
</form>
PHP code for sending the form
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
{
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
}
$send = $_POST['send'];
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "<br />";
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$message = wordwrap($message, 70, "/r/n");
$headers = "From ". $details['email'];
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
if ($send)
{
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";
}
}
else
{
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
}
EDIT
In order for the code to work on the same page, you need to set the action to action=""
Otherwise, you need to use two pages. One for your form and one for contact.php which is your handler. I suggest you use two pages, but here is a version that will work inside one page.
<?php
if(isset($_POST['send'])) {
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");
}
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "\n";
$send = $_POST['send'];
$email = $_POST['email'];
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
exit;
} // closing brace for if(isset($_POST['send'])) {
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="post">
<fieldset>
<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" ></textarea>
<p class="small"><span class="star">*</span> Denotes a required field </p>
<input type="submit" id="send" name="send" value="Send" />
</fieldset>
</form>
</body>
</html>
Original answer
This line is not properly formatted.
$message = wordwrap($message, 70, "/r/n");
change it to:
$message = wordwrap($message, 70, "\r\n");
You need to use \ instead of /
EDIT
The only way I could get your form to work, is to add a die function.
Try this now:
<?php
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");
}
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "\n";
$send = $_POST['send'];
$email = $_POST['email'];
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
exit;
?>

Issue with one page site contact form

i have a one page portfolio with a contact form, i have been trying to troubleshoot this thing but have not succeeded. I am no PHP expert so I will try to explain as best as I can. Basically what I have is a contact template file from word press that i have used in the past, but what i want to do is (if possible) implement in my portfolio. My main issue is that when I preview my site in the browser it shows the PHP code that is on the form.
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// Check fields for errors
if (empty($_POST["txtName"])) {
$errors["txtName"] = "Please enter your name.";
}
if (empty($_POST["txtPhone"])) {
$errors["txtPhone"] = "Please enter your phone number.";
}
if (empty($_POST["txtEmail"])) {
$errors["txtEmail"] = "Please enter your email address.";
} else {
if (!eregi('^[[:alnum:]][a-z0-9_\.\-]*#[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST["txtEmail"])))) {
$errors["txtEmail"] = "Please provide a valid email address.";
}
}
if (count($errors) < 1) {
$to = "test-email#gmail.com";
$subject = 'Bave Designs Contact';
$headers = "From:" . $_POST["txtEmail"] . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = '<html><body style="font-family:Arial, Helvetica, sans-serif; font-size:12px;">';
$message .= '<p><strong>Name:</strong> ' . $_POST["txtName"] . '<br />
<strong>Phone:</strong> ' . $_POST["txtPhone"] . '<br />
<strong>Email:</strong> ' . $_POST["txtEmail"] . '<br />
<strong>Message:</strong><br />' . $_POST["txtComment"] . '</p>
</body>
</html>';
if ( !mail($to,$subject,$message,$headers) ) {
$errors["send"] = "There was a problem sending your message. Please try again.";
}
}
}
<div id="contact-form">
<?php
if (count($errors) > 0) {
echo '<ul style="color:red; padding:0 0 18px 22px;">';
foreach ($errors as $error) {
echo "<li>" . $error . "</li>";
}
echo "</ul>";
}
?>
<?php if ($_SERVER['REQUEST_METHOD'] == "POST" && count($errors) < 1) { ?>
<p class="success-message"><?php _e('Thank you! Your message has been sent.'); ?></p>
<?php } else { ?>
<form id="contact1" method="post" action="#message" class="contact-form">
<p><label for="txtName">Name</label>
<input type="text" id="txtName" name="txtName" class="field" size="40" value="<?php echo $_POST['txtName'] ?>" /></p>
<p><label for="txtPhone">Phone</label>
<input type="text" id="txtPhone" name="txtPhone" class="field" size="40" value="<?php echo $_POST['txtPhone'] ?>" /></p>
<p><label for="txtEmail">Email</label>
<input type="text" id="txtEmail" name="txtEmail" class="field" size="40" value="<?php echo $_POST['txtEmail'] ?>" /></p>
<p><label for="txtComment">Message</label>
<textarea name="txtComment" id="txtComment" class="field" cols="40" rows="10"><?php echo $_POST['txtComment'] ?></textarea></p>
<p><input type="submit" id="btnSubmit" name="btnSubmit" class="button" value="Submit" /></p>
</form>
<?php } ?>
</div><!-- CONTACT FORM ENDS-->
I switched the wp_mail function to mail which I know is correct.
Any help would be appreciated.
Thanks in advance.
My main issue is that when I preview my site in the browser it shows
the PHP code that is on the form.
So, if I understand correctly, your PHP codes are being shown in your page?
If that is the case, I urge you to contact your webhosting provider asap, because there seems to be a big problem with your Apache configuration.

Why isnt this PHP Mailer for HTML Forms working?

This is the PHP code:
<?PHP
$to = "adschweinfurth#gmail.com";
$subject = "New Finance Request";
$headers = "Request:";
$forward = 0;
$location = "";
$date = date ("l, F jS, Y");
$time = date ("h:i A");
$msg = "Below is the request sent on $date at $time.\n\n";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
foreach ($_POST as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
}
else {
foreach ($_GET as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
}
mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thanks! Your request has been sent for approval and someone will be in contact with you soon!";
}
?>
This is the HTML code:
<form action="mailer.php" method="post">
<strong>Name:</strong><br />
<input type="text" name="Name" />
<br />
<br />
<strong>Email:</strong><br />
<input type="text" name="Email" />
<br />
<br />
<strong>Organization:</strong><br />
<input type="text" name="Org:" />
<br />
<br />
<strong>Amount:</strong><br />
<input type="text" name="Amount" />
<br />
<br />
<strong>Explain Request:</strong><br />
<textarea rows="5" cols="30" name="Message"></textarea>
<br />
<hr />
<input type="submit" name="submit" value="Submit">
</form>
I can not for the life of me get it to actually send the email. It goes to the thanks page, but I never receive any email...Let me know, comment below.
Put it up like this if email is really sent or if there is an error:
if (mail($to, $subject, $msg, $headers)){
// email sent
}
else {
echo 'some error occurred !';
}

Categories