Forum Moderators: coopster
I am having problems with this code:
<?php
$to='email@website.com';
$messageSubject='Contact from website';
$confirmationSubject='Website.com contact form confirmation';
$confirmationBody="Thank you for your message, here is a copy of the message you sent.\n\n";
$email='';
$enquiry='';
$body='';
$displayForm=true;
if ($_POST){
$confirmation=$_POST['confirmation'];
$email=stripslashes($_POST['email']);
$enquiry=stripslashes($_POST['enquiry']);
$body=stripslashes($_POST['body']);
// validate e-mail address
$valid=eregi('^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$',$email);
$crack=eregi("(\r¦\n)(to:¦from:¦cc:¦bcc:)",$body);
if ($email && $body && $valid &&!$crack){
if ( mail($to,$messageSubject,"Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$email."\r\n") && ($_POST['confirmation']=="y") ) {
mail($email,$confirmationSubject,$confirmationBody."Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$to."\r\n");
}
$displayForm=false;
}
?>
<p>
Your message was successfully sent.
In addition, a copy for you was sent to your e-mail address.
Your message is shown below.
</p>
<?php
echo '<p>'.'Enquiry:'.'\t'.htmlspecialchars($enquiry).'</p>';
echo '<p>'.'Message'.'\r\r'.htmlspecialchars($body).'</p>';
}else{ // the messages could not be sent
?>
<p>
Something went wrong when the server tried to send your message.
This is usually due to a server error, and is probably not your fault.
We apologise for any inconvenience caused.
</p>
<?php
}else if ($crack){ // cracking attempt
?>
<p><strong>
Your message contained e-mail headers within the message body.
This message has not been sent.
</strong></p>
<?php
}else{ // form not complete
?>
<p><strong>
Your message could not be sent.
You must include both a valid e-mail address and a message.
</strong></p>
<?php
}
}
if ($displayForm){
?>
<div id="contactform">
<form action="http://www.website.com/contact.php" method="post">
<label for="email">Your e-mail address</label>
<input type="text" name="email" id="email" value="<?php echo htmlspecialchars($email);?>" size="30">
<br /><br />
<label for="enquiry">Your enquiry</label>
<select name="enquiry" id="enquiry">
<option>Product information</option>
<option>Help</option>
<option>Personal message</option>
</select>
<br /><br />
<label for="body">Your message:</label>
<textarea name="body" id="body" cols="70" rows="5"><?php
if ($_POST){echo htmlspecialchars($body); }?></textarea>
<br /><br />
<label for="confirmation">Send yourself a copy? </label><input type="checkbox" name="confirmation" id="confirmation" value="y">
<br /><br />
<input type="submit" value="Submit">
</form></div><?php
}
?>
Which is that if the checkbox is ticked then it sends a confirmation message to the person.
This bit here:
if ( mail($to,$messageSubject,"Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$email."\r\n") && ($_POST['confirmation']=="y") ) {
mail($email,$confirmationSubject,$confirmationBody."Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$to."\r\n");
}
$displayForm=false;
may be the problem
Any ideas please?
It has an error but I am not sure how to fix it. The idea is that the the checkbox - if ticked means that the person who filled in the form gets a confirmation message.
if ( mail($to,$messageSubject,"Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$email."\r\n") && ($_POST['confirmation']=="y") ) {
mail($email,$confirmationSubject,$confirmationBody."Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$to."\r\n");
}
$displayForm=false;
Is the part that I am not sure about.
if ( mail($to,$messageSubject,"Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$email."\r\n") && ($_POST['confirmation']=="y") ) {
mail($email,$confirmationSubject,$confirmationBody."Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$to."\r\n");
}
$displayForm=false;
doesn't have any parse error. error has got to be somewhere else
1) you are having multiple "else" statements in one "if-else" structure, thats invalid.
2) you are defining "else if(something)" after an "else" statement, thats wrong too.
Thats the error.
$to='email@email.com';
$messageSubject='Contact from website';
$confirmationSubject='contact form confirmation';
$confirmationBody="Thank you for your message, here is a copy of the message you sent.\n\n";
$email='';
$enquiry='';
$body='';
$displayForm=true;
if ($_POST){
$confirmation=$_POST['confirmation'];
$email=stripslashes($_POST['email']);
$enquiry=stripslashes($_POST['enquiry']);
$body=stripslashes($_POST['body']);
// validate e-mail address
$valid=eregi('^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$',$email);
$crack=eregi("(\r¦\n)(to:¦from:¦cc:¦bcc:)",$body);
if ($email && $body && $valid &&!$crack){
mail($to,$messageSubject,"Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$email."\r\n");
$displayForm=false;
}
if ($_POST['confirmation']=="y") {
mail($email,$confirmationSubject,$confirmationBody."Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$to."\r\n");
$displayForm=false;
?>
<p>
Your message was successfully sent.
In addition, a copy for you was sent to your e-mail address.
Your message is shown below.
</p>
<?php
echo '<p>'.'Enquiry:'.'\t'.htmlspecialchars($enquiry).'</p>';
echo '<p>'.'Message'.'\r\r'.htmlspecialchars($body).'</p>';
} else{ // the messages could not be sent
?>
<p>
Something went wrong when the server tried to send your message.
This is usually due to a server error, and is probably not your fault.
We apologise for any inconvenience caused.
</p>
<?php
}else if ($crack){ // cracking attempt
?>
<p><strong>
Your message contained e-mail headers within the message body.
This message has not been sent.
</strong></p>
<?php
}else{ // form not complete
?>
<p><strong>
Your message could not be sent.
You must include both a valid e-mail address and a message.
</strong></p>
<?php
}
}
if ($displayForm){
?>
<div id="contactform">
<form action="http://www.contact2.php" method="post">
<label for="email">Your e-mail address</label>
<input type="text" name="email" id="email" value="<?php echo htmlspecialchars($email);?>" size="30">
<br /><br />
<label for="enquiry">Your enquiry</label>
<select name="enquiry" id="enquiry">
<option>Product information</option>
<option>Help</option>
<option>Personal message</option>
</select>
<br /><br />
<label for="body">Your message:</label>
<textarea name="body" id="body" cols="70" rows="5"><?php
if ($_POST){echo htmlspecialchars($body); }?></textarea>
<br /><br />
<label for="confirmation">Send yourself a copy? </label><input type="checkbox" name="confirmation" id="confirmation" value="y">
<br /><br />
<input type="submit" value="Submit">
</form></div><?php
}
require ("inc/footer.inc.php");
?>
I have tried to do it logically but it says
Parse error: parse error, unexpected T_ELSE in contact2.php on line 51
Which is:
}else if ($crack){ // cracking attempt
Any ideas?
Thanks
this is the if-else structure you are having in your code
if ($email && $body && $valid &&!$crack)
{
//something
}
if ($_POST['confirmation']=="y")
{
//something
}
else
{
//something
}
else if ($crack)
{
//something
}
else
{
//something
}
This has exact same errors as i mentioned in message #8 above. if you simply fix them it will be ok.
to be more specific,
you ll have to remove one "else"
and you ll have to put your "else if" before "else".
you can ask anyone else to confirm, any experts! come in here please.
<p>
Your message was successfully sent.
In addition, a copy for you was sent to your e-mail address.
Your message is shown below.
</p>
<?php
echo '<p>'.'Enquiry:'.'\t'.htmlspecialchars($enquiry).'</p>';
echo '<p>'.'Message'.'\r\r'.htmlspecialchars($body).'</p>';
} else if { // the messages could not be sent
?>
<p>
Something went wrong when the server tried to send your message.
This is usually due to a server error, and is probably not your fault.
We apologise for any inconvenience caused.
</p>
<?php
}else if ($crack){ // cracking attempt
?>
<p><strong>
Your message contained e-mail headers within the message body.
This message has not been sent.
</strong></p>
<?php
}else{ // form not complete
?>
<p><strong>
Your message could not be sent.
You must include both a valid e-mail address and a message.
</strong></p>
<?php
}
}
if ($displayForm){
?>
I thought I didn't need the:
} else if { // the messages could not be sent
because I can't see what the if does... whereas if I had it as just "else" then that would be the error response?
I may have two default error message if I remove the "if" from the
} else if { // the messages could not be sent
then it has
} else { // the messages could not be sent
and
}else{ // form not complete
How would I resolve this please?
<?php
$page="contact";
require ("inc/top.inc.php");
$to='email@email.com';
$messageSubject='Contact from website';
$confirmationSubject='contact form confirmation';
$confirmationBody="Thank you for your message, here is a copy of the message you sent.\n\n";
$email='';
$enquiry='';
$body='';
$displayForm=true;
if ($_POST)
{
$confirmation=$_POST['confirmation'];
$email=stripslashes($_POST['email']);
$enquiry=stripslashes($_POST['enquiry']);
$body=stripslashes($_POST['body']);
// validate e-mail address
$valid=eregi('^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$',$email);
$crack=eregi("(\r¦\n)(to:¦from:¦cc:¦bcc",$body); if ($email && $body && $valid &&!$crack)
{
mail($to,$messageSubject,"Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$email."\r\n");
$displayForm=false;
$mailStatus="Sent"; //This is a new variable, its value can be Sent or Failed
}
else if ($crack)
{
echo "
<p><strong>
Your message contained e-mail headers within the message body.
This message has not been sent.
</strong></p> ";
$mailStatus="Failed";
}
else if (!$valid)
{
echo "
<p><strong>
Your message could not be sent.
You must include both a valid e-mail address and a message.
</strong></p> ";
$mailStatus="Failed";
}
else
{ // the messages could not be sent
echo "
<p>
Something went wrong when the server tried to send your message.
This is usually due to a server error, and is probably not your fault.
We apologise for any inconvenience caused.
</p> ";
$mailStatus="Failed";
}
if ($_POST['confirmation']=="y" && $mailStatus=="Sent")
{
mail($email,$confirmationSubject,$confirmationBody."Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$to."\r\n");
$displayForm=false;
echo "
<p>
Your message was successfully sent.
In addition, a copy for you was sent to your e-mail address.
Your message is shown below.
</p> ";
echo '<p>'.'Enquiry:'.'\t'.htmlspecialchars($enquiry).'</p>';
echo '<p>'.'Message'.'\r\r'.htmlspecialchars($body).'</p>';
}
}
if ($displayForm)
{
?>
<div id="contactform">
<form action="http://www.contact2.php" method="post">
<label for="email">Your e-mail address</label>
<input type="text" name="email" id="email" value="<?php echo htmlspecialchars($email);?>" size="30">
<br /><br />
<label for="enquiry">Your enquiry</label>
<select name="enquiry" id="enquiry">
<option>Product information</option>
<option>Help</option>
<option>Personal message</option>
</select>
<br /><br />
<label for="body">Your message:</label>
<textarea name="body" id="body" cols="70" rows="5"><?php
if ($_POST){echo htmlspecialchars($body); }?></textarea>
<br /><br />
<label for="confirmation">Send yourself a copy? </label><input type="checkbox" name="confirmation" id="confirmation" value="y">
<br /><br />
<input type="submit" value="Submit">
</form></div><?php
}
require ("inc/footer.inc.php");
?>
howz that? ;)
replace this code block
if ($_POST['confirmation']=="y" && $mailStatus=="Sent")
{
mail($email,$confirmationSubject,$confirmationBody."Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$to."\r\n");
$displayForm=false;
echo "
<p>
Your message was successfully sent.
In addition, a copy for you was sent to your e-mail address.
Your message is shown below.
</p> ";
echo '<p>'.'Enquiry:'.'\t'.htmlspecialchars($enquiry).'</p>';
echo '<p>'.'Message'.'\r\r'.htmlspecialchars($body).'</p>';
}
with this one
if ($mailStatus=="Sent")
{
if($_POST['confirmation']=="y")
{
mail($email,$confirmationSubject,$confirmationBody."Enquiry:"."\t".$enquiry."\n\n"."Message:"."\n\n".$body,'From: '.$to."\r\n");
$displayForm=false;
echo "
<p>
Your message was successfully sent.
In addition, a copy for you was sent to your e-mail address.
Your message is shown below.
</p> ";
echo '<p>'.'Enquiry:'."\t".htmlspecialchars($enquiry).'</p>';
echo '<p>'.'Message'."\r\r".htmlspecialchars($body).'</p>';
}
else
{
echo "
<p>
Your message was successfully sent.
Your message is shown below.
</p> ";
echo '<p>'.'Enquiry:'."\t".htmlspecialchars($enquiry).'</p>';
echo '<p>'.'Message'."\r\r".htmlspecialchars($body).'</p>';}
}
and it will show confirmation in both cases and \n and \r werent formatted coz they were in single quotes, i changed em to double quotes and now they should work fine.
;)