Jump to content

Problem on simple if statement


foolygoofy26

Recommended Posts

I have a form on page A that gets a code and send it to page B, if the key is correct it will print out the name of X file as a link, if not correct, it will print out Access Denied. For some reason I get an error on page B on the line 2 of this code. Could someone tell me what is wrong in this if statement.


<?php
if ($_POST["code"] == "list")
echo "<a href=Downloads/list.txt target=top>See List</a>";

else
echo "Access Denied";
?>
Link to comment
https://forums.phpfreaks.com/topic/6055-problem-on-simple-if-statement/
Share on other sites

[!--quoteo(post=359510:date=Mar 28 2006, 08:52 PM:name=Cojawfee)--][div class=\'quotetop\']QUOTE(Cojawfee @ Mar 28 2006, 08:52 PM) [snapback]359510[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You have to use braces in PHP, they are not optional.
[/quote]
No, braces are optional if there is only one statement in the statement block. Read the [a href=\"http://us3.php.net/manual/en/language.control-structures.php\" target=\"_blank\"]manual[/a] and be enlightened.

BTW, the first response given is all wrong. There are invalid PHP start tags within the PHP statements.

To the OP, what is the error you get & do you have any PHP code above these statements? Many times PHP will print an message indicating an error on a particular line, but the error is really on a line above the one indicated. This usually happens when you haven't closed a quoted string.

Please post about 5 to 10 lines of code that precede these.

Ken
rather than checking that $_POST['code'] value also check that is actually set. This is why you are getting an error on your page B. So chnage you code to the following:

[code]<?php

//first check that $_POST['code'] is actually set and then chekc that its value is set to "list"
if (isset($_POST['code']) && $_POST['code'] == "list")
{
    echo "<a href=Downloads/list.txt target=top>See List</a>";
}
else
{
    echo "Access Denied";
}
?>[/code]
I would also recommend you to use indentaion with you code blocks this helps you to idently code bocks to code blocks for functions, if, if/ele/else if statements etc. ALso I would recommend to use braces too.
Thanks everyone for the advices. litebearer your example works. the only problem I ran into was that on page A when you type list you would get access denied and not the printed link as expected. Still, thanks I got the point and the lesson. Next stop for me is finding out a way to solve that little issue

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.