I can´t find the problem in the form

A

Anonymous

Guest
why do I get

Parse error: syntax error, unexpected identifier "end", expecting "," or ";" in /customers/b/d/3/stene.one/httpd.www/lifa/send.php on line 45 Parse error: syntax error, unexpected identifier "end", expecting "," or ";" in /customers/b/d/3/stene.one/httpd.www/lifa/send.php on line 45

with the code
Code:
<?php
 
// anger en variabel som kan lagra de eventuella felaktigheterna
$errors = array();
// kontrollera om ett Förnamn angivits
if (!$_POST["fnamn"])
$errors[] = "- FÖRNAMN";
// kontrollera om ett Efternamn angivits
if (!$_POST["enamn"])
$errors[] = "- EFTERNAMN";
// kontrollera om en E-postadress angivits
if (!$_POST["email"])
$errors[] = "- E-POSTADRESS";
// kontrollera om ett Meddelande angivits
if (!$_POST["message"])
$errors[] = "- inget MEDDELANDE har skrivits!";
// om felaktig information finns visas detta meddelande
if (count($errors)>0){
echo "<strong>Följande information måste anges innan du kan skicka formuläret:</strong><br />";
foreach($errors as $fel)
echo "$fel <br />";
echo "<br />Ange den information som saknas och skicka formuläret igen. Tack! <br />";
echo "<a href='javascript:history.go(-1)'>klicka här för att komma tillbaka till formuläret</a>";
}
else {
// formuläret är korrekt ifyllt och informationen bearbetas
$to = "info@stene.one";
$from = $_POST["email"];
$subject = 'Kontakt från webbplatsen!';
$fnamn = $_POST["fnamn"];
$enamn = $_POST["enamn"];
$message = $_POST["message"];

########################################################################
// HEADERS för innehållstyp och textkodning
$headers = "Content-Type: text/plain; charset=utf-8 \r\n"; 
$headers .= "From:".$fnamn." ".$enamn." <".$from.">"."\r\n";
$headers .= "MIME-Version: 1.0 \r\n";
########################################################################
if (mail($to, $subject, $message, $headers))
echo nl2br("<h2>Ditt meddelande har skickats!</h2>
<b>mottagare:</b> $to
<b>meddelande:</b>
$message;")
end if;}
?>
 
What IDE do you use? That kind of error should be pointed by editor.
I use Visual Studio Code (do not confuse with Visual Studio) with php extensions.
I can help you with the whole configuration.

The error message tell you exactly where and what is ircorrect. After the nl2br function the ; character was expected but it was missing and the next word was found.
You should also remove the "end if".

Please use the
Code:
 and
tags for your code next time (or use gist). No one wants to spend time to read an illegible code
 
The last if loop doesn't have a opening bracket and get rid of the end if.
 
Michalio said:
What IDE do you use? That kind of error should be pointed by editor.
I use Visual Studio Code (do not confuse with Visual Studio) with php extensions.
I can help you with the whole configuration.

The error message tell you exactly where and what is ircorrect. After the nl2br function the ; character was expected but it was missing and the next word was found.
You should also remove the "end if".

Please use the
Code:
 and
tags for your code next time (or use gist). No one wants to spend time to read an illegible code

I use Coffeecup
 
Please pay attention to code formatting, it will be easier to find any issue.
Check this code:
https://gist.github.com/PphEight/f81e26ea6b75e14156ed0881114beef3
You can check all changes here:
https://gist.github.com/PphEight/f81e26ea6b75e14156ed0881114beef3/revisions#diff-2f69267b4ca80fc98e9bee32835c2c97440eeb54ab0405b6732866fac162796a
 
Code:
<?php
 

// anger en variabel som kan lagra de eventuella felaktigheterna
$errors = array();

// kontrollera om ett Förnamn angivits
if (!$_POST["fnamn"])
$errors[] = "- FÖRNAMN";
{
    $errors[] = "- FÖRNAMN";
}

// kontrollera om ett Efternamn angivits
if (!$_POST["enamn"])
$errors[] = "- EFTERNAMN";
{
    $errors[] = "- EFTERNAMN";
}

// kontrollera om en E-postadress angivits
if (!$_POST["email"])
$errors[] = "- E-POSTADRESS";
{
    $errors[] = "- E-POSTADRESS";
}

// kontrollera om ett Meddelande angivits
if (!$_POST["message"])
$errors[] = "- inget MEDDELANDE har skrivits!";
{
    $errors[] = "- inget MEDDELANDE har skrivits!";
}

// om felaktig information finns visas detta meddelande
if (count($errors)>0){
echo "<strong>Följande information måste anges innan du kan skicka formuläret:</strong><br />";
foreach($errors as $fel)
echo "$fel <br />";
echo "<br />Ange den information som saknas och skicka formuläret igen. Tack! <br />";
echo "<a href='javascript:history.go(-1)'>klicka här för att komma tillbaka till formuläret</a>";
if (count($errors) > 0)
{
    echo "<strong>Följande information måste anges innan du kan skicka formuläret:</strong><br />";
    foreach ($errors as $fel)
    {
        echo "$fel <br />";
    }

    echo "<br />Ange den information som saknas och skicka formuläret igen. Tack! <br />";
    echo "<a href='javascript:history.go(-1)'>klicka här för att komma tillbaka till formuläret</a>";
}
else
{
    // formuläret är korrekt ifyllt och informationen bearbetas
    $to = "info@stene.one";
    $from = $_POST["email"];
    $subject = 'Kontakt från webbplatsen!';
    $fnamn = $_POST["fnamn"];
    $enamn = $_POST["enamn"];
    $message = $_POST["message"];

    ########################################################################
    // HEADERS för innehållstyp och textkodning
    $headers = "Content-Type: text/plain; charset=utf-8\r\n"; 
    $headers .= 'From:' . $fnamn . ' ' . $enamn . ' <' . $from . '>' . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";

    ########################################################################
    if (mail($to, $subject, $message, $headers))
    {
        echo "<h2>Ditt meddelande har skickats!</h2><br><b>mottagare:</b> $to<br><b>meddelande:</b><br>$message;";
    }
}
else {
// formuläret är korrekt ifyllt och informationen bearbetas
$to = "info@stene.one";
$from = $_POST["email"];
$subject = 'Kontakt från webbplatsen!';
$fnamn = $_POST["fnamn"];
$enamn = $_POST["enamn"];
$message = $_POST["message"];

########################################################################
// HEADERS för innehållstyp och textkodning
$headers = "Content-Type: text/plain; charset=utf-8 \r\n"; 
$headers .= "From:".$fnamn." ".$enamn." <".$from.">"."\r\n";
$headers .= "MIME-Version: 1.0 \r\n";
########################################################################
if (mail($to, $subject, $message, $headers))
echo nl2br("<h2>Ditt meddelande har skickats!</h2>
<b>mottagare:</b> $to
<b>meddelande:</b>
$message;")
end if;}
?>

gives me
Parse error: syntax error, unexpected token "else" in /customers/b/d/3/stene.one/httpd.www/lifa/send.php on line 75

This is weird, It looks correct to me
 
You have something like that:
Code:
if( /* condition */ )
{
    // Code
}
else
{
    // Code
}
else
{
    // Code
}
The second else statement is duplicated and you can remove it. Only elseif statement can be multiple times in one if .. elseif .. else structure.

If you want I can show you how to configure your IDE to point all of similar issues to fix them easier
 
Code:
<?php
       $to = "info@stene.one";    
       $fnamn = $_POST["fnamn"];
       $enamn = $_POST["enamn"];
       $epost = $_POST["epost"];
       $meddelande = $_POST["meddelande"];


    mail($to, $fnamn, $enamn, $meddelande);

if(!$to->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}                                         

?>

this works a little better, just having problems to catch any error
Fatal error: Uncaught Error: Call to a member function send() on string in /customers/b/d/3/stene.one/httpd.www/lifa/send.php:11 Stack trace: #0 {main} thrown in /customers/b/d/3/stene.one/httpd.www/lifa/send.php on line 11
 
The $to variable is a string, so it have no send() method. If you want to check that the mail was sent then assing the results of the function (not method) send and then check if it is true (mail sent) or false (mail not sent).
I sent to you a private message
 
Code:
<?php
       $to = "info@stene.one";    
       $fnamn = $_POST["fnamn"];
       $enamn = $_POST["enamn"];
       $epost = $_POST["epost"];
       $meddelande = $_POST["meddelande"];


    mail($to, $fnamn, $enamn, $meddelande);    
 
if(!$epost->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}                                                                     
?>

epost is not a string but I still get
Fatal error: Uncaught Error: Call to a member function send() on string in /customers/b/d/3/stene.one/httpd.www/lifa/send.php:11 Stack trace: #0 {main} thrown in /customers/b/d/3/stene.one/httpd.www/lifa/send.php on line 11
 
Code:
<?php
 

// anger en variabel som kan lagra de eventuella felaktigheterna
$errors = array();

// kontrollera om ett Förnamn angivits
if (!$_POST["fnamn"])
$errors[] = "- FÖRNAMN";
{
    $errors[] = "- FÖRNAMN";
}

// kontrollera om ett Efternamn angivits
if (!$_POST["enamn"])
$errors[] = "- EFTERNAMN";
{
    $errors[] = "- EFTERNAMN";
}

// kontrollera om en E-postadress angivits
if (!$_POST["email"])
$errors[] = "- E-POSTADRESS";
{
    $errors[] = "- E-POSTADRESS";
}

// kontrollera om ett Meddelande angivits
if (!$_POST["message"])
$errors[] = "- inget MEDDELANDE har skrivits!";
{
    $errors[] = "- inget MEDDELANDE har skrivits!";
}

// om felaktig information finns visas detta meddelande
if (count($errors)>0){
echo "<strong>Följande information måste anges innan du kan skicka formuläret:</strong><br />";
foreach($errors as $fel)
echo "$fel <br />";
echo "<br />Ange den information som saknas och skicka formuläret igen. Tack! <br />";
echo "<a href='javascript:history.go(-1)'>klicka här för att komma tillbaka till formuläret</a>";
}
if (count($errors) > 0)
{
    echo "<strong>Följande information måste anges innan du kan skicka formuläret:</strong><br />";
    foreach ($errors as $fel)
    {
        echo "$fel <br />";
    }

    echo "<br />Ange den information som saknas och skicka formuläret igen. Tack! <br />";
    echo "<a href='javascript:history.go(-1)'>klicka här för att komma tillbaka till formuläret</a>";
}
else
{
    // formuläret är korrekt ifyllt och informationen bearbetas
    $to = "info@stene.one";
    $from = $_POST["email"];
    $subject = 'Kontakt från webbplatsen!';
    $fnamn = $_POST["fnamn"];
    $enamn = $_POST["enamn"];
    $message = $_POST["message"];

    ########################################################################
    // HEADERS för innehållstyp och textkodning
    $headers = "Content-Type: text/plain; charset=utf-8\r\n"; 
    $headers .= 'From:' . $fnamn . ' ' . $enamn . ' <' . $from . '>' . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";

    ########################################################################
    if (mail($to, $subject, $message))
    {
        echo "<h2>Ditt meddelande har skickats!</h2><br><b>mottagare:</b> $to<br><b>meddelande:</b><br>$message;";
    }
}

// formuläret är korrekt ifyllt och informationen bearbetas
$to = "info@stene.one";
$from = $_POST["email"];
$subject = 'Kontakt från webbplatsen!';
$fnamn = $_POST["fnamn"];
$enamn = $_POST["enamn"];
$message = $_POST["message"];

########################################################################
// HEADERS för innehållstyp och textkodning
$headers = "Content-Type: text/plain; charset=utf-8 \r\n"; 
$headers .= "From:".$fnamn." ".$enamn." <".$from.">"."\r\n";
$headers .= "MIME-Version: 1.0 \r\n";
########################################################################
if (mail($to, $subject, $message))
echo nl2br("<h2>Ditt meddelande har skickats!</h2>
<b>mottagare:</b> $to
<b>meddelande:</b>
$message;")
?>

This is the result
Warning: Undefined array key "email" in /customers/b/d/3/stene.one/httpd.www/lifa/send.php on line 22 Warning: Undefined array key "message" in /customers/b/d/3/stene.one/httpd.www/lifa/send.php on line 29 Följande information måste anges innan du kan skicka formuläret:
- FÖRNAMN
- EFTERNAMN
- E-POSTADRESS
- E-POSTADRESS
- inget MEDDELANDE har skrivits!
- inget MEDDELANDE har skrivits!

Ange den information som saknas och skicka formuläret igen. Tack!
klicka här för att komma tillbaka till formuläretFöljande information måste anges innan du kan skicka formuläret:
- FÖRNAMN
- EFTERNAMN
- E-POSTADRESS
- E-POSTADRESS
- inget MEDDELANDE har skrivits!
- inget MEDDELANDE har skrivits!

Ange den information som saknas och skicka formuläret igen. Tack!
klicka här för att komma tillbaka till formuläret Warning: Undefined array key "email" in /customers/b/d/3/stene.one/httpd.www/lifa/send.php on line 79 Warning: Undefined array key "message" in /customers/b/d/3/stene.one/httpd.www/lifa/send.php on line 83
Ditt meddelande har skickats!

mottagare: info@stene.one
meddelande:
;
 
First of all try to keep code formatted correctly, then you will be able to find quickly that the one if statement is duplicated:
Code:
if (count($errors)>0){
That if statement and the code block inside can be deleted.
You also duplicate the code responsed for sending the email.
You also created a weird code:
Code:
if (!$_POST["message"])
$errors[] = "- inget MEDDELANDE har skrivits!";
{
    $errors[] = "- inget MEDDELANDE har skrivits!";
}
Is it works?

Let's read the warning. The email and message is undefined thats mean you do not send it, check the form and verify that there are fields for email and message.

Keep code clean, adding indentions let you know better what is going on in the code. Without that you are lost in shirt code.
Read about good practices: https://www.php-fig.org/psr/psr-12/
 
Back
Top