HomeWatched TopicsRegisterSearchDirectory
FAQMemberlistUsergroupsLog inStoresItemsBank

Reply to topic Page 1 of 2
Goto page 1, 2  Next
PHP Form Mailer with Validation
Message  

Reply with quote
Post PHP Form Mailer with Validation 
I am a complete php illiterate.  I have been able to adapt some scripts in past but this one has me beat.  Please don't laugh at my stupidity.  I tried adapting the following to use with my html form.  Can you spot the problem?  Right now it does not send my email but redirects to index2.html every time even when all required fields are entered.

[code]<?php
//Edit the strings below and you're good to go!
$adminemail = 'service@copysystemsinc.com';//Your email address
$thankspage = 'thankyou.html';//Thanks message here
$contactpage = 'index2.html';//The page with the contact form
$invalidemail = 'The email address you entered is invalid';//The text to display if email addy is invalid
$incomplete = "Some required fields are empty; Click back and try again.";//Text to display whenever there's an empty field
$emptyfield = '*';//To be displayed where there's an empty field
//You don't have to edit anything below here.

$id = $_POST["id"];
if (!$id) {
    $id = "main";
    }

###############################
###  This sends the message ###
###############################
if ($id == 'send') {

// get all variables and remove slashes
$Company = stripslashes($_REQUEST["Company"]);
$name = stripslashes($_REQUEST["Contact"]);
$PhoneNo = stripslashes($_REQUEST["PhoneNo"]);
$ModelNo = stripslashes($_REQUEST["ModelNo"]);
$SerialIDNo = stripslashes($_REQUEST["SerialIDNo"]);
$MeterReading = stripslashes($_REQUEST["MeterReading"]);
$Problem = stripslashes($_REQUEST["Problem"]);
$email = stripslashes($_REQUEST["Email"]);//If Email addy is entered, then we proceed to check whether it is valid
$subject = "Service Request";
$message = stripslashes($_REQUEST["Problem"]);

//checking the fields
if($name == ''){$error1 = "$emptyfield";}//Making sure a name is entered
if($email == ''){$error2 = "$emptyfield";}//Making sure the email addy is entered

//Borrowed this part from php.net
else{
if(eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$", trim($email))) {
$checkemail = 1;//Valid email addy
}
else{
$checkemail = 0;//Invalid email addy
}
if($checkemail == 0){
$error11 = "$invalidemail";
}
}
if($subject == ''){$error3 = "$emptyfield";}
if($message == ''){$error4 = "$emptyfield";}
if($Company == ''){$error5 = "$emptyfield";}
if($Contact == ''){$error6= "$emptyfield";}
if($PhoneNo == ''){$error7 = "$emptyfield";}
if($ModelNo == ''){$error8 = "$emptyfield";}
if($SerialIDNo == ''){$error9 = "$emptyfield";}
if($MeterReading == ''){$error10 = "$emptyfield";}
if($error1 || $error2 || $error3 || $error4 || $error5 || $error6 || $error7 || $error8 || $error9 || $error10){
        $error = "$incomplete";
}
//This will redirect to the main page if there are errors
if ($error1 || $error2 || $error3 || $error4 || $error5 || $error6 || $error7 || $error8 || $error9 || $error10 ||$error11) {
        $id = 'main';
        }
else{
//If everything is OK, send message
mail($adminemail,$subject,$message,"From: $name <$email>\nReply-To: $email");

// Show a successpage
$fileopen = fopen($thankspage, "r");
$page = fread($fileopen, filesize($thankspage));
echo $page;
}
}

###########################
#### The contact form #####
###########################
if ($id == 'main') {

$fileopen = fopen($contactpage, "r");
$page = fread($fileopen, filesize($contactpage));
$page = str_replace("{error}", $error, $page);
$page = str_replace("{error1}", $error1, $page);
$page = str_replace("{error2}", $error2, $page);
$page = str_replace("{error3}", $error3, $page);
$page = str_replace("{error4}", $error4, $page);
$page = str_replace("{error5}", $error5, $page);
$page = str_replace("{error6}", $error6, $page);
$page = str_replace("{error7}", $error7, $page);
$page = str_replace("{error8}", $error8, $page);
$page = str_replace("{error9}", $error9, $page);
$page = str_replace("{error10}", $error10, $page);
$page = str_replace("{error11}", $error11, $page);
$page = str_replace("{name}", $name, $page);
$page = str_replace("{email}", $email, $page);
$page = str_replace("{subject}", $subject, $page);
$page = str_replace("{message}", $message, $page);
echo $page;
}
?>[/code]

EDIT: Remember the code tags :)

View user's profile Send private message

Reply with quote
Post  
When I ran the script with a few modifications, such as removing all the error checking except for the e-mail address to see if it was entered and a valid address the script went through to the success page but did not send me an e-mail.

There was no error message but may be take a look on PHP.net to see if you have the mail() function correct.

View user's profile Send private message

Reply with quote
Post  
Actually, I took a look at the mail() function and it works just fine. I only just received the mail from my server.

So, if I were you, I would check to see if your mail function actually works;

Code:

<?PHP
$to = "your@email.com";
$subject = "Test Subject";
$contents = "Test Contents";
mail($to,$subject,$contents);
?>


Try this script to see if your server can actually send you an e-mail. Change the $to variable to your own e-mail address. If no mail gets through I recommend that you contact the support team.

View user's profile Send private message

Reply with quote
Post  
The mail function works differently on different machines because of the settings. I had met different types of configurations, this code worked perfectly for the mail function.

<?php
$to  = "receiveremail@mail.com" . ", " ;
$to .= "receiveremail2@mail.com";

$subject = "Replace this with your subject";

$message = 'this is is your message';

$headers = "From: Your Name <youremail@mail.com>";

mail($to, $subject, $message, $headers);
?>

This is the mail function only. If you still face the problem or too lazy to do it yourself Smile, do not hesitate - tell it. I will write the whole code for you.

Regards,
Pratamishus.


_________________
http://preloaders.net
Free animated GIF's generator for AJAX.
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

Reply with quote
Post  
[quote="catcalls"]Actually, I took a look at the mail() function and it works just fine. I only just received the mail from my server.

So, if I were you, I would check to see if your mail function actually works;

[code]
<?PHP
$to = "your@email.com";
$subject = "Test Subject";
$contents = "Test Contents";
mail($to,$subject,$contents);
?>
[/code]

Try this script to see if your server can actually send you an e-mail. Change the $to variable to your own e-mail address. If no mail gets through I recommend that you contact the support team.[/quote]

Thanks, Catcalls.  The mail function works fine on my server.  The form functions perfectly without any error checking.  It is the form validation that has got me stumped.  I don't want to use just javascript to validate as it is easy for someone to have it disabled.  I want the form to be sent only if all fields have data.  Seems my error checking routine is wrong.  I admit I have no idea how to fix it.

View user's profile Send private message

Reply with quote
Post  
[quote="pratamishus"]The mail function works differently on different machines because of the settings. I had met different types of configurations, this code worked perfectly for the mail function.

<?php
$to  = "receiveremail@mail.com" . ", " ;
$to .= "receiveremail2@mail.com";

$subject = "Replace this with your subject";

$message = 'this is is your message';

$headers = "From: Your Name <youremail@mail.com>";

mail($to, $subject, $message, $headers);
?>

This is the mail function only. If you still face the problem or too lazy to do it yourself :), do not hesitate - tell it. I will write the whole code for you.

Regards,
Pratamishus.[/quote]

Pratamishus,

It's not that I'm too lazy, it's just that I'm hopelessly lost.  I have absolutely no idea what is wrong with my error checking routine.  I would love to learn PHP, but I don't think my lame brain can cope--LOL!  Does the error checking even make sense to anyone?  I simply followed the pattern of an error checking routine I found online somewhere, changing to add my variables but I've obviously screwed the whole thing up.

View user's profile Send private message

Reply with quote
Post Re: PHP Form Mailer with Validation 
I gave up on the php validation and just got a perl script instead.  It worked perfectly first try.  Thanks for the attempt at helping.

View user's profile Send private message

Reply with quote
Post  
earthjargon,

If its the error checking, I thought I should mention this;

I have encountered servers that hate the short hand versions of AND, OR etc

such as ||, &&

basically, scripts just do not work with these servers that use || so that is a consideration.

Also, on your script you have to have every part of the form filled in otherwise it just will not send an e-mail.

I did check that the e-mail verification works, and that part of the verification of the form works.

One thing I tried, was to add the statements

echo "Here";

or

echo "Error";
to see where program flow was going.

This way, the script tells you if it encounters any errors.

View user's profile Send private message

Reply with quote
Post  
I have almost accomplished a script like yours. I will post it today. do not give up


_________________
http://preloaders.net
Free animated GIF's generator for AJAX.
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

Reply with quote
Post  
[quote="catcalls"]earthjargon,

If its the error checking, I thought I should mention this;

I have encountered servers that hate the short hand versions of AND, OR etc

such as ||, &&

basically, scripts just do not work with these servers that use || so that is a consideration.

Also, on your script you have to have every part of the form filled in otherwise it just will not send an e-mail.

[color=darkred]xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
----------------------This is exactly what I want to happen.  The form is for requesting a service call and we need all the information completed--------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[/color]

I did check that the e-mail verification works, and that part of the verification of the form works.

One thing I tried, was to add the statements

echo "Here";

or

echo "Error";
to see where program flow was going.

This way, the script tells you if it encounters any errors.[/quote]

[color=darkred]Thanks for this bit of information.  I will definitely use this to my advantage in the future.[/color] :D

View user's profile Send private message

Reply with quote
Post  
[quote="pratamishus"]I have almost accomplished a script like yours. I will post it today. do not give up[/quote]

The only reason I did give up was because I found an alternate script in perl that seems to work well with my server.  The thing that I have found is that one php script I have for forms seems to work fine but others don't.  I telnetted into my ftp server yesterday and typed in which php just out of curiosity and it couldn't find out, yet one of my php mail forms works; how is this possible?  Typing which perl found perl right away.  Tech support says php is available but why can't I seem to locate it?  Or doesn't the which command work for finding php?  Maybe I'm way off here.

View user's profile Send private message

Reply with quote
Post  
Hi!

I had problems like yours before. Sometimes the problem is with headers. Check out the php mail function that works on your server and customize all your other functions accordingly. Even though you have found a solution I am still sending you the script. It should work.

Code:
<?php
DEFINE("mymailer","verified"); //- place this for security on the page from where you send the form

if (isset($_POST['mail_checker']))
{
if(defined('mymailer'))
{
$adminemail = 'pratamishus@yahoo.com';//Your email address
$Company = stripslashes($_REQUEST["company"]);
$name = stripslashes($_REQUEST["contact"]);
$PhoneNo = stripslashes($_REQUEST["phoneno"]);
$ModelNo = stripslashes($_REQUEST["modelno"]);
$SerialIDNo = stripslashes($_REQUEST["serialidno"]);
$MeterReading = stripslashes($_REQUEST["meterreading"]);
$Problem = stripslashes($_REQUEST["problem"]);
$email = stripslashes($_REQUEST["email"]);//If Email addy is entered, then we proceed to check whether it is valid
$subject = "Service Request";
$message = stripslashes($_REQUEST["problem"]);

$message.= '
Additional details are:

company - '.$Company.'
name - '.$name.'
phone no - '.$PhoneNo.'
model no - '.$ModelNo.'
serial id no = '.$SerialIDNo.'
meter reading - '.$MeterReading.'
e-mail - '.$email.'
';

$to  = $adminemail;

$headers = "From: $name <$email>";
if (mail($to, $subject, $message, $headers))
{
$output='Email sent successfully';
}
else
{
$output='Could not send an email. Please try again later.';
}
}
else
{
$output = 'You can not use your own mailing form';
}
}
else
{
//DEFINE("mymailer","verified");
$output='
<script language="javascript">
function email_validator(email_string)
{
param = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

if (param.test(email_string))
return true;
else
return false;
}
function trim_string(str)
{
return str.replace(/^\s+/g, \'\').replace(/\s+$/g, \'\');
}
function validator()
{
document.submit_form.email.value = trim_string(document.submit_form.email.value);

document.submit_form.company.value=trim_string(document.submit_form.company.value);
document.submit_form.contact.value=trim_string(document.submit_form.contact.value);
document.submit_form.phoneno.value=trim_string(document.submit_form.phoneno.value);
document.submit_form.modelno.value=trim_string(document.submit_form.modelno.value);
document.submit_form.serialidno.value=trim_string(document.submit_form.serialidno.value);
document.submit_form.meterreading.value=trim_string(document.submit_form.meterreading.value);
document.submit_form.problem.value=trim_string(document.submit_form.problem.value);

if (document.submit_form.company.value==\'\')
{
alert(\'Fill in the Company field, please.\');
}
else if (document.submit_form.contact.value==\'\')
{
alert(\'Fill in the Name field, please.\');
}
else if (document.submit_form.phoneno.value==\'\')
{
alert(\'Fill in the Phone No. field, please.\');
}
else if (document.submit_form.modelno.value==\'\')
{
alert(\'Fill in the Model No field, please.\');
}
else if (document.submit_form.serialidno.value==\'\')
{
alert(\'Fill in the Serial Id No field, please.\');
}
else if (document.submit_form.meterreading.value==\'\')
{
alert(\'Fill in the Meter Reading field, please.\');
}
else if (document.submit_form.problem.value==\'\')
{
alert(\'Fill in the Problem field, please.\');
}
else
{
if (email_validator(document.submit_form.email.value))
document.submit_form.submit();
else
alert (\'You entered invalid e-mail.\');
}
}
</script>
<form action="index.php" method="post" name="submit_form">
<table border="0" width="525" class="simple">
<tr>
<td nowrap colspan=2><font><strong>Fill this form please:<br><br></strong></font></td>
</tr>
<tr>
<td>Company name<font color="#CE0101">*</font></td>
<td><input type="text" name="company" size=40></td>
</tr>
<tr>
<td>Your name<font color="#CE0101">*</font></td>
<td><input type="text" name="contact"></td>
</tr>
<tr>
<td valign="top">Your Problem<font color="#CE0101">*</font></td>
<td><textarea name="problem" cols="30" rows="6"></textarea></td>
</tr>
<tr>
<td>Your email<font color="#CE0101">*</font></td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Phone No.<font color="#CE0101">*</font></td>
<td><input type="text" name="phoneno" size=40></td>
</tr>
<tr>
<td>Model No.<font color="#CE0101">*</font></td>
<td><input type="text" name="modelno" size=20></td>
</tr>
<tr>
<td>Serial Id No</td>
<td><input type="text" name="serialidno" size=20></td>
</tr>
<tr>
<td width="30%">Meter Reading<font color="#CE0101">*</font></td>
<td><input type="text" name="meterreading" size=40></td></tr>
<tr>
<td colspan=2 align="left"><input type="hidden" name="mail_checker" value="true"><input type="button" onclick="validator()" value="Send Email" style="margin-top:2px;">&nbsp;&nbsp;&nbsp;<input type="reset" value="Clear fields" style="margin-top:2px;"></td>
</tr>   
</table>
</form>';
}

echo $output;
?>


This one uses JavaScript to check the form.
Regards,
Pratamishus


_________________
http://preloaders.net
Free animated GIF's generator for AJAX.
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

Reply with quote
Post  
[quote="pratamishus"]Hi!

I had problems like yours before. Sometimes the problem is with headers. Check out the php mail function that works on your server and customize all your other functions accordingly. Even though you have found a solution I am still sending you the script. It should work.

<SNIP>

This one uses JavaScript to check the form.
Regards,
Pratamishus[/quote]

Thank you for this help, pratamishus.  I will try it out to see if this one will work with my server.  I read through it and yours makes much more sense to me.  I can actually follow the logic of what you are doing.  I especially like the use of javascript to give them immediate feedback of what they have entered wrong.

View user's profile Send private message

Reply with quote
Post  
[quote="pratamishus"]The mail function works differently on different machines because of the settings. I had met different types of configurations, this code worked perfectly for the mail function.

<?php
$to  = "receiveremail@mail.com" . ", " ;
$to .= "receiveremail2@mail.com";

$subject = "Replace this with your subject";

$message = 'this is is your message';

$headers = "From: Your Name <youremail@mail.com>";

mail($to, $subject, $message, $headers);
?>

This is the mail function only. If you still face the problem or too lazy to do it yourself :), do not hesitate - tell it. I will write the whole code for you.

Regards,
Pratamishus.[/quote]

Pratamishus,

Thanks again for all your help.  Your php mail fuction worked perfectly for my server.  This has been a great learning experience for me all the way around.  Maybe there's hope for me yet!  And also thanks to catcalls for the tip adding the echos to check where the code is flowing.  I don't feel so lost as I did.

earthjargon

View user's profile Send private message

Reply with quote
Post  
You are welcome. I am happy it was useful.


_________________
http://preloaders.net
Free animated GIF's generator for AJAX.
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
Display posts from previous:
Reply to topic Page 1 of 2
Goto page 1, 2  Next
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
  



Google

FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Free Insurance Quotes!
Web Hosting Advertise Here $10 a Month Designer Children
Web Hosting and Dedicated Servers HGH Domain name registration
Insurance Affordable web-hosting


Web Design by PlatinumShore.com & Web Hosting by TradeWebHosting.com