开发者

add extra field to form not working

I have a working recommend to script, I just wanted to add a extra field named fename, now I get an error like this:

Parse error: syntax error, unexpected $end in

this is the html form:

<form action="action.php" method="post">
    Uw naam:<br /> 
    <input type="text" name="name" size="25"><br />
    <br />
    Uw e-mail adres:
     <br /> 
    <input type="text" name="email" size="25"><br />
    <br />
    De naam van uw kennis of vriend:
    <br /> 
    <input type="text" name="fename" size="25"><br />
    <br />
     Het e-mail adres van uw vriend of kennis:<br />
    <input name="femail" type="text" size="25"><br />
    <br />
    Bijzonderheden:<br>
    <textarea rows="5" name="recon" cols="75">
</textarea><br />
<input type="submit" name="submit" value="Aanbevelen!">
</form>

This is the action.php

<? 
if (!$_POST['name']) {echo "Je moet wel je naam invullen a.u.b."; } else {

if (!$_POST['email']) {echo "Je moet wel je e-mail adres invullen a.u.b."; } else {

if (!$_POST['fename']) {echo "Naam van uw vriend of vriendin invullen."; } else {

if (!$_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);

}

}

}



?>


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);

}

?>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜