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
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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.