Log in

View Full Version : PHP form question



Austinkir
03-10-2009, 02:58 AM
Hi!
I am building contact page here theartslab.com/contact.php and I'm having trouble with the php code.

If I take the last lines out
else{
print "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\nIf you would like to contact us, you can send an email to info@theartslab.com as well."; }
}
Then it works as intended, but with those in i get this error: "Parse error: parse error, unexpected T_ELSE in /home/content/d/r/e/drekster1/html/contact2.php on line 29"

here is the full code:
<?php
$to = $_REQUEST['Email'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "****Web contact form****";

$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$fields{"list"} = "Mailing List";
$fields{"Message"} = "Message";

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$headers2 = "From: derek@theartslab.com";
$subject2 = "Thank you for visiting TheArtsLab!";
$autoreply = "Thank you for contacting TheArtsLab! We have recieved your message, and will be contacting you shortly if nessasary. If you signed up for our newsletter you have been entered into our system.\n\n<strong>Have a good day (or night)!</strong>";

if($from == '') {print "You have not entered an email, please go back and enter a valid email. We value your privacy and will never share it with any third parties... or forth and fifth parties. Never.";}
else {
if($name == '') {print "You have not entered a name, please go back enter one... We don't mind if its not your real one.";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
print "Thank you". $Name;
}
else {
print "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\nIf you would like to contact us, you can send an email to info@theartslab.com as well."; }
}
?>
and here is line 29:
else {


Thank you for any help!
Austin Kirschenmann

Nile
03-10-2009, 03:21 AM
Few things I noticed:

The { and } array use is being removed in PHP 6.
Variables should contain characters: [a-z]|[A-Z], not [a-z]|[A-Z]|[0-9]
Your code should be beautiful:


if(a){
return b;
} else {
return a;
}

Looks much better than.

if(a){
return b;}
else {return a;}





<?php
$to = $_REQUEST['Email'];
$from = $_REQUEST['Email'];
$name = $_REQUEST['Name'];
$headers[0] = "From: $from";
$subject[0] = "****Web contact form****";
$fields = array();
$fields["Name"] = "Name";
$fields["Email"] = "Email";
$fields["Phone"] = "Phone";
$fields["list"] = "Mailing List";
$fields["Message"] = "Message";
$body = "We have received the following information:\n\n";
foreach($fields as $a => $b) {
$body .= sprintf("%20s: %s\n", $b, $_REQUEST[$a]);
}
$headers[1] = "From: derek@theartslab.com";
$subject[1] = "Thank you for visiting TheArtsLab!";
$autoreply = "Thank you for contacting TheArtsLab! We have recieved your message, and will be contacting you shortly if nessasary. If you signed up for our newsletter you have been entered into our system.\n\n<strong>Have a good day (or night)!</strong>";
if (empty($from)) {
print "You have not entered an email, please go back and enter a valid email. We value your privacy and will never share it with any third parties... or forth and fifth parties. Never.";
} else {
if (empty($name)) {
print "You have not entered a name, please go back enter one... We don't mind if its not your real one.";
} else {
$send[0] = mail($to, $subject[0], $body, $headers[0]);
$send[1] = mail($from, $subject[1], $autoreply, $headers[1]);
print "Thank you".$name;
} else {
print "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\n If you would like to contact us, you can send an email to info@theartslab.com as well.";
}
}
?>


Give this a try:

Austinkir
03-10-2009, 04:21 AM
Thank you for your reply. I plugged in the code, and it is not working, in the same spot as before, where the code will give a message if an error occurs in sending. You mentioned php 6, so I checked the version my server is running, and its 4.3.11. Could that be why its not working?

Thanks again!

Austin

JasonDFR
03-10-2009, 07:45 AM
if($from == '') {

print "";

} else {

if($name == '') {

print "You hcks not your real one.";

} else { // change this to elseif .

$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
print "Thank you". $Name;

} else { // As written, this is wrong

print "We encounteredab.com as well.";
}
}
?>

You can not have two else statements in a row. Once you use else, the conditional is done. This is why you are getting an error when the last else is parsed.

And as Nile said, make you code readable. Use spaces, tabs, and line spaces. Everything inside if / else statements and loops should be indented. Pur curly brackets on the same lines as the ifs and elses. Figuring out problems is fun, formatting code is not.

JasonDFR
03-10-2009, 07:55 AM
Few things I noticed:
[LIST]
The { and } array use is being removed in PHP 6.
Variables should contain characters: [a-z]|[A-Z], not [a-z]|[A-Z]|[0-9]
Your code should be beautiful:


Why shouldn't variables contain numbers?

Nile
03-10-2009, 11:54 AM
If you were to do algebra with php, it would get all messed up(in your mind):




$a3 = 4; //12
$a2 = 5; //10
$b = 7; //7



Mabye changing:



if (empty($from)) {
print "You have not entered an email, please go back and enter a valid email. We value your privacy and will never share it with any third parties... or forth and fifth parties. Never.";
} else {
if (empty($name)) {
print "You have not entered a name, please go back enter one... We don't mind if its not your real one.";
} else {
$send[0] = mail($to, $subject[0], $body, $headers[0]);
$send[1] = mail($from, $subject[1], $autoreply, $headers[1]);
print "Thank you".$name;
} else {
print "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\n If you would like to contact us, you can send an email to info@theartslab.com as well.";
}


To:


if (empty($from)) {
print "You have not entered an email, please go back and enter a valid email. We value your privacy and will never share it with any third parties... or forth and fifth parties. Never.";
} else {
if (empty($name)) {
print "You have not entered a name, please go back enter one... We don't mind if its not your real one.";
} else if(!empty($name)){
$send[0] = mail($to, $subject[0], $body, $headers[0]);
$send[1] = mail($from, $subject[1], $autoreply, $headers[1]);
if($send[0] && $send[1]){
print "Thank you ".$name;
} else {
echo "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\n If you would like to contact us, you can send an email to info@theartslab.com as well.";
exit;
} else {
print "We encountered an error sending your mail, we would be eternally grateful if you were to notify webmaster@theartslab.com \n\n If you would like to contact us, you can send an email to info@theartslab.com as well.";
}
Change the highlighted && to be || if you only care if one email was sent.

JasonDFR
03-10-2009, 12:13 PM
If you were to do algebra with php, it would get all messed up(in your mind):




$a3 = 4; //12
$a2 = 5; //10
$b = 7; //7





I understand that 3 * 4 = 12, but this isn't algebra. Is this something you heard somewhere, came up with yourself, or a best practice for programming?

Nile
03-10-2009, 12:17 PM
It gets confusing when you get to the point using numbers... I mean you can, its just confusing.

Using empty, is better than is_null or the $var == "".

JasonDFR
03-10-2009, 12:27 PM
Using empty, is better than is_null or the $var == "".

Why do you think this?

Nile
03-10-2009, 10:53 PM
Because it tests for both.