0

Please professional programmers in the house, what is wrong with this code?

I get this error whenever i try to run it.

Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\a\go.php on line 8

The php code:

<?php

$term=$_POST['term'];
$level=$_POST['level'];

if  (

$term = 'First';
$level='js1';

)
{
header("Location: result.php");
exit();
} 
elseif (
$term = 'First';
$level='js2'; 
)
{
header("Location: result2.php");
exit();
} 

else {
$error = "Entry is invalid";
}

?>
Charles Nwankwo
  • 31
  • 1
  • 1
  • 5

6 Answers6

4

Check your if condition. The if and else if conditions must not contain any semicolons. If you are using two comparisons you must use && or || in between them. If you are using = in the if and elseif statement then it will always return true.

<?php

$term=$_POST['term'];
$level=$_POST['level'];

if  ($term == 'First' && $level=='js1')
  {
    header("Location: result.php");
    exit();
  } 
else if ($term == 'First' && $level='js2')
  {
    header("Location: result2.php");
    exit();
  } 
else {
  $error = "Entry is invalid";
}

?>
Viswanath Polaki
  • 1,357
  • 1
  • 10
  • 19
2

Change your if condition

it should be

if($term = 'First'&& $level='js1') or if($term = 'First'|| $level='js1') 

   elseif ($term = 'First' && $level='js2') or elseifif($term = 'First'|| $level='js2')

not

if  ($term = 'First'; $level='js1';)

elseif ($term = 'First' ; $level='js2';)
Arun Kumaresh
  • 6,211
  • 6
  • 32
  • 50
1

All of your if statements have a malformed format. All you are doing is setting variables within your if statement. Therefore you are not using assignment operators properly. An example of how your if statement should look is:

if($condition === true || $condition2 == 'hello'){
    doSomething();
} else if($condition === false || $condtion2 == 'bye'){
    doSomethingElse();
}

EDIT: Also i would recommend working on your code indentation skills, this will really help to read your code in the future.

Lewis Browne
  • 904
  • 7
  • 23
  • This is the best answer. Not just writing code for the asker. but explaining why it is not working. – Stender Aug 18 '17 at 10:34
  • Thanks for the upvote, I try to always use the `give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime` philosophy. – Lewis Browne Aug 18 '17 at 10:35
0

You are not making correct boolean expressions in those ifs.

if ($term === 'first' && $level === 'js1') {...}

elseif($term === 'First' && $level === 'js2') {...}

Also, I strongly recommend you to put a die(); after a redirect to avoid unnecessary load (unless you need to execute code after the redirect).

-1

try this.

<?php

    $term=$_POST['term'];
    $level=$_POST['level'];

    if( $term == 'First' && $level=='js1'){
        header("Location: result.php");
        exit();
    } elseif ( $term == 'First' && $level=='js2'){
        header("Location: result2.php");
        exit();
    } else {
        $error = "Entry is invalid";
    }
?>
Nazish Fraz
  • 94
  • 1
  • 9
-1

If there is a header already sent error you receive then put ob_start() to top of your php file OR You can also use javascript for this like below.

<script>window.location.href = "a.php";</script>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
prashant
  • 184
  • 1
  • 9