Solved help with contact form

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

charlzguitarz

Thread Starter
Joined
Dec 21, 2018
Messages
29
Hi, the following code is a functioning contact form with captcha; exactly what I want, that is if I could get it sent:
following that code isthe code for "send_form_email.php", (action=")
A OK until Clicking "send"; regardless if "comments" is filled, error code is displayed.
After a week of this and dozens of searches, ok I need help. thanks
=================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<style>
body{
background-color: #ccffff;
}
</style>

</HEAD>
<BODY><b><center>
<div class="row">

<div class="col-md-6 col-md-offset-3" id="form_container">

<h2>Contact Us</h2>

Please send your message below. We will get back to you at the earliest! <br />
&#x2714; means required<br /><br />

<!--*********************************************************** -->
<form name="htmlform" method="post" action="send_form_email.php">
<!--*********************************************************** -->

<div class="row">

<div class="col-sm-6 form-group">

<label for="name"> &#x2714; Name</label>

<input type="text" class="form-control" id="name" name="name" required>


</div>

<div class="col-sm-6 form-group">

<label for="email"> &#x2714; Email</label>

<input type="email" class="form-control" id="email" name="email" required>

<div class="row">

<div class="col-sm-12 form-group">

Comments/Questions <label for="comments"></label>

<textarea class="form-control" type="textarea" name="
Comments/Questions" id="
Comments/Questions" maxlength="6000" rows="7"></textarea>


</div>
</div><br />

<font color="#DD0000">Enter Code ></font>
<span id="txtCaptchaDiv" style="background-color:#A51D22;color:#FFF;padding:5px"></span>
<input type="hidden" id="txtCaptcha" />
<input type="text" name="txtInput" id="txtInput" size="15" />
<input type="submit" value="Submit"/>
</form>

<script type="text/javascript">
function checkform(theform) {
var why = "";

if(theform.txtInput.value == "") { why += "- Security code should not be empty.\n"; }
if(theform.txtInput.value != "") {
if(ValidCaptcha(theform.txtInput.value) == false)
{ why += "- Security code did not match.\n"; }
}
if(why != "") { alert(why);return false; }
}

//Generates the captcha function
var a = Math.ceil(Math.random() * 9)+ '';
var b = Math.ceil(Math.random() * 9)+ '';
var c = Math.ceil(Math.random() * 9)+ '';
var d = Math.ceil(Math.random() * 9)+ '';
var e = Math.ceil(Math.random() * 9)+ '';

var code = a + b + c + d + e;
document.getElementById("txtCaptcha").value = code;
document.getElementById("txtCaptchaDiv").innerHTML = code;

// Validate the Entered input aganist the generated security code function
function ValidCaptcha(){
var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
var str2 = removeSpaces(document.getElementById('txtInput').value);
if (str1 == str2) { return true; } else { return false; }
}

// Remove the spaces from the entered and generated code
function removeSpaces(string)
{return string.split(' ').join('');}
</script>
</center></body>
</html>
===============================================================
<?php
if(isset($_POST['email'])) {

// CHANGE THE TWO LINES BELOW
$email_to = "[email protected]";

$email_subject = "charlzguitarz contact form";

function died($error) {
// your error code can go here
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();
}

// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])))
{ died('there appears to be a problem with your submission.'); }

$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from))
{ $error_message .= 'is the Email Address you entered valid?<br />'; }
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name))
{ $error_message .= 'is the Name you entered valid?<br />'; }
if(strlen($comments) < 2)
{ $error_message .= 'The Comments you entered do not appear to be valid.<br />'; }
if(strlen($error_message) > 0)
{ died($error_message); }
$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments/Questions: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- place your own success html below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
die();
?>
 
Joined
Dec 15, 2011
Messages
3,397
First Name
Jim
Try placing your function (died) at the very start of your php script instead of within the conditional clause - if(isset($_POST['email']))...
... also change @mail to mail.
@mail suppresses error messages (which you may want to see!)
 
Last edited:

charlzguitarz

Thread Starter
Joined
Dec 21, 2018
Messages
29
Try placing your function (died) at the very start of your php script instead of within the conditional clause - if(isset($_POST['email']))...
... also change @mail to mail.
@mail suppresses error messages (which you may want to see!)
=================================================
is this what you suggested? submission displays "; echo $error."....etc.,....etc.,...
--------------------------------------------------------------------------------
<?hp
if(isset($_POST['email'])) {

function died($error) {
// your error code can go here
echo "There were error(s) found with 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();
}

// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])))
{ died('problem with your submission.'); }

$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from))
{ $error_message .= 'is the Email Address you entered valid?<br />'; }
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name))
{ $error_message .= 'is the Name you entered valid?<br />'; }
if(strlen($comments) < 2)
{ $error_message .= 'The Comments you entered do not appear to be valid.<br />'; }
if(strlen($error_message) > 0)
{ died($error_message); }
$email_message = "Form details below.\n\n";

// CHANGE THE TWO LINES BELOW
$email_to = "[email protected]";

$email_subject = "charlzguitarz contact form";


function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments/Questions: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- place your own success html below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
die();
?>
 
Joined
Dec 15, 2011
Messages
3,397
First Name
Jim
PHP:
<?php
function died($error) {
// your error code can go here
echo "There were error(s) found with 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();
}

if(isset($_POST['email'])) {
 

charlzguitarz

Thread Starter
Joined
Dec 21, 2018
Messages
29
PHP:
<?php
function died($error) {
// your error code can go here
echo "There were error(s) found with 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();
}

if(isset($_POST['email'])) {
-------------------------------------------------------------
this code doesn't throw error but doesn't send form
-----------------------------------------------------------
<?php
function died($error) {
// your error code can go here
echo "There were error(s) found with 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();
}

if(isset($_POST['email'])) {


// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])))
{ died('problem with your submission.'); }

$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from))
{ $error_message .= 'is the Email Address you entered valid?<br />'; }
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name))
{ $error_message .= 'is the Name you entered valid?<br />'; }
if(strlen($comments) < 2)
{ $error_message .= 'The Comments you entered do not appear to be valid.<br />'; }
if(strlen($error_message) > 0)
{ died($error_message); }
$email_message = "Form details below.\n\n";

// CHANGE THE TWO LINES BELOW
$email_to = "[email protected]";

$email_subject = "charlzguitarz contact form";


function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments/Questions: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- place your own success html below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
die();
?>
 
Joined
Dec 15, 2011
Messages
3,397
First Name
Jim
-------------------------------------------------------------
this code doesn't throw error but doesn't send form
-----------------------------------------------------------
That is because you have some errors in your PHP script:( which I don't think you can see, because of your testing method.
How are you testing? You may want to consider using a PHP server simulator, such as xampp or wamp, so that you can track errors.

Anyway here are your errors, corrections:
1) In the html ...
HTML:
<textarea class="form-control" type="textarea" name="
Comments/Questions" id="
Comments/Questions" maxlength="6000" rows="7"></textarea>
should be changed to
HTML:
<textarea class="form-control" type="textarea" name="comments" id="comments" maxlength="6000" rows="7"></textarea>
... this is because the expected name is "comments" not "Comments/Questions";) and is not being picked up in your php script.
2) In your php script ...
PHP:
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])))
has one too many close brackets. Change it to ...
PHP:
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments']))
 
Last edited:
Joined
Dec 15, 2011
Messages
3,397
First Name
Jim
Please open a new thread for your next problem area - the Captcha (which does not work), because it is javascript and requires expertise I do not possess;)
But I do notice one problem ...
In order to perform javascript we must include a link to the js library in the <head> section of our html ...
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
the link was derived from here
 
Last edited:

charlzguitarz

Thread Starter
Joined
Dec 21, 2018
Messages
29
Please open a new thread for your next problem area - the Captcha (which does not work), because it is javascript and requires expertise I do not possess;)
But I do notice one problem ...
In order to perform javascript we must include a link to the js library in the <head> section of our html ...
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
the link was derived from here
--------------------------------------------------------------
Thanks so much for your help , I have tried numerous versions attempting to send the form, none of them working. Never would have anticipated the difficulty. I went at it in stages, form, validation, captcha, then response. I use xammp. I'm copying and pasting your sugestions but falling short. Sending full code to ensure all is evident: The captcha does work.
------------------------------------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-127316285-1"></script>

<meta name="viewport" content="width=device-width, initial-scale=1">
<TITLE>CKguitarz contact page</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-">
<!--site by Kirk Enterprises--><!--last updated 1/31/2016-->
<!--web designer: Charlie Kirk-->
<meta name="description" content="CKguitarz contact page">
<META content="inexpensive, doubleneck guitars, double neck, single neck guitars, small guitars, conversion, custom guitar, guitar, lightweight" name=keywords>

<style>
body{
background-color: #ccffff;
}
</style>

</HEAD>
<BODY><b><center>

<div class="containerBox">
<img class="img-responsive" src="notes02.jpeg">
<div class='text-box'>
<p class='dataNumber'><input type="text" value="charlzguitarz" size = 10 STYLE="color: #000000; background-color: #D4AAFF; font-size: 15px;"></p>
</div></div><br />

<STYLE TYPE="text/css">
.containerBox { position: relative;display: inline-block; }
.text-box { position: absolute;height: 30%;text-align: center;width: 100%;margin: auto;
top: 0;bottom: 0;right: 0;left: 0;font-size: 30px; }
.img-responsive { display: block;max-width: 100%;
height: 120px;margin: auto;padding: auto; }
.dataNumber { margin-top: auto; }
</STYLE>

<div class="row">

<div class="col-md-6 col-md-offset-3" id="form_container">

<h2>Contact Us</h2>

Please send your message below. We will get back to you at the earliest! <br />
&#x2714; means required<br /><br />

<!--*********************************************************** -->
<form name="htmlform" method="post" action="html_form_send.php">

<!--*********************************************************** -->

<form role="form" method="post" id="reused_form">

<div class="row">

<div class="col-sm-6 form-group">

<label for="name"> &#x2714; Name</label>

<input type="text" class="form-control" id="name" name="name" required>


</div>

<div class="col-sm-6 form-group">

<label for="email"> &#x2714; Email</label>

<input type="email" class="form-control" id="email" name="email" required>

<div class="row">

<div class="col-sm-12 form-group">

Comments/Questions <label for="comments"></label>

!-- ****************************************************** -->
<textarea class="form-control" type="textarea" name="
comments" id="comments" maxlength="6000" rows="7"></textarea>


!-- ****************************************************** -->

</div>
</div><br />

<font color="#DD0000">Enter Code ></font>
<span id="txtCaptchaDiv" style="background-color:#A51D22;color:#FFF;padding:5px"></span>
<input type="hidden" id="txtCaptcha" />
<input type="text" name="txtInput" id="txtInput" size="15" />
<input type="submit" value="Submit"/>
</form>

<script type="text/javascript">
function checkform(theform)
{
var why = "";

if(theform.txtInput.value == "")
{ why += "- Security code should not be empty.\n"; }
if(theform.txtInput.value != "")
{
if(ValidCaptcha(theform.txtInput.value) == false)
{ why += "- Security code did not match.\n"; }
}
if(why != "")
{ alert(why); return false; }
}

//Generates the captcha function
var a = Math.ceil(Math.random() * 9)+ '';
var b = Math.ceil(Math.random() * 9)+ '';
var c = Math.ceil(Math.random() * 9)+ '';
var d = Math.ceil(Math.random() * 9)+ '';
var e = Math.ceil(Math.random() * 9)+ '';

var code = a + b + c + d + e;
document.getElementById("txtCaptcha").value = code;
document.getElementById("txtCaptchaDiv").innerHTML = code;

// Validate the Entered input aganist the generated security code function
function ValidCaptcha()
{
var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
var str2 = removeSpaces(document.getElementById('txtInput').value);
if (str1 == str2)
{ return true; }
else
{ return false; }
}

// Remove the spaces from the entered and generated code
function removeSpaces(string){
return string.split(' ').join('');
}
</script>

</center></body>
</html>
------------------------------------------------------------
<?php
function died($error) {
// your error code can go here
echo "There were error(s) found with 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(); }

if(isset($_POST['email'])) {

<!-- *********************************************************** -->
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments']))
{ died('problem with your submission.'); }

$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
<!-- *********************************************************** -->

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from))
{ $error_message .= 'is the Email Address you entered valid?<br />'; }
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name))
{ $error_message .= 'is the Name you entered valid?<br />'; }
if(strlen($comments) < 2)
{ $error_message .= 'The Comments you entered do not appear to be valid.<br />'; }
if(strlen($error_message) > 0)
{ died($error_message); }
$email_message = "Form details below.\n\n";

// CHANGE THE TWO LINES BELOW
$email_to = "[email protected]";
$email_subject = "charlzguitarz contact form";

function clean_string($string)
{
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

<!-- *********************************************************** -->
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments/Questions: ".clean_string($comments)."\n";
<!-- **************************************************************** -->

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- place your own success html below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
die();
?>
 
Joined
Dec 15, 2011
Messages
3,397
First Name
Jim
I'm copying and pasting your sugestions but falling short. Sending full code to ensure all is evident: The captcha does work.
This html line wasn't changed
HTML:
<textarea class="form-control" type="textarea" name="
comments" id="comments" maxlength="6000" rows="7"></textarea>
- the 'break' in the line is causing a newline character to be the first byte of the posted comment name, which causes the error.

When you say the Captcha works, have you tried sending through no input or an incorrect number? When I submit with blank or incorrect input, the submission succeeds without any alertso_O
Sorry, but I cannot find where the function checkform is triggered:(

FYI - further to my comment about needing jquery to perform javascript, please check out this link. With advanced browsers, it appears that this statement is not a given - my bad:(
 
Last edited:
Joined
Sep 5, 2007
Messages
2,307
First Name
Colin
Give this a try
PHP:
if((!isset($_POST['name']) || (isset($_POST['email'])) || (!isset($_POST['comments'])))
 
Joined
Dec 15, 2011
Messages
3,397
First Name
Jim
if((!isset($_POST['name']) || (isset($_POST['email'])) || (!isset($_POST['comments'])))
The extra set of brackets may make it more readable, Colin, but they are not needed - so long as there is at least one set of parentheses in a compound if statement, it will work;)
 

charlzguitarz

Thread Starter
Joined
Dec 21, 2018
Messages
29
When you say the Captcha works, have you tried sending through no input or an incorrect number? When I submit with blank or incorrect input, the submission succeeds without any alertso_O
Sorry, but I cannot find where the function checkform is triggered:(
----------------------------------------------------------------------------
being current with your much appreciated suggestions:
When I leave name or email blank I get "please fill out this field"
When I fill out required fields and submit with blank code I get "security code should not be empty"
When I fill out required fields and submit with incorrect code I get "security code did not match"
when is filled out completely and correctly I get no errors and no delivery ?
-------------------------------------------------------------------------
html:
<!--*********************************************************** -->
<form name="htmlform" method="post" action="html_form_send.php">
<!--*********************************************************** -->
<form role="form" method="post" id="reused_form">
<div class="row">
<div class="col-sm-6 form-group">
<label for="name"> &#x2714; Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="col-sm-6 form-group">
<label for="email"> &#x2714; Email</label>
<input type="email" class="form-control" id="email" name="email" required>
<div class="row">
<div class="col-sm-12 form-group">
Comments/Questions <label for="comments"></label>
!-- ****************************************************** -->
<textarea class="form-control" type="textarea" name="comments" id="comments"

maxlength="6000" rows="7"></textarea>
!-- ****************************************************** -->
</div>
</div><br />
<font color="#DD0000">Enter Code ></font>
<span id="txtCaptchaDiv" style="background-

color:#A51D22;color:#FFF;padding:5px"></span>
<input type="hidden" id="txtCaptcha" />
<input type="text" name="txtInput" id="txtInput" size="15" />
<input type="submit" value="Submit"/>
</form>
<!-- ********************* begin captcha test ******************* -->
<script type="text/javascript">
function checkform(theform)
{
var why = "";
if(theform.txtInput.value == "")
{ why += "- Security code should not be empty.\n"; }
if(theform.txtInput.value != "")
{
if(ValidCaptcha(theform.txtInput.value) == false)
{ why += "- Security code did not match.\n"; }
}
if(why != "")
{ alert(why); return false; }
}
// Generates the captcha function
var a = Math.ceil(Math.random() * 9)+ '';
var b = Math.ceil(Math.random() * 9)+ '';
var c = Math.ceil(Math.random() * 9)+ '';
var d = Math.ceil(Math.random() * 9)+ '';
var e = Math.ceil(Math.random() * 9)+ '';
var code = a + b + c + d + e;
document.getElementById("txtCaptcha").value = code;
document.getElementById("txtCaptchaDiv").innerHTML = code;
// Validate the Entered input aganist the generated security code function
function ValidCaptcha()
{
var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
var str2 = removeSpaces(document.getElementById('txtInput').value);
if (str1 == str2)
{ return true; }
else
{ return false; }
}
// Remove the spaces from the entered and generated code
function removeSpaces(string){
return string.split(' ').join('');
}
</script>
<!-- *********************end of captcha test******************** -->
----------------------------------------------------------------------
php:
if(isset($_POST['email'])) {

<!-- *********************************************************** -->
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments']))
{ died('problem with your submission.'); }

$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
<!-- *********************************************************** -->

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from))
{ $error_message .= 'is the Email Address you entered valid?<br />'; }
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name))
{ $error_message .= 'is the Name you entered valid?<br />'; }
if(strlen($comments) < 2)
{ $error_message .= 'The Comments you entered do not appear to be valid.<br />'; }
if(strlen($error_message) > 0)
{ died($error_message); }
$email_message = "Form details below.\n\n";

// CHANGE THE TWO LINES BELOW
$email_to = "[email protected]";
$email_subject = "charlzguitarz contact form";

function clean_string($string)
{
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

<!-- *********************************************************** -->
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments/Questions: ".clean_string($comments)."\n";
<!-- **************************************************************** -->
 

charlzguitarz

Thread Starter
Joined
Dec 21, 2018
Messages
29
I really appreciate your sticking w/me jiminSA.
I changed this:
<form name="htmlform" method="post" action="html_form_send.php">
with this:
<form name="htmlform" method="post" action="html_form_send.php" onsubmit=”return checkform(this);”>
with or without code the error php code is displayed:

"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); } if(isset($_POST['email'])) { // validation expected data exists if(!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['comments'])) { died('problem with your submission.'); } $name = $_POST['name']; // required $email_from = $_POST['email']; // required $comments = $_POST['comments']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'is the Email Address you entered valid?
'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$name)) { $error_message .= 'is the Name you entered valid?
'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; // CHANGE THE TWO LINES BELOW $email_to = "[email protected]"; $email_subject = "charlzguitarz contact form"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Comments/Questions: ".clean_string($comments)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($email_to, $email_subject, $email_message, $headers); ?> Thank you for contacting us. We will be in touch with you very soon.
 
Joined
Dec 15, 2011
Messages
3,397
First Name
Jim
What I would like you to do is post the code you have for your html file and your php file, entirely and separately, using the Insert... facility which you can invoke by clicking the 4th icon from the right at the top of the reply box. This makes it much easier to read;) I am using the following code, which successfully sends the email, but which does not perform the Captcha check:
The HTML file:
HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<style>
body{
background-color: #ccffff;
}
</style>
</HEAD>
<BODY><b><center>
<div class="row">

<div class="col-md-6 col-md-offset-3" id="form_container">

<h2>Contact Us</h2>

Please send your message below. We will get back to you at the earliest! <br />
&#x2714; means required<br /><br />

<!--*********************************************************** -->
<form name="htmlform" method="post" action="mail.php">
<!--*********************************************************** -->

<div class="row">

<div class="col-sm-6 form-group">

<label for="name"> &#x2714; Name</label>

<input type="text" class="form-control" id="name" name="name" required>


</div>

<div class="col-sm-6 form-group">

<label for="email"> &#x2714; Email</label>

<input type="email" class="form-control" id="email" name="email" required>

<div class="row">

<div class="col-sm-12 form-group">

Comments/Questions <label for="comments"></label>

<textarea class="form-control" type="textarea" name="comments" id="comments" maxlength="6000" rows="7"></textarea>


</div>
</div><br />

<font color="#DD0000">Enter Code ></font>
<span id="txtCaptchaDiv" style="background-color:#A51D22;color:#FFF;padding:5px"></span>
<input type="hidden" id="txtCaptcha" />
<input type="text" name="txtInput" id="txtInput" size="15" />
<input type="submit" value="Submit"/>
</form>

<script type="text/javascript">
function checkform() {
var why = "";

if(document.getElementById('txtInput').value == "") { why += "- Security code should not be empty.\n"; }
if(document.getElementById('txtInput').value != "") {
if(ValidCaptcha(document.getElementById('txtInput').value) == false)
{ why += "- Security code did not match.\n"; }
}
if(why != "") { alert(why);return false; }
}

//Generates the captcha function
var a = Math.ceil(Math.random() * 9)+ '';
var b = Math.ceil(Math.random() * 9)+ '';
var c = Math.ceil(Math.random() * 9)+ '';
var d = Math.ceil(Math.random() * 9)+ '';
var e = Math.ceil(Math.random() * 9)+ '';

var code = a + b + c + d + e;
document.getElementById("txtCaptcha").value = code;
document.getElementById("txtCaptchaDiv").innerHTML = code;

// Validate the Entered input aganist the generated security code function
function ValidCaptcha(){
var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
var str2 = removeSpaces(document.getElementById('txtInput').value);
if (str1 == str2) { return true; } else { return false; }
}

// Remove the spaces from the entered and generated code
function removeSpaces(string)
{return string.split(' ').join('');}
</script>
</center></body>
</html>
The PHP code:
PHP:
<?php
function died($error) {
// your error code can go here
echo "There were error(s) found with 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();
}

if(isset($_POST['email'])) {

// validation expected data exists
if( !isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['comments']) )
{ died('problem with your submission.'); }

$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from))
{ $error_message .= 'is the Email Address you entered valid?<br />'; }
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name))
{ $error_message .= 'is the Name you entered valid?<br />'; }
if(strlen($comments) < 2)
{ $error_message .= 'The Comments you entered do not appear to be valid.<br />'; }
if(strlen($error_message) > 0)
{ died($error_message); }
$email_message = "Form details below.\n\n";

// CHANGE THE TWO LINES BELOW
$email_to = "[email protected]";

$email_subject = "charlzguitarz contact form";


function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "<br />Name: ".clean_string($name)."<br />";
$email_message .= "Email: ".clean_string($email_from)."<br />";
$email_message .= "Comments/Questions: ".clean_string($comments)."<br />";


// create email headers
$headers = 'From: '.$email_from."<br />".
'Reply-To: '.$email_from."<br />" .
'X-Mailer: PHP/' . phpversion();
//mail($email_to, $email_subject, $email_message, $headers);
echo $email_to."<br />".$email_subject."<br />".$email_message."<br />".$headers;
?>
<!-- place your own success html below -->
<br />Thank you for contacting us. We will be in touch with you very soon.
<?php
}
die();
?>
If we do a comparison, we should be able to sort both problems on both sides:)
FYI: You may notice that I have replaced the actual mailing with an echo of the email to be sent - this, because xampp does not d0 email send;)
 
Last edited:

charlzguitarz

Thread Starter
Joined
Dec 21, 2018
Messages
29
What I would like you to do is post the code you have for your html file and your php file, entirely and separately, using the Insert... facility which you can invoke by clicking the 4th icon from the right at the top of the reply box. This makes it much easier to read;) I am using the following code, which successfully sends the email, but which does not perform the Captcha check
----------------------------------------------------------------------------------
1) post html code ?
2) click the 4th icon from right at the top of the reply box from your eply?
3) post php?
4) click the 4th icon from right at the top of the reply box from your reply?
-----------------------------------------------------------------------------------
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Top