Forum Moderators: coopster

Message Too Old, No Replies

And if not working

         

music_man

5:43 am on Nov 14, 2005 (gmt 0)

10+ Year Member



Hi

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?&nbsp;&nbsp;</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?

music_man

8:15 pm on Nov 15, 2005 (gmt 0)

10+ Year Member



bump...

coopster

3:17 am on Nov 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What is not working, music_man? You're going to have to be more specific about the issue here.

music_man

9:19 am on Nov 16, 2005 (gmt 0)

10+ Year Member



Hi

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.

coopster

12:26 pm on Nov 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What is the error though?

music_man

6:55 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



Parse error: parse error, unexpected T_ELSE in contact2.php on line 44

Anyango

7:00 pm on Nov 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




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

Anyango

7:21 pm on Nov 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please don't mind but this code is not at all following any rules of "if else" structure and that's the error.

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.

Anyango

7:25 pm on Nov 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If else Hierarchy should be

if(something)
{
do this
}
else if (something)
{
do this
}
else if (someotherthing)
{
do this
}
else
{
do this
}

niether can "else" come before "else if" (if both exist), nor can there be two "else" blocks in one if-else

Regards
Kami

music_man

11:15 pm on Nov 19, 2005 (gmt 0)

10+ Year Member



<?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;
}

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?&nbsp;&nbsp;</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

Anyango

5:56 am on Nov 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well well well , Looks like you don't trust i am saying right. This latest code has again all that disturbed if else structure as i mentioned before, nothing is changed.

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.

Anyango

5:59 am on Nov 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and BTW


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

This is exactly due to the reason that your "else-if"
is coming after "else".

music_man

6:14 am on Nov 20, 2005 (gmt 0)

10+ Year Member



I had no intention to insult you. If I have caused offence, I'm sorry. Thanks for your help so far. I will take another look and try to incorporate what you have said.

music_man

6:23 am on Nov 20, 2005 (gmt 0)

10+ Year Member



I think what it is, is

<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?

Anyango

6:43 am on Nov 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey, i did some alterations to your code, just put somethings up and some down but the logic is exactly same as you intented it to have, it works perfect, take a look.


<?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?&nbsp;&nbsp;</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? ;)

Anyango

6:45 am on Nov 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<edit>
Sorry, mistakenly double posted, deleting this one.
</edit>

music_man

6:59 am on Nov 20, 2005 (gmt 0)

10+ Year Member



Great! It sends the message to the user fi the tickbox is done. It doesn't show a "thanks" message if the checkbox isn't ticked. And the \t and \r are not formatted. Should I put another "else if" so that when the checkbox is ticked then it shows a different message or omits the "sent a copy to you" message"

Anyango

7:05 am on Nov 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ah yes, my mistake.

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.

;)

music_man

7:32 am on Nov 20, 2005 (gmt 0)

10+ Year Member



You are awesome! Thank you so much! It works fine!