foolygoofy26 Posted March 28, 2006 Share Posted March 28, 2006 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>";elseecho "Access Denied"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/6055-problem-on-simple-if-statement/ Share on other sites More sharing options...
litebearer Posted March 29, 2006 Share Posted March 29, 2006 Try...[code]<?php if ($_POST["code"] == "list") { <? <a href="Downloads/list.txt" target="top">See List</a> <?PHP;}else{echo "Access Denied";}?>[/code]Note the use of bracesLite... Quote Link to comment https://forums.phpfreaks.com/topic/6055-problem-on-simple-if-statement/#findComment-21774 Share on other sites More sharing options...
Cojawfee Posted March 29, 2006 Share Posted March 29, 2006 You have to use braces in PHP, they are not optional. Quote Link to comment https://forums.phpfreaks.com/topic/6055-problem-on-simple-if-statement/#findComment-21802 Share on other sites More sharing options...
kenrbnsn Posted March 29, 2006 Share Posted March 29, 2006 [!--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 Quote Link to comment https://forums.phpfreaks.com/topic/6055-problem-on-simple-if-statement/#findComment-21816 Share on other sites More sharing options...
wildteen88 Posted March 29, 2006 Share Posted March 29, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/6055-problem-on-simple-if-statement/#findComment-21975 Share on other sites More sharing options...
foolygoofy26 Posted March 30, 2006 Author Share Posted March 30, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/6055-problem-on-simple-if-statement/#findComment-22419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.