<?php
$to = "service@mysite.no";
$subject = "Reparasjon av " . $_REQUEST['type'] . " fra mysite.no";
$types = if(!empty($_REQUEST['type'])) {echo($_REQUEST['type'] . ". ");};
$reps = if(!empty($_REQUEST['rep'])) {echo($_REQUEST['rep']);};
$message = $types . . $reps . "
Kommentarer:
" . $_REQUEST['kommentarer'] . "
Fra:
" . $_REQUEST['navn'] . "
Telefon: " . $_REQUEST['telefon'] . "
Email: " . $_REQUEST['email'] . "
Bosted: " . $_REQUEST['bosted'];
$headers = "From: " . $_REQUEST['email'] . "
" . 'MIME-Version: 1.0' . "
" . 'Content-type: text/plain; charset=UTF-8' . "
";
if (mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $headers)) {
header( 'Location: http://www.mysite.no/' );
} else {
header( 'Location: http://www.mysite.no/' );
}
?>
It says there's a T_IF error in line 4. What's the problem?
You can't use if
there, it's a syntax error. Technically if
is a statement, not an expression. That means you can't use it in an assignment like $types = if (...)
.
An IF statement does not return a value, so assigning it to a variable does nothing (and might even be causing your error!) Take off the semicolons from the end of the if statements, too.
Try this:
if (!empty($some_variable)) {
$my_var = $some_variable;
}
if()
is a language construct, not a function. It does not return anything, and cannot be assigned to a variable.
$types = if(!empty($_REQUEST['type'])) {echo($_REQUEST['type'] . ". ");};
^^^^^^^^--- not allowed
Try:
if (!empty($_REQUEST['type']) {
$types = $_REQUEST['type'];
}
As well, echo cause direct output to the client. it does not "return" anything that can be assigned.
The first thing I can see is in line $message = …
there is a double concatenation operator, which is clearly a syntax error. Should be (and should use escaped output):
$message = $types . $reps . "
Kommentarer:
" . $_REQUEST['kommentarer'] . "
Fra:
" . $_REQUEST['navn'] . "
Telefon: " . $_REQUEST['telefon'] . "
Email: " . $_REQUEST['email'] . "
Bosted: " . $_REQUEST['bosted'];
ps. god, so much wrong with this code (still no escaping/sanitazation in place) …
<?php
$to = "service@mysite.no";
$subject = "Reparasjon av " . $_REQUEST['type'] . " fra mysite.no";
$types = !empty($_REQUEST['type']) ? $_REQUEST['type'] . ". " : '';
$reps = !empty($_REQUEST['rep']) ? $_REQUEST['rep'] : '' ;
$message = $types . $reps . "
Kommentarer:
" . $_REQUEST['kommentarer'] . "
Fra:
" . $_REQUEST['navn'] . "
Telefon: " . $_REQUEST['telefon'] . "
Email: " . $_REQUEST['email'] . "
Bosted: " . $_REQUEST['bosted'];
$headers = "From: " . $_REQUEST['email'] . "
" . 'MIME-Version: 1.0' . "
" . 'Content-type: text/plain; charset=UTF-8' . "
";
if (mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $headers)) {
header( 'Location: http://www.mysite.no/' );
} else {
header( 'Location: http://www.mysite.no/' );
}
?>
The lines
$types = if(!empty($_REQUEST['type'])) {echo($_REQUEST['type'] . ". ");};
$reps = if(!empty($_REQUEST['rep'])) {echo($_REQUEST['rep']);};
are invalid. if
statements are not expressions in PHP; they don't evaluate to a value which can be assigned to a variable. You're also not "returning" anything from the if
; echo
writes to the screen, it doesn't "echo" a value out of an if statement to the calling scope.
You want the following:
if(!empty($_REQUEST['type'])) {
$types = ($_REQUEST['type'] . ". ");
}