tags:

views:

34

answers:

2
+5  A: 

You need another } since you added another nested if-else.

However, it'd be far better to refactor using elseif's since those are designed exactly for this type of chaining:

<?php 
if (!$_POST['name']) {echo "Je moet wel je naam invullen a.u.b."; } 
elseif (!$_POST['email']) {echo "Je moet wel je e-mail adres invullen a.u.b."; }
elseif (!$_POST['fename']) {echo "Naam van uw vriend of vriendin invullen."; }
elseif (!$_POST['femail']) {echo "Je moet wel het e-mail adres van uw vriend/familie of kennis invullen. a.u.b."; } 
else {
$name=$_POST['name'];
$email=$_POST['email'];
$fename=$_POST['fename'];
$femail=$_POST['femail'];
$recon=$_POST['recon'];
$recon=htmlspecialchars($recon);
$headers = "From: $email\r\nReply-To: $femail\r\n";


PRINT "Bedankt $name dat u ons heeft aanbevolen.";


mail("$femail", "Computerhulp is nabij!", "

Beste $name , $fename heeft u ons aanbevolen.

$recon
",$headers);

}

?>
Amber
+1  A: 

When you write a code, keep the block minimum. If you using the IDE, then it will tell you when you missed a }. Use a consistent bracket style and indentation to help you spot a bug.

Here is your code above but will be more readable and easier to find the bug:

<?php

$error = 0;
if (!$_POST['name']) {
  echo "Je moet wel je naam invullen a.u.b.";
  $error = 1;
}
if (!$_POST['email']) {
  echo "Je moet wel je e-mail adres invullen a.u.b.";
  $error = 1;
}
if (!$_POST['fename']) {
  echo "Naam van uw vriend of vriendin invullen.";
  $error = 1;
}
if (!$_POST['femail']) {
  echo "Je moet wel het e-mail adres van uw vriend/familie of kennis invullen. a.u.b.";
  $error = 1;
}
if ( $error == 0 ) {
  $name=$_POST['name'];
  $email=$_POST['email'];
  $fename=$_POST['fename'];
  $femail=$_POST['femail'];
  $recon=$_POST['recon'];
  $recon=htmlspecialchars($recon);
  $headers = "From: $email\r\nReply-To: $femail\r\n";

  PRINT "Bedankt $name dat u ons heeft aanbevolen.";

  mail("$femail", "Computerhulp is nabij!", "

Beste $name , $fename heeft u ons aanbevolen.

$recon
",$headers);

}

See how I use $error to prevent nested if-else.

Donny Kurnia
+1 I was typing a similar answer using multiple errors concatenated into `$errors` and checking that to see if it's blank at the end. Either way, avoiding unnecessary nesting is good, and representing every level of nesting with indentation is essential.
bobince
Great, thanks for your support. Does anyone happen to know how I can translate <br /> in a email message? My email client takes </br /> very litterly
Chris
I got it \r\n should do the trick, thanks anyways.
Chris
@Chris, It's better if you also store the email message into a variable, so the `mail` line will become like this: `mail($femail, $subject, $mail_content, $headers);`. To determine when to use variable or pass the value directly, if it will be more than 1 line or 60 column, then use variables.
Donny Kurnia